0002-wads_in.sql (2857B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2014--2022 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_wads_in( 18 IN partition_suffix TEXT DEFAULT NULL 19 ) 20 RETURNS VOID 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 table_name TEXT DEFAULT 'wads_in'; 25 BEGIN 26 PERFORM create_partitioned_table( 27 'CREATE TABLE %I ' 28 '(wad_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 29 ',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)' 30 ',origin_exchange_url TEXT NOT NULL' 31 ',amount taler_amount NOT NULL' 32 ',arrival_time INT8 NOT NULL' 33 ',UNIQUE (wad_id, origin_exchange_url)' 34 ') %s ;' 35 ,table_name 36 ,'PARTITION BY HASH (wad_id)' 37 ,partition_suffix 38 ); 39 PERFORM comment_partitioned_table( 40 'Incoming exchange-to-exchange wad wire transfers' 41 ,table_name 42 ,partition_suffix 43 ); 44 PERFORM comment_partitioned_column( 45 'Unique identifier of the wad, part of the wire transfer subject' 46 ,'wad_id' 47 ,table_name 48 ,partition_suffix 49 ); 50 PERFORM comment_partitioned_column( 51 'Base URL of the originating URL, also part of the wire transfer subject' 52 ,'origin_exchange_url' 53 ,table_name 54 ,partition_suffix 55 ); 56 PERFORM comment_partitioned_column( 57 'Actual amount that was received by our exchange' 58 ,'amount' 59 ,table_name 60 ,partition_suffix 61 ); 62 PERFORM comment_partitioned_column( 63 'Time when the wad was received' 64 ,'arrival_time' 65 ,table_name 66 ,partition_suffix 67 ); 68 END $$; 69 70 71 CREATE FUNCTION constrain_table_wads_in( 72 IN partition_suffix TEXT 73 ) 74 RETURNS VOID 75 LANGUAGE plpgsql 76 AS $$ 77 DECLARE 78 table_name TEXT DEFAULT 'wads_in'; 79 BEGIN 80 table_name = concat_ws('_', table_name, partition_suffix); 81 EXECUTE FORMAT ( 82 'ALTER TABLE ' || table_name || 83 ' ADD CONSTRAINT ' || table_name || '_wad_in_serial_id_key' 84 ' UNIQUE (wad_in_serial_id) ' 85 ',ADD CONSTRAINT ' || table_name || '_wad_is_origin_exchange_url_key' 86 ' UNIQUE (wad_id, origin_exchange_url) ' 87 ); 88 END $$; 89 90 91 INSERT INTO exchange_tables 92 (name 93 ,version 94 ,action 95 ,partitioned 96 ,by_range) 97 VALUES 98 ('wads_in' 99 ,'exchange-0002' 100 ,'create' 101 ,TRUE 102 ,FALSE), 103 ('wads_in' 104 ,'exchange-0002' 105 ,'constrain' 106 ,TRUE 107 ,FALSE);