aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/0002-wad_out_entries.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb/0002-wad_out_entries.sql')
-rw-r--r--src/exchangedb/0002-wad_out_entries.sql157
1 files changed, 127 insertions, 30 deletions
diff --git a/src/exchangedb/0002-wad_out_entries.sql b/src/exchangedb/0002-wad_out_entries.sql
index 3f1076b25..1db151563 100644
--- a/src/exchangedb/0002-wad_out_entries.sql
+++ b/src/exchangedb/0002-wad_out_entries.sql
@@ -15,8 +15,8 @@
--
-CREATE OR REPLACE FUNCTION create_table_wad_out_entries(
- IN shard_suffix VARCHAR DEFAULT NULL
+CREATE FUNCTION create_table_wad_out_entries(
+ IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@@ -24,53 +24,140 @@ AS $$
DECLARE
table_name VARCHAR DEFAULT 'wad_out_entries';
BEGIN
-
PERFORM create_partitioned_table(
- 'CREATE TABLE IF NOT EXISTS %I '
- '(wad_out_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
- ',wad_out_serial_id INT8' -- REFERENCES wads_out (wad_out_serial_id) ON DELETE CASCADE
- ',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
- ',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
- ',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
- ',purse_expiration INT8 NOT NULL'
- ',merge_timestamp INT8 NOT NULL'
- ',amount_with_fee_val INT8 NOT NULL'
- ',amount_with_fee_frac INT4 NOT NULL'
- ',wad_fee_val INT8 NOT NULL'
- ',wad_fee_frac INT4 NOT NULL'
- ',deposit_fees_val INT8 NOT NULL'
- ',deposit_fees_frac INT4 NOT NULL'
- ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
- ',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
- ') %s ;'
+ 'CREATE TABLE %I '
+ '(wad_out_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
+ ',wad_out_serial_id INT8'
+ ',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
+ ',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
+ ',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
+ ',purse_expiration INT8 NOT NULL'
+ ',merge_timestamp INT8 NOT NULL'
+ ',amount_with_fee_val INT8 NOT NULL'
+ ',amount_with_fee_frac INT4 NOT NULL'
+ ',wad_fee_val INT8 NOT NULL'
+ ',wad_fee_frac INT4 NOT NULL'
+ ',deposit_fees_val INT8 NOT NULL'
+ ',deposit_fees_frac INT4 NOT NULL'
+ ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
+ ',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
+ ') %s ;'
,table_name
,'PARTITION BY HASH (purse_pub)'
- ,shard_suffix
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_table(
+ 'Purses combined into a wad'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Wad the purse was part of'
+ ,'wad_out_serial_id'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Target reserve for the purse'
+ ,'reserve_pub'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Public key of the purse'
+ ,'purse_pub'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Hash of the contract associated with the purse'
+ ,'h_contract'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Time when the purse expires'
+ ,'purse_expiration'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Time when the merge was approved'
+ ,'merge_timestamp'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Total amount in the purse'
+ ,'amount_with_fee_val'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Wad fee charged to the purse'
+ ,'wad_fee_val'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Total deposit fees charged to the purse'
+ ,'deposit_fees_val'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE'
+ ,'reserve_sig'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE'
+ ,'purse_sig'
+ ,table_name
+ ,partition_suffix
);
+END
+$$;
- table_name = concat_ws('_', table_name, shard_suffix);
+
+CREATE FUNCTION constrain_table_wad_out_entries(
+ IN partition_suffix VARCHAR
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ table_name VARCHAR DEFAULT 'wad_out_entries';
+BEGIN
+ table_name = concat_ws('_', table_name, partition_suffix);
-- FIXME: change to materialized index by reserve_pub!
EXECUTE FORMAT (
- 'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_pub '
+ 'CREATE INDEX ' || table_name || '_by_reserve_pub '
'ON ' || table_name || ' '
'(reserve_pub);'
);
-
+ EXECUTE FORMAT (
+ 'ALTER TABLE ' || table_name ||
+ ' ADD CONSTRAINT ' || table_name || '_wad_out_entry_serial_id_key'
+ ' UNIQUE (wad_out_entry_serial_id) '
+ );
END
$$;
-CREATE OR REPLACE FUNCTION add_constraints_to_wad_out_entries_partition(
- IN partition_suffix VARCHAR
-)
+
+CREATE FUNCTION foreign_table_wad_out_entries()
RETURNS VOID
LANGUAGE plpgsql
AS $$
+DECLARE
+ table_name VARCHAR DEFAULT 'wad_out_entries';
BEGIN
EXECUTE FORMAT (
- 'ALTER TABLE wad_out_entries_' || partition_suffix || ' '
- 'ADD CONSTRAINT wad_out_entries_' || partition_suffix || '_wad_out_entry_serial_id_key '
- 'UNIQUE (wad_out_entry_serial_id) '
+ 'ALTER TABLE ' || table_name ||
+ ' ADD CONSTRAINT ' || table_name || '_foreign_wad_out'
+ ' REFERENCES wads_out (wad_out_serial_id) ON DELETE CASCADE'
);
END
$$;
@@ -87,4 +174,14 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
+ ,FALSE),
+ ('wad_out_entries'
+ ,'exchange-0002'
+ ,'constrain'
+ ,TRUE
+ ,FALSE),
+ ('wad_out_entries'
+ ,'exchange-0002'
+ ,'foreign'
+ ,TRUE
,FALSE);