From: miker Date: Tue, 12 Apr 2011 18:16:07 +0000 (+0000) Subject: Add a trigger function to simulate FKEY constraints on inherited tables X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=d429c2d3bcd1ab3152de1091a4e7699a4af7114a;p=contrib%2FConifer.git Add a trigger function to simulate FKEY constraints on inherited tables git-svn-id: svn://svn.open-ils.org/ILS/trunk@20053 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/src/sql/Pg/000.functions.general.sql b/Open-ILS/src/sql/Pg/000.functions.general.sql index 2f09a50d95..5204ceee1e 100644 --- a/Open-ILS/src/sql/Pg/000.functions.general.sql +++ b/Open-ILS/src/sql/Pg/000.functions.general.sql @@ -24,6 +24,19 @@ EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET ' || quote END; $$ LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION evergreen.fake_fkey_tgr () RETURNS TRIGGER AS $F$ +DECLARE + copy_id BIGINT; +BEGIN + EXECUTE 'SELECT ($1).' || quote_ident(TG_ARGV[0]) INTO copy_id USING NEW; + PERFORM * FROM asset.copy WHERE id = copy_id; + IF NOT FOUND THEN + RAISE EXCEPTION 'Key (%.%=%) does not exist in asset.copy', TG_TABLE_SCHEMA, TG_TABLE_NAME, copy_id; + END IF; + RETURN NULL; +END; +$F$ LANGUAGE PLPGSQL; + SELECT evergreen.change_db_setting('search_path', ARRAY['evergreen','public','pg_catalog']); COMMIT; diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 1e3752ef28..43390dab12 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -70,7 +70,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0510'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0511'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/090.schema.action.sql b/Open-ILS/src/sql/Pg/090.schema.action.sql index 3a34659d06..12dbec0478 100644 --- a/Open-ILS/src/sql/Pg/090.schema.action.sql +++ b/Open-ILS/src/sql/Pg/090.schema.action.sql @@ -151,6 +151,8 @@ CREATE INDEX action_circulation_target_copy_idx ON action.circulation (target_co CREATE UNIQUE INDEX circ_parent_idx ON action.circulation ( parent_circ ) WHERE parent_circ IS NOT NULL; CREATE UNIQUE INDEX only_one_concurrent_checkout_per_copy ON action.circulation(target_copy) WHERE checkin_time IS NULL; +CREATE TRIGGER action_circulation_target_copy_trig AFTER INSERT OR UPDATE ON action.circulation FOR EACH ROW EXECUTE PROCEDURE evergreen.fake_fkey_tgr('target_copy'); + CREATE TRIGGER mat_summary_create_tgr AFTER INSERT ON action.circulation FOR EACH ROW EXECUTE PROCEDURE money.mat_summary_create ('circulation'); CREATE TRIGGER mat_summary_change_tgr AFTER UPDATE ON action.circulation FOR EACH ROW EXECUTE PROCEDURE money.mat_summary_update (); CREATE TRIGGER mat_summary_remove_tgr AFTER DELETE ON action.circulation FOR EACH ROW EXECUTE PROCEDURE money.mat_summary_delete (); diff --git a/Open-ILS/src/sql/Pg/upgrade/0511.schema.fake_fkey_tgr-for-inherited-tables.sql b/Open-ILS/src/sql/Pg/upgrade/0511.schema.fake_fkey_tgr-for-inherited-tables.sql new file mode 100644 index 0000000000..71e0b44886 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0511.schema.fake_fkey_tgr-for-inherited-tables.sql @@ -0,0 +1,21 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0511'); -- miker + +CREATE OR REPLACE FUNCTION evergreen.fake_fkey_tgr () RETURNS TRIGGER AS $F$ +DECLARE + copy_id BIGINT; +BEGIN + EXECUTE 'SELECT ($1).' || quote_ident(TG_ARGV[0]) INTO copy_id USING NEW; + PERFORM * FROM asset.copy WHERE id = copy_id; + IF NOT FOUND THEN + RAISE EXCEPTION 'Key (%.%=%) does not exist in asset.copy', TG_TABLE_SCHEMA, TG_TABLE_NAME, copy_id; + END IF; + RETURN NULL; +END; +$F$ LANGUAGE PLPGSQL; + +CREATE TRIGGER action_circulation_target_copy_trig AFTER INSERT OR UPDATE ON action.circulation FOR EACH ROW EXECUTE PROCEDURE evergreen.fake_fkey_tgr('target_copy'); + +COMMIT; +