0002-account_merges.sql (4028B)
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_account_merges( 18 IN partition_suffix TEXT DEFAULT NULL 19 ) 20 RETURNS VOID 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 table_name TEXT DEFAULT 'account_merges'; 25 BEGIN 26 PERFORM create_partitioned_table( 27 'CREATE TABLE IF NOT EXISTS %I ' 28 '(account_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 29 ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)' 30 ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)' 31 ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' 32 ',wallet_h_payto BYTEA NOT NULL CHECK (LENGTH(wallet_h_payto)=32)' 33 ',PRIMARY KEY (purse_pub)' 34 ') %s ;' 35 ,table_name 36 ,'PARTITION BY HASH (purse_pub)' 37 ,partition_suffix 38 ); 39 PERFORM comment_partitioned_table( 40 'Merge requests where a purse- and account-owner requested merging the purse into the account' 41 ,table_name 42 ,partition_suffix 43 ); 44 PERFORM comment_partitioned_column( 45 'public key of the target reserve' 46 ,'reserve_pub' 47 ,table_name 48 ,partition_suffix 49 ); 50 PERFORM comment_partitioned_column( 51 'public key of the purse' 52 ,'purse_pub' 53 ,table_name 54 ,partition_suffix 55 ); 56 PERFORM comment_partitioned_column( 57 'hash over the normalized (!) payto:// URI that identifies the receiving wallet' 58 ,'wallet_h_payto' 59 ,table_name 60 ,partition_suffix 61 ); 62 PERFORM comment_partitioned_column( 63 'signature by the reserve private key affirming the merge, of type TALER_SIGNATURE_WALLET_ACCOUNT_MERGE' 64 ,'reserve_sig' 65 ,table_name 66 ,partition_suffix 67 ); 68 END 69 $$; 70 71 72 CREATE FUNCTION constrain_table_account_merges( 73 IN partition_suffix TEXT 74 ) 75 RETURNS VOID 76 LANGUAGE plpgsql 77 AS $$ 78 DECLARE 79 table_name TEXT DEFAULT 'account_merges'; 80 BEGIN 81 table_name = concat_ws('_', table_name, partition_suffix); 82 83 -- Note: this index *may* be useful in 84 -- pg_get_reserve_history depending on how 85 -- smart the DB is when computing the JOIN. 86 -- Removing it MAY boost performance slightly, at 87 -- the expense of trouble if the "merge_by_reserve" 88 -- query planner goes off the rails. Needs benchmarking 89 -- to be sure. 90 EXECUTE FORMAT ( 91 'CREATE INDEX ' || table_name || '_by_reserve_pub ' 92 'ON ' || table_name || ' ' 93 '(reserve_pub);' 94 ); 95 EXECUTE FORMAT ( 96 'ALTER TABLE ' || table_name || 97 ' ADD CONSTRAINT ' || table_name || '_account_merge_request_serial_id_key' 98 ' UNIQUE (account_merge_request_serial_id) ' 99 ); 100 END 101 $$; 102 103 104 CREATE FUNCTION foreign_table_account_merges() 105 RETURNS VOID 106 LANGUAGE plpgsql 107 AS $$ 108 DECLARE 109 table_name TEXT DEFAULT 'account_merges'; 110 BEGIN 111 EXECUTE FORMAT ( 112 'ALTER TABLE ' || table_name || 113 ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub' 114 ' FOREIGN KEY (reserve_pub) ' 115 ' REFERENCES reserves (reserve_pub) ON DELETE CASCADE' 116 ',ADD CONSTRAINT ' || table_name || '_foreign_purse_pub' 117 ' FOREIGN KEY (purse_pub) ' 118 ' REFERENCES purse_requests (purse_pub)' 119 ); 120 END 121 $$; 122 123 124 INSERT INTO exchange_tables 125 (name 126 ,version 127 ,action 128 ,partitioned 129 ,by_range) 130 VALUES 131 ('account_merges' 132 ,'exchange-0002' 133 ,'create' 134 ,TRUE 135 ,FALSE), 136 ('account_merges' 137 ,'exchange-0002' 138 ,'constrain' 139 ,TRUE 140 ,FALSE), 141 ('account_merges' 142 ,'exchange-0002' 143 ,'foreign' 144 ,TRUE 145 ,FALSE);