-- -- This file is part of TALER -- Copyright (C) 2014--2022 Taler Systems SA -- -- TALER is free software; you can redistribute it and/or modify it under the -- terms of the GNU General Public License as published by the Free Software -- Foundation; either version 3, or (at your option) any later version. -- -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License along with -- TALER; see the file COPYING. If not, see -- CREATE OR REPLACE FUNCTION create_table_refunds( IN shard_suffix VARCHAR DEFAULT NULL ) RETURNS VOID LANGUAGE plpgsql AS $$ DECLARE table_name VARCHAR DEFAULT 'refunds'; BEGIN PERFORM create_partitioned_table( 'CREATE TABLE IF NOT EXISTS %I' '(refund_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE' ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)' -- REFERENCES known_coins (coin_pub) ON DELETE CASCADE ',deposit_serial_id INT8 NOT NULL' -- REFERENCES deposits (deposit_serial_id) ON DELETE CASCADE' ',merchant_sig BYTEA NOT NULL CHECK(LENGTH(merchant_sig)=64)' ',rtransaction_id INT8 NOT NULL' ',amount_with_fee_val INT8 NOT NULL' ',amount_with_fee_frac INT4 NOT NULL' -- ,PRIMARY KEY (deposit_serial_id, rtransaction_id) -- done per shard! ') %s ;' ,table_name ,'PARTITION BY HASH (coin_pub)' ,shard_suffix ); table_name = concat_ws('_', table_name, shard_suffix); EXECUTE FORMAT ( 'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_coin_pub_index ' 'ON ' || table_name || ' ' '(coin_pub);' ); END $$; CREATE OR REPLACE FUNCTION constrain0002_table_refunds ( IN partition_suffix VARCHAR DEFAULT NULL ) RETURNS void LANGUAGE plpgsql AS $$ BEGIN EXECUTE FORMAT ( -- FIXME: '_' issue if partition_suffix is NULL -- => solve with general ALTER TABLE helper function! 'ALTER TABLE refunds_' || partition_suffix || ' ' 'ADD CONSTRAINT refunds_' || partition_suffix || '_refund_serial_id_key ' 'UNIQUE (refund_serial_id) ' ',ADD PRIMARY KEY (deposit_serial_id, rtransaction_id) ' ); END $$; INSERT INTO exchange_tables (name ,version ,action ,partitioned ,by_range) VALUES ('refunds' ,'exchange-0002' ,'create' ,TRUE ,FALSE), ('refunds' ,'exchange-0002' ,'constrain0002' ,TRUE ,FALSE);