From 2eff222c524fa3b5ce2dd4a636aaec8dfb0862c7 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sun, 27 Nov 2022 15:31:39 +0100
Subject: more work on SQL refactoring

---
 src/exchangedb/0002-wads_out.sql | 77 ++++++++++++++++++++++++++++++++++------
 1 file changed, 67 insertions(+), 10 deletions(-)

(limited to 'src/exchangedb/0002-wads_out.sql')

diff --git a/src/exchangedb/0002-wads_out.sql b/src/exchangedb/0002-wads_out.sql
index a44b615e4..d0e8d75fa 100644
--- a/src/exchangedb/0002-wads_out.sql
+++ b/src/exchangedb/0002-wads_out.sql
@@ -14,7 +14,7 @@
 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 --
 
-CREATE OR REPLACE FUNCTION create_table_wads_out(
+CREATE FUNCTION create_table_wads_out(
   IN shard_suffix VARCHAR DEFAULT NULL
 )
 RETURNS VOID
@@ -23,12 +23,11 @@ AS $$
 DECLARE
   table_name VARCHAR DEFAULT 'wads_out';
 BEGIN
-
   PERFORM create_partitioned_table(
-    'CREATE TABLE IF NOT EXISTS %I '
-      '(wad_out_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
+    'CREATE TABLE %I '
+      '(wad_out_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
       ',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
-      ',partner_serial_id INT8 NOT NULL' -- REFERENCES partners(partner_serial_id) ON DELETE CASCADE
+      ',partner_serial_id INT8 NOT NULL'
       ',amount_val INT8 NOT NULL'
       ',amount_frac INT4 NOT NULL'
       ',execution_time INT8 NOT NULL'
@@ -37,21 +36,69 @@ BEGIN
     ,'PARTITION BY HASH (wad_id)'
     ,shard_suffix
   );
-
+  PERFORM comment_partitioned_table(
+     'Wire transfers made to another exchange to transfer purse funds'
+    ,table_name
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Unique identifier of the wad, part of the wire transfer subject'
+    ,'wad_id'
+    ,table_name
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'target exchange of the wad'
+    ,'partner_serial_id'
+    ,table_name
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Amount that was wired'
+    ,'amount_val'
+    ,table_name
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Time when the wire transfer was scheduled'
+    ,'execution_time'
+    ,table_name
+    ,shard_suffix
+  );
 END
 $$;
 
-CREATE OR REPLACE FUNCTION add_constraints_to_wads_out_partition(
+
+CREATE FUNCTION constrain_table_wads_out(
   IN partition_suffix VARCHAR
 )
 RETURNS VOID
 LANGUAGE plpgsql
 AS $$
+DECLARE
+  table_name VARCHAR DEFAULT 'wads_out';
+BEGIN
+  table_name = concat_ws('_', table_name, partition_suffix);
+  EXECUTE FORMAT (
+    'ALTER TABLE ' || table_name ||
+    ' ADD CONSTRAINT ' || table_name || '_wad_out_serial_id_key'
+    ' UNIQUE (wad_out_serial_id) '
+  );
+END
+$$;
+
+
+CREATE FUNCTION foreign_table_wads_out()
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+DECLARE
+  table_name VARCHAR DEFAULT 'wads_out';
 BEGIN
   EXECUTE FORMAT (
-    'ALTER TABLE wads_out_' || partition_suffix || ' '
-      'ADD CONSTRAINT wads_out_' || partition_suffix || '_wad_out_serial_id_key '
-        'UNIQUE (wad_out_serial_id) '
+    'ALTER TABLE ' || table_name ||
+    ' ADD CONSTRAINT ' || table_name || '_foreign_partner'
+    ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
   );
 END
 $$;
@@ -68,4 +115,14 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'create'
     ,TRUE
+    ,FALSE),
+    ('wads_out'
+    ,'exchange-0002'
+    ,'constrain'
+    ,TRUE
+    ,FALSE),
+    ('wads_out'
+    ,'exchange-0002'
+    ,'foreign'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

