From 58a0882909f2b1ede572ae575b83c18746f96cff Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Mon, 13 Jun 2022 15:31:52 +0200
Subject: -towards coin audits with purse deposits

---
 src/exchangedb/common-0001.sql | 70 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

(limited to 'src/exchangedb/common-0001.sql')

diff --git a/src/exchangedb/common-0001.sql b/src/exchangedb/common-0001.sql
index ea3b74ecd..c493af9bd 100644
--- a/src/exchangedb/common-0001.sql
+++ b/src/exchangedb/common-0001.sql
@@ -1219,6 +1219,54 @@ BEGIN
 END
 $$;
 
+
+------------------------------- purse_refunds ----------------------------------------
+
+CREATE OR REPLACE FUNCTION create_table_purse_refunds(
+  IN shard_suffix VARCHAR DEFAULT NULL
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+DECLARE
+  table_name VARCHAR DEFAULT 'purse_refunds';
+BEGIN
+
+  PERFORM create_partitioned_table(
+    'CREATE TABLE IF NOT EXISTS %I '
+      '(purse_refunds_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
+      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
+      ',PRIMARY KEY (purse_pub)'
+    ') %s ;'
+    ,table_name
+    ,'PARTITION BY HASH (purse_pub)'
+    ,shard_suffix
+  );
+
+  table_name = concat_ws('_', table_name, shard_suffix);
+
+END
+$$;
+
+CREATE OR REPLACE FUNCTION add_constraints_to_purse_refunds_partition(
+  IN partition_suffix VARCHAR
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+BEGIN
+  EXECUTE FORMAT (
+    'ALTER TABLE purse_refunds_' || partition_suffix || ' '
+      'ADD CONSTRAINT purse_refunds_' || partition_suffix || '_purse_refunds_serial_id_key '
+        'UNIQUE (purse_refunds_serial_id) '
+  );
+END
+$$;
+
+
+
+
+
 ---------------------------- purse_merges -----------------------------
 
 CREATE OR REPLACE FUNCTION create_table_purse_merges(
@@ -1828,6 +1876,9 @@ BEGIN
   ALTER TABLE IF EXISTS purse_requests
     DETACH partition purse_requests_default;
 
+  ALTER TABLE IF EXISTS purse_refunds
+    DETACH partition purse_refunds_default;
+
   ALTER TABLE IF EXISTS purse_merges
     DETACH partition purse_merges_default;
 
@@ -1894,6 +1945,7 @@ BEGIN
   DROP TABLE IF EXISTS cs_nonce_locks_default;
 
   DROP TABLE IF EXISTS purse_requests_default;
+  DROP TABLE IF EXISTS purse_refunds_default;
   DROP TABLE IF EXISTS purse_merges_default;
   DROP TABLE IF EXISTS account_merges_default;
   DROP TABLE IF EXISTS contracts_default;
@@ -2105,6 +2157,13 @@ BEGIN
     );
     PERFORM add_constraints_to_purse_requests_partition(num_partitions::varchar);
 
+    PERFORM create_hash_partition(
+      'purse_refunds'
+      ,modulus
+      ,num_partitions
+    );
+    PERFORM add_constraints_to_purse_refunds_partition(num_partitions::varchar);
+
     PERFORM create_hash_partition(
       'purse_merges'
       ,modulus
@@ -2316,6 +2375,10 @@ BEGIN
     DROP CONSTRAINT IF EXISTS purse_requests_pkey CASCADE
   ;
 
+  ALTER TABLE IF EXISTS purse_refunds
+    DROP CONSTRAINT IF EXISTS purse_refunds_pkey CASCADE
+  ;
+
   ALTER TABLE IF EXISTS purse_merges
     DROP CONSTRAINT IF EXISTS purse_merges_pkey CASCADE
   ;
@@ -2571,6 +2634,13 @@ BEGIN
     ,current_shard_num
     ,local_user
   );
+  PERFORM create_foreign_hash_partition(
+    'purse_refunds'
+    ,total_num_shards
+    ,shard_suffix
+    ,current_shard_num
+    ,local_user
+  );
   PERFORM create_foreign_hash_partition(
     'purse_merges'
     ,total_num_shards
-- 
cgit v1.2.3

