From 4f75bcdca35b1ce8aa1f3db444c63f4763e28301 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 27 Nov 2022 14:45:01 +0100 Subject: more work on SQL refactoring --- src/exchangedb/0002-purse_requests.sql | 110 ++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 21 deletions(-) (limited to 'src/exchangedb/0002-purse_requests.sql') diff --git a/src/exchangedb/0002-purse_requests.sql b/src/exchangedb/0002-purse_requests.sql index 9f0aef067..666546346 100644 --- a/src/exchangedb/0002-purse_requests.sql +++ b/src/exchangedb/0002-purse_requests.sql @@ -14,7 +14,7 @@ -- TALER; see the file COPYING. If not, see -- -CREATE OR REPLACE FUNCTION create_table_purse_requests( +CREATE FUNCTION create_table_purse_requests( IN shard_suffix VARCHAR DEFAULT NULL ) RETURNS VOID @@ -23,10 +23,9 @@ AS $$ DECLARE table_name VARCHAR DEFAULT 'purse_requests'; BEGIN - PERFORM create_partitioned_table( - 'CREATE TABLE IF NOT EXISTS %I ' - '(purse_requests_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE + 'CREATE TABLE %I ' + '(purse_requests_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)' ',merge_pub BYTEA NOT NULL CHECK (LENGTH(merge_pub)=32)' ',purse_creation INT8 NOT NULL' @@ -48,38 +47,102 @@ BEGIN ,'PARTITION BY HASH (purse_pub)' ,shard_suffix ); + PERFORM comment_partitioned_table( + 'Requests establishing purses, associating them with a contract but without a target reserve' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Public key of the purse' + ,'purse_pub' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Local time when the purse was created. Determines applicable purse fees.' + ,'purse_creation' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'When the purse is set to expire' + ,'purse_expiration' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Hash of the contract the parties are to agree to' + ,'h_contract_terms' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'see the enum TALER_WalletAccountMergeFlags' + ,'flags' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'set to TRUE if this purse currently counts against the number of free purses in the respective reserve' + ,'in_reserve_quota' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Total amount expected to be in the purse' + ,'amount_with_fee_val' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + '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.' + ,'purse_fee_val' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Total amount actually in the purse (updated)' + ,'balance_val' + ,table_name + ,shard_suffix + ); + PERFORM comment_partitioned_column( + 'Signature of the purse affirming the purse parameters, of type TALER_SIGNATURE_PURSE_REQUEST' + ,'purse_sig' + ,table_name + ,shard_suffix + ); +END +$$; +CREATE FUNCTION constrain_table_purse_requests( + IN partition_suffix VARCHAR +) +RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + table_name VARCHAR DEFAULT 'purse_requests'; +BEGIN table_name = concat_ws('_', table_name, shard_suffix); -- FIXME: change to materialized index by merge_pub! EXECUTE FORMAT ( - 'CREATE INDEX IF NOT EXISTS ' || table_name || '_merge_pub ' + 'CREATE INDEX ' || table_name || '_merge_pub ' 'ON ' || table_name || ' ' '(merge_pub);' ); - -- FIXME: drop index on master (crosses shards)? -- Or use materialized index? (needed?) EXECUTE FORMAT ( - 'CREATE INDEX IF NOT EXISTS ' || table_name || '_purse_expiration ' + 'CREATE INDEX ' || table_name || '_purse_expiration ' 'ON ' || table_name || ' ' '(purse_expiration);' ); - -END -$$; - -CREATE OR REPLACE FUNCTION add_constraints_to_purse_requests_partition( - IN partition_suffix VARCHAR -) -RETURNS VOID -LANGUAGE plpgsql -AS $$ -BEGIN EXECUTE FORMAT ( - 'ALTER TABLE purse_requests_' || partition_suffix || ' ' - 'ADD CONSTRAINT purse_requests_' || partition_suffix || '_purse_requests_serial_id_key ' - 'UNIQUE (purse_requests_serial_id) ' + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_purse_requests_serial_id_key' + ' UNIQUE (purse_requests_serial_id) ' ); END $$; @@ -96,4 +159,9 @@ INSERT INTO exchange_tables ,'exchange-0002' ,'create' ,TRUE + ,FALSE), + ('purse_requests' + ,'exchange-0002' + ,'constrain' + ,TRUE ,FALSE); -- cgit v1.2.3