0002-prewire.sql (3081B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--2023 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 CREATE FUNCTION create_table_prewire( 18 IN partition_suffix TEXT DEFAULT NULL 19 ) 20 RETURNS VOID 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 table_name TEXT DEFAULT 'prewire'; 25 BEGIN 26 PERFORM create_partitioned_table( 27 'CREATE TABLE %I' 28 '(prewire_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY' 29 ',wire_method TEXT NOT NULL' 30 ',finished BOOLEAN NOT NULL DEFAULT FALSE' 31 ',failed BOOLEAN NOT NULL DEFAULT FALSE' 32 ',buf BYTEA NOT NULL' 33 ') %s ;' 34 ,table_name 35 ,'PARTITION BY HASH (prewire_uuid)' 36 ,partition_suffix 37 ); 38 PERFORM comment_partitioned_table( 39 'pre-commit data for wire transfers we are about to execute' 40 ,table_name 41 ,partition_suffix 42 ); 43 PERFORM comment_partitioned_column( 44 'set to TRUE if the bank responded with a non-transient failure to our transfer request' 45 ,'failed' 46 ,table_name 47 ,partition_suffix 48 ); 49 PERFORM comment_partitioned_column( 50 'set to TRUE once bank confirmed receiving the wire transfer request' 51 ,'finished' 52 ,table_name 53 ,partition_suffix 54 ); 55 PERFORM comment_partitioned_column( 56 'serialized data to send to the bank to execute the wire transfer' 57 ,'buf' 58 ,table_name 59 ,partition_suffix 60 ); 61 END 62 $$; 63 64 65 CREATE FUNCTION constrain_table_prewire( 66 IN partition_suffix TEXT DEFAULT NULL 67 ) 68 RETURNS VOID 69 LANGUAGE plpgsql 70 AS $$ 71 DECLARE 72 table_name TEXT DEFAULT 'prewire'; 73 BEGIN 74 table_name = concat_ws('_', table_name, partition_suffix); 75 EXECUTE FORMAT ( 76 'CREATE INDEX ' || table_name || '_by_finished_index ' 77 'ON ' || table_name || ' ' 78 '(finished)' 79 ' WHERE finished;' 80 ); 81 EXECUTE FORMAT ( 82 'COMMENT ON INDEX ' || table_name || '_by_finished_index ' 83 'IS ' || quote_literal('for do_gc') || ';' 84 ); 85 EXECUTE FORMAT ( 86 'CREATE INDEX ' || table_name || '_by_failed_finished_index ' 87 'ON ' || table_name || ' ' 88 '(prewire_uuid)' 89 ' WHERE finished=FALSE' 90 ' AND failed=FALSE;' 91 ); 92 EXECUTE FORMAT ( 93 'COMMENT ON INDEX ' || table_name || '_by_failed_finished_index ' 94 'IS ' || quote_literal('for wire_prepare_data_get') || ';' 95 ); 96 END 97 $$; 98 99 100 INSERT INTO exchange_tables 101 (name 102 ,version 103 ,action 104 ,partitioned 105 ,by_range) 106 VALUES 107 ('prewire' 108 ,'exchange-0002' 109 ,'create' 110 ,TRUE 111 ,FALSE), 112 ('prewire' 113 ,'exchange-0002' 114 ,'constrain' 115 ,TRUE 116 ,FALSE);