create_tables.sql (2373B)
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 PROCEDURE IF EXISTS merchant.sync_instance_procedures(BIGINT); 18 CREATE PROCEDURE merchant.sync_instance_procedures( 19 in_merchant_serial BIGINT 20 ) 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 rec RECORD; 25 r RECORD; 26 my_schema_name TEXT; 27 v_new_def TEXT; 28 BEGIN 29 my_schema_name = format('merchant_instance_%s.', in_merchant_serial); 30 FOR r IN 31 SELECT pg_get_functiondef(p.oid) AS definition 32 FROM pg_proc p 33 JOIN pg_namespace n 34 ON n.oid = p.pronamespace 35 WHERE n.nspname = 'merchant_instances' 36 LOOP 37 v_new_def := replace( 38 r.definition, 39 'merchant_instances.', 40 my_schema_name 41 ); 42 EXECUTE v_new_def; 43 END LOOP; 44 45 FOR r IN 46 SELECT pg_get_triggerdef(t.oid, true) AS trigger_def 47 FROM pg_trigger t 48 JOIN pg_class c 49 ON c.oid = t.tgrelid 50 JOIN pg_namespace n 51 ON n.oid = c.relnamespace 52 WHERE n.nspname = 'merchant_instances' 53 AND NOT t.tgisinternal 54 LOOP 55 v_new_def := replace( 56 r.trigger_def, 57 'merchant_instances.', 58 my_schema_name 59 ); 60 EXECUTE v_new_def; 61 END LOOP; 62 END $$; 63 64 COMMENT ON PROCEDURE merchant.sync_instance_procedures(BIGINT) 65 IS 'Synchronizes procedures and triggers for the given instance by copying the current version from merchant_instances into the per-instance SCHEMA'; 66 67 68 DROP PROCEDURE IF EXISTS merchant.sync_all_instance_procedures(); 69 CREATE PROCEDURE merchant.sync_all_instance_procedures() 70 LANGUAGE plpgsql 71 AS $$ 72 DECLARE 73 merchant_id BIGINT; 74 BEGIN 75 FOR merchant_id IN 76 SELECT merchant_serial 77 FROM merchant.merchant_instances 78 LOOP 79 CALL merchant.sync_instance_procedures 80 (merchant_id); 81 END LOOP; 82 END $$;