0002-purse_requests.sql (4701B)
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_requests( 18 IN partition_suffix TEXT DEFAULT NULL 19 ) 20 RETURNS VOID 21 LANGUAGE plpgsql 22 AS $$ 23 DECLARE 24 table_name TEXT DEFAULT 'purse_requests'; 25 BEGIN 26 PERFORM create_partitioned_table( 27 'CREATE TABLE %I ' 28 '(purse_requests_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' 29 ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' 30 ',merge_pub BYTEA NOT NULL CHECK (LENGTH(merge_pub)=32)' 31 ',purse_creation INT8 NOT NULL' 32 ',purse_expiration INT8 NOT NULL' 33 ',h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64)' 34 ',age_limit INT4 NOT NULL' 35 ',flags INT4 NOT NULL' 36 ',in_reserve_quota BOOLEAN NOT NULL DEFAULT(FALSE)' 37 ',was_decided BOOLEAN NOT NULL DEFAULT(FALSE)' 38 ',amount_with_fee taler_amount NOT NULL' 39 ',purse_fee taler_amount NOT NULL' 40 ',balance taler_amount NOT NULL DEFAULT (0,0)' 41 ',purse_sig BYTEA NOT NULL CHECK(LENGTH(purse_sig)=64)' 42 ',PRIMARY KEY (purse_pub)' 43 ') %s ;' 44 ,table_name 45 ,'PARTITION BY HASH (purse_pub)' 46 ,partition_suffix 47 ); 48 PERFORM comment_partitioned_table( 49 'Requests establishing purses, associating them with a contract but without a target reserve' 50 ,table_name 51 ,partition_suffix 52 ); 53 PERFORM comment_partitioned_column( 54 'Public key of the purse' 55 ,'purse_pub' 56 ,table_name 57 ,partition_suffix 58 ); 59 PERFORM comment_partitioned_column( 60 'Local time when the purse was created. Determines applicable purse fees.' 61 ,'purse_creation' 62 ,table_name 63 ,partition_suffix 64 ); 65 PERFORM comment_partitioned_column( 66 'When the purse is set to expire' 67 ,'purse_expiration' 68 ,table_name 69 ,partition_suffix 70 ); 71 PERFORM comment_partitioned_column( 72 'Hash of the contract the parties are to agree to' 73 ,'h_contract_terms' 74 ,table_name 75 ,partition_suffix 76 ); 77 PERFORM comment_partitioned_column( 78 'see the enum TALER_WalletAccountMergeFlags' 79 ,'flags' 80 ,table_name 81 ,partition_suffix 82 ); 83 PERFORM comment_partitioned_column( 84 'set to TRUE if this purse currently counts against the number of free purses in the respective reserve' 85 ,'in_reserve_quota' 86 ,table_name 87 ,partition_suffix 88 ); 89 PERFORM comment_partitioned_column( 90 'Total amount expected to be in the purse' 91 ,'amount_with_fee' 92 ,table_name 93 ,partition_suffix 94 ); 95 PERFORM comment_partitioned_column( 96 'Purse fee the client agreed to pay from the reserve (accepted by the exchange at the time the purse was created). Zero if in_reserve_quota is TRUE.' 97 ,'purse_fee' 98 ,table_name 99 ,partition_suffix 100 ); 101 PERFORM comment_partitioned_column( 102 'Total amount actually in the purse (updated)' 103 ,'balance' 104 ,table_name 105 ,partition_suffix 106 ); 107 PERFORM comment_partitioned_column( 108 'Signature of the purse affirming the purse parameters, of type TALER_SIGNATURE_PURSE_REQUEST' 109 ,'purse_sig' 110 ,table_name 111 ,partition_suffix 112 ); 113 END 114 $$; 115 116 CREATE FUNCTION constrain_table_purse_requests( 117 IN partition_suffix TEXT 118 ) 119 RETURNS VOID 120 LANGUAGE plpgsql 121 AS $$ 122 DECLARE 123 table_name TEXT DEFAULT 'purse_requests'; 124 BEGIN 125 table_name = concat_ws('_', table_name, partition_suffix); 126 127 EXECUTE FORMAT ( 128 'CREATE INDEX ' || table_name || '_merge_pub ' 129 'ON ' || table_name || ' ' 130 '(merge_pub);' 131 ); 132 EXECUTE FORMAT ( 133 'CREATE INDEX ' || table_name || '_purse_expiration ' 134 'ON ' || table_name || ' ' 135 '(purse_expiration) ' || 136 'WHERE NOT was_decided;' 137 ); 138 EXECUTE FORMAT ( 139 'ALTER TABLE ' || table_name || 140 ' ADD CONSTRAINT ' || table_name || '_purse_requests_serial_id_key' 141 ' UNIQUE (purse_requests_serial_id) ' 142 ); 143 END 144 $$; 145 146 147 INSERT INTO exchange_tables 148 (name 149 ,version 150 ,action 151 ,partitioned 152 ,by_range) 153 VALUES 154 ('purse_requests' 155 ,'exchange-0002' 156 ,'create' 157 ,TRUE 158 ,FALSE), 159 ('purse_requests' 160 ,'exchange-0002' 161 ,'constrain' 162 ,TRUE 163 ,FALSE);