LP#1152753: upgrade script
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 1 Sep 2017 21:26:32 +0000 (17:26 -0400)
committerMike Rylander <mrylander@gmail.com>
Fri, 1 Sep 2017 21:17:47 +0000 (17:17 -0400)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_vii_fake_fkey.sql [new file with mode: 0644]

diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_vii_fake_fkey.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_vii_fake_fkey.sql
new file mode 100644 (file)
index 0000000..8c2b1e4
--- /dev/null
@@ -0,0 +1,19 @@
+BEGIN;
+
+CREATE OR REPLACE FUNCTION evergreen.vandelay_import_item_imported_as_inh_fkey() RETURNS TRIGGER AS $f$
+BEGIN   
+        IF NEW.imported_as IS NULL THEN
+                RETURN NEW;
+        END IF;
+        PERFORM 1 FROM asset.copy WHERE id = NEW.imported_as;
+        IF NOT FOUND THEN
+                RAISE foreign_key_violation USING MESSAGE = FORMAT(
+                        $$Referenced asset.copy id not found, imported_as:%s$$, NEW.imported_as
+                );
+        END IF;
+        RETURN NEW;
+END;
+$f$ LANGUAGE PLPGSQL VOLATILE COST 50;
+
+
+COMMIT;