summaryrefslogtreecommitdiff
path: root/src/exchangedb/0002-reserves_in.sql
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-11-27 14:05:47 +0100
committerChristian Grothoff <christian@grothoff.org>2022-11-27 14:05:47 +0100
commita322770d290cae69e7d2f7629ee575e068254428 (patch)
tree75a80ac74d165fa0dd00df6095ad0c482d706da5 /src/exchangedb/0002-reserves_in.sql
parentbe2c11a1797d8d16b86439a80a4f110f82bb5829 (diff)
downloadexchange-a322770d290cae69e7d2f7629ee575e068254428.tar.gz
exchange-a322770d290cae69e7d2f7629ee575e068254428.tar.bz2
exchange-a322770d290cae69e7d2f7629ee575e068254428.zip
more work on SQL refactoring
Diffstat (limited to 'src/exchangedb/0002-reserves_in.sql')
-rw-r--r--src/exchangedb/0002-reserves_in.sql81
1 files changed, 54 insertions, 27 deletions
diff --git a/src/exchangedb/0002-reserves_in.sql b/src/exchangedb/0002-reserves_in.sql
index 2ca0ea718..a5ef4dc8e 100644
--- a/src/exchangedb/0002-reserves_in.sql
+++ b/src/exchangedb/0002-reserves_in.sql
@@ -14,8 +14,8 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
-CREATE OR REPLACE FUNCTION create_table_reserves_in(
- IN shard_suffix VARCHAR DEFAULT NULL
+CREATE FUNCTION create_table_reserves_in(
+ IN partition_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
@@ -23,9 +23,8 @@ AS $$
DECLARE
table_name VARCHAR default 'reserves_in';
BEGIN
-
PERFORM create_partitioned_table(
- 'CREATE TABLE IF NOT EXISTS %I'
+ 'CREATE TABLE %I'
'(reserve_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
',reserve_pub BYTEA PRIMARY KEY' -- REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
',wire_reference INT8 NOT NULL'
@@ -37,19 +36,58 @@ BEGIN
') %s ;'
,table_name
,'PARTITION BY HASH (reserve_pub)'
- ,shard_suffix
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_table(
+ 'list of transfers of funds into the reserves, one per incoming wire transfer'
+ ,table_name
+ ,partition_suffix
);
+ PERFORM comment_partitioned_column(
+ 'Identifies the debited bank account and KYC status'
+ ,'wire_source_h_payto'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Public key of the reserve. Private key signifies ownership of the remaining balance.'
+ ,'reserve_pub'
+ ,table_name
+ ,partition_suffix
+ );
+ PERFORM comment_partitioned_column(
+ 'Amount that was transferred into the reserve'
+ ,'credit_val'
+ ,table_name
+ ,partition_suffix
+ );
+END
+$$;
- table_name = concat_ws('_', table_name, shard_suffix);
+CREATE FUNCTION constrain_table_reserves_in(
+ IN partition_suffix VARCHAR
+)
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ table_name VARCHAR default 'reserves_in';
+BEGIN
+ table_name = concat_ws('_', table_name, partition_suffix);
+ EXECUTE FORMAT (
+ 'ALTER TABLE reserves_in_' || partition_suffix || ' '
+ 'ADD CONSTRAINT reserves_in_' || partition_suffix || '_reserve_in_serial_id_key '
+ 'UNIQUE (reserve_in_serial_id)'
+ );
EXECUTE FORMAT (
- 'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_in_serial_id_index '
+ 'CREATE INDEX ' || table_name || '_by_reserve_in_serial_id_index '
'ON ' || table_name || ' '
'(reserve_in_serial_id);'
);
-- FIXME: where do we need this index? Can we do better?
EXECUTE FORMAT (
- 'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_exch_accnt_section_execution_date_idx '
+ 'CREATE INDEX ' || table_name || '_by_exch_accnt_section_execution_date_idx '
'ON ' || table_name || ' '
'(exchange_account_section '
',execution_date'
@@ -57,28 +95,12 @@ BEGIN
);
-- FIXME: where do we need this index? Can we do better?
EXECUTE FORMAT (
- 'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_exch_accnt_reserve_in_serial_id_idx '
+ 'CREATE INDEX ' || table_name || '_by_exch_accnt_reserve_in_serial_id_idx '
'ON ' || table_name || ' '
- '(exchange_account_section,'
- 'reserve_in_serial_id DESC'
+ '(exchange_account_section'
+ ',reserve_in_serial_id DESC'
');'
);
-
-END
-$$;
-
-CREATE OR REPLACE FUNCTION add_constraints_to_reserves_in_partition(
- IN partition_suffix VARCHAR
-)
-RETURNS void
-LANGUAGE plpgsql
-AS $$
-BEGIN
- EXECUTE FORMAT (
- 'ALTER TABLE reserves_in_' || partition_suffix || ' '
- 'ADD CONSTRAINT reserves_in_' || partition_suffix || '_reserve_in_serial_id_key '
- 'UNIQUE (reserve_in_serial_id)'
- );
END
$$;
@@ -94,4 +116,9 @@ INSERT INTO exchange_tables
,'exchange-0002'
,'create'
,TRUE
+ ,FALSE),
+ ('reserves_in'
+ ,'exchange-0002'
+ ,'constrain'
+ ,TRUE
,FALSE);