pg_create_instance_trigger.sql (2262B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2026 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 DROP FUNCTION IF EXISTS merchant.merchant_instances_after_insert_trigger() CASCADE; 18 CREATE FUNCTION merchant.merchant_instances_after_insert_trigger() 19 RETURNS trigger 20 LANGUAGE plpgsql 21 AS $$ 22 BEGIN 23 PERFORM merchant.create_instance_schema(NEW.merchant_serial); 24 RETURN NEW; 25 END $$; 26 COMMENT ON FUNCTION merchant.merchant_instances_after_insert_trigger() 27 IS 'Builds the per-instance schema merchant_instance_<merchant_serial>' 28 ' for the newly inserted row.'; 29 30 DROP TRIGGER IF EXISTS merchant_instances_on_insert 31 ON merchant.merchant_instances; 32 CREATE TRIGGER merchant_instances_on_insert 33 AFTER INSERT 34 ON merchant.merchant_instances 35 FOR EACH ROW 36 EXECUTE FUNCTION merchant.merchant_instances_after_insert_trigger(); 37 38 39 DROP FUNCTION IF EXISTS merchant.merchant_instances_after_delete_trigger() CASCADE; 40 CREATE FUNCTION merchant.merchant_instances_after_delete_trigger() 41 RETURNS trigger 42 LANGUAGE plpgsql 43 AS $$ 44 BEGIN 45 EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', 46 'merchant_instance_' || OLD.merchant_serial::TEXT); 47 RETURN NULL; 48 END $$; 49 COMMENT ON FUNCTION merchant.merchant_instances_after_delete_trigger() 50 IS 'Drops the per-instance schema merchant_instance_<merchant_serial>' 51 ' when its row in merchant.merchant_instances is removed.'; 52 53 DROP TRIGGER IF EXISTS merchant_instances_on_delete 54 ON merchant.merchant_instances; 55 CREATE TRIGGER merchant_instances_on_delete 56 AFTER DELETE ON merchant.merchant_instances 57 FOR EACH ROW 58 EXECUTE FUNCTION merchant.merchant_instances_after_delete_trigger();