0002-purse_merges.sql (3697B)
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_purse_merges( 18 IN partition_suffix TEXT DEFAULT NULL 19 ) 20 RETURNS VOID 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 table_name TEXT DEFAULT 'purse_merges'; 25 BEGIN 26 PERFORM create_partitioned_table( 27 'CREATE TABLE %I ' 28 '(purse_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 29 ',partner_serial_id INT8' 30 ',reserve_pub BYTEA NOT NULL CHECK(length(reserve_pub)=32)' 31 ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' 32 ',merge_sig BYTEA NOT NULL CHECK (LENGTH(merge_sig)=64)' 33 ',merge_timestamp INT8 NOT NULL' 34 ',PRIMARY KEY (purse_pub)' 35 ') %s ;' 36 ,table_name 37 ,'PARTITION BY HASH (purse_pub)' 38 ,partition_suffix 39 ); 40 PERFORM comment_partitioned_table( 41 'Merge requests where a purse-owner requested merging the purse into the account' 42 ,table_name 43 ,partition_suffix 44 ); 45 PERFORM comment_partitioned_column( 46 'identifies the partner exchange, NULL in case the target reserve lives at this exchange' 47 ,'partner_serial_id' 48 ,table_name 49 ,partition_suffix 50 ); 51 PERFORM comment_partitioned_column( 52 'public key of the target reserve' 53 ,'reserve_pub' 54 ,table_name 55 ,partition_suffix 56 ); 57 PERFORM comment_partitioned_column( 58 'public key of the purse' 59 ,'purse_pub' 60 ,table_name 61 ,partition_suffix 62 ); 63 PERFORM comment_partitioned_column( 64 'signature by the purse private key affirming the merge, of type TALER_SIGNATURE_WALLET_PURSE_MERGE' 65 ,'merge_sig' 66 ,table_name 67 ,partition_suffix 68 ); 69 PERFORM comment_partitioned_column( 70 'when was the merge message signed' 71 ,'merge_timestamp' 72 ,table_name 73 ,partition_suffix 74 ); 75 END 76 $$; 77 78 79 CREATE FUNCTION constrain_table_purse_merges( 80 IN partition_suffix TEXT 81 ) 82 RETURNS VOID 83 LANGUAGE plpgsql 84 AS $$ 85 DECLARE 86 table_name TEXT DEFAULT 'purse_merges'; 87 BEGIN 88 table_name = concat_ws('_', table_name, partition_suffix); 89 90 EXECUTE FORMAT ( 91 'ALTER TABLE ' || table_name || 92 ' ADD CONSTRAINT ' || table_name || '_purse_merge_request_serial_id_key' 93 ' UNIQUE (purse_merge_request_serial_id) ' 94 ); 95 END 96 $$; 97 98 99 CREATE FUNCTION foreign_table_purse_merges() 100 RETURNS VOID 101 LANGUAGE plpgsql 102 AS $$ 103 DECLARE 104 table_name TEXT DEFAULT 'purse_merges'; 105 BEGIN 106 EXECUTE FORMAT ( 107 'ALTER TABLE ' || table_name || 108 ' ADD CONSTRAINT ' || table_name || '_foreign_partner_serial_id' 109 ' FOREIGN KEY (partner_serial_id) ' 110 ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE' 111 ',ADD CONSTRAINT ' || table_name || '_foreign_purse_pub' 112 ' FOREIGN KEY (purse_pub) ' 113 ' REFERENCES purse_requests (purse_pub) ON DELETE CASCADE' 114 ); 115 END 116 $$; 117 118 119 INSERT INTO exchange_tables 120 (name 121 ,version 122 ,action 123 ,partitioned 124 ,by_range) 125 VALUES 126 ('purse_merges' 127 ,'exchange-0002' 128 ,'create' 129 ,TRUE 130 ,FALSE), 131 ('purse_merges' 132 ,'exchange-0002' 133 ,'constrain' 134 ,TRUE 135 ,FALSE), 136 ('purse_merges' 137 ,'exchange-0002' 138 ,'foreign' 139 ,TRUE 140 ,FALSE);