LP#1533331 - Fix evergreen.protect_reserved_rows_from_delete() trigger function
authorChris Sharp <csharp@georgialibraries.org>
Mon, 8 Feb 2016 18:00:06 +0000 (13:00 -0500)
committerJason Stephenson <jstephenson@mvlc.org>
Fri, 4 Mar 2016 16:24:47 +0000 (11:24 -0500)
The fix for bug 1513554 included creating a general trigger function to prevent
the deletion of reserved table rows with an ID lower than a specified threshold.
Testing for that seemed to pass muster, but using the function in practice revealed
a failure to delete deletable rows either.

This adds an explicit RETURN to the function, which fixes the issue.

Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/src/sql/Pg/000.functions.general.sql
Open-ILS/src/sql/Pg/t/lp1513554_do_not_delete_reserved_cancel_reasons.pg
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_protect_reserved_rows_from_delete.sql [new file with mode: 0644]

index 0cfa909..ad6182e 100644 (file)
@@ -81,6 +81,7 @@ BEGIN
 IF OLD.id < TG_ARGV[0]::INT THEN
     RAISE EXCEPTION 'Cannot delete row with reserved ID %', OLD.id;
 END IF;
+RETURN OLD;
 END
 $protect_reserved$
 LANGUAGE plpgsql;
index 8042860..7dedeb2 100644 (file)
@@ -1,6 +1,6 @@
 BEGIN;
 
-SELECT plan(1);
+SELECT plan(3);
 
 SELECT throws_ok(
     'delete from acq.cancel_reason where id = 1', 
@@ -8,6 +8,16 @@ SELECT throws_ok(
     'Cannot delete row with reserved ID 1'
 );
 
+SELECT lives_ok(
+    'insert into acq.cancel_reason (id, org_unit, label, description) values (3001, 1, ''Test Reason'', ''Test Cancel Reason'')',
+    'Creating test cancel reason'
+);
+
+SELECT lives_ok(
+    'delete from acq.cancel_reason where id = 3001',
+    'Testing delete of non-reserved cancel reason'
+);
+
 SELECT * FROM finish();
 
 ROLLBACK;
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_protect_reserved_rows_from_delete.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_protect_reserved_rows_from_delete.sql
new file mode 100644 (file)
index 0000000..6a527a9
--- /dev/null
@@ -0,0 +1,15 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+CREATE OR REPLACE FUNCTION evergreen.protect_reserved_rows_from_delete() RETURNS trigger AS $protect_reserved$
+BEGIN
+IF OLD.id < TG_ARGV[0]::INT THEN
+    RAISE EXCEPTION 'Cannot delete row with reserved ID %', OLD.id; 
+END IF;
+RETURN OLD;
+END
+$protect_reserved$
+LANGUAGE plpgsql;
+
+COMMIT;