From 2edbe8cc53a9050283963650b4d1a5b0c5994243 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 13 Sep 2010 17:26:03 +0000 Subject: [PATCH] we need to move permissions around for a Great Renumbering, but i18n is not protected by fkeys. this provides infratructure for maintaining i18n values that already exist in a db, and an example usage on permission.perm_list git-svn-id: svn://svn.open-ils.org/ILS/trunk@17634 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/sql/Pg/002.schema.config.sql | 30 ++++++++++++++++- Open-ILS/src/sql/Pg/006.schema.permissions.sql | 3 ++ .../0393.schema.perm-id-change-i18n-protection.sql | 38 ++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0393.schema.perm-id-change-i18n-protection.sql diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 5c2e08588..a376efc58 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -68,7 +68,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0392'); -- phasefx +INSERT INTO config.upgrade_log (version) VALUES ('0393'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, @@ -647,6 +647,34 @@ CREATE TABLE config.i18n_core ( CREATE UNIQUE INDEX i18n_identity ON config.i18n_core (fq_field,identity_value,translation); +CREATE OR REPLACE FUNCTION oils_i18n_update_apply(old_ident TEXT, new_ident TEXT, hint TEXT) RETURNS VOID AS $_$ +BEGIN + + EXECUTE $$ + UPDATE config.i18n_core + SET identity_value = $$ || quote_literal(new_ident) || $$ + WHERE fq_field LIKE '$$ || hint || $$.%' + AND identity_value = $$ || quote_literal(old_ident) || $$::TEXT;$$; + + RETURN; + +END; +$_$ LANGUAGE PLPGSQL; + +CREATE OR REPLACE FUNCTION oils_i18n_id_tracking(/* hint */) RETURNS TRIGGER AS $_$ +BEGIN + PERFORM oils_i18n_update_apply( OLD.id::TEXT, NEW.id::TEXT, TG_ARGV[0]::TEXT ); + RETURN NEW; +END; +$_$ LANGUAGE PLPGSQL; + +CREATE OR REPLACE FUNCTION oils_i18n_code_tracking(/* hint */) RETURNS TRIGGER AS $_$ +BEGIN + PERFORM oils_i18n_update_apply( OLD.code::TEXT, NEW.code::TEXT, TG_ARGV[0]::TEXT ); + RETURN NEW; +END; +$_$ LANGUAGE PLPGSQL; + CREATE TABLE config.billing_type ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, diff --git a/Open-ILS/src/sql/Pg/006.schema.permissions.sql b/Open-ILS/src/sql/Pg/006.schema.permissions.sql index e870cfe44..c734d94c6 100644 --- a/Open-ILS/src/sql/Pg/006.schema.permissions.sql +++ b/Open-ILS/src/sql/Pg/006.schema.permissions.sql @@ -27,6 +27,9 @@ CREATE TABLE permission.perm_list ( description TEXT ); CREATE INDEX perm_list_code_idx ON permission.perm_list (code); +CREATE TRIGGER maintain_perm_i18n_tgr + AFTER UPDATE ON permission.perm_list + FOR EACH ROW EXECUTE PROCEDURE oils_i18n_id_tracking('ppl'); CREATE TABLE permission.grp_tree ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/upgrade/0393.schema.perm-id-change-i18n-protection.sql b/Open-ILS/src/sql/Pg/upgrade/0393.schema.perm-id-change-i18n-protection.sql new file mode 100644 index 000000000..06ac6482d --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0393.schema.perm-id-change-i18n-protection.sql @@ -0,0 +1,38 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0393'); -- miker + +CREATE OR REPLACE FUNCTION oils_i18n_update_apply(old_ident TEXT, new_ident TEXT, hint TEXT) RETURNS VOID AS $_$ +BEGIN + + EXECUTE $$ + UPDATE config.i18n_core + SET identity_value = $$ || new_ident || $$ + WHERE fq_field LIKE '$$ || hint || $$.%' + AND identity_value = $$ || old_ident || $$;$$; + + RETURN; + +END; +$_$ LANGUAGE PLPGSQL; + +CREATE OR REPLACE FUNCTION oils_i18n_id_tracking(/* hint */) RETURNS TRIGGER AS $_$ +BEGIN + PERFORM oils_i18n_update_apply( OLD.id::TEXT, NEW.id::TEXT, TG_ARGV[0]::TEXT ); + RETURN NEW; +END; +$_$ LANGUAGE PLPGSQL; + +CREATE OR REPLACE FUNCTION oils_i18n_code_tracking(/* hint */) RETURNS TRIGGER AS $_$ +BEGIN + PERFORM oils_i18n_update_apply( OLD.code::TEXT, NEW.code::TEXT, TG_ARGV[0]::TEXT ); + RETURN NEW; +END; +$_$ LANGUAGE PLPGSQL; + + +CREATE TRIGGER maintain_perm_i18n_tgr + AFTER UPDATE ON permission.perm_list + FOR EACH ROW EXECUTE PROCEDURE oils_i18n_tracking('ppl', 'id'); + +COMMIT; -- 2.11.0