From 4f75bcdca35b1ce8aa1f3db444c63f4763e28301 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sun, 27 Nov 2022 14:45:01 +0100
Subject: more work on SQL refactoring

---
 src/exchangedb/0002-prewire.sql | 55 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 9 deletions(-)

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

diff --git a/src/exchangedb/0002-prewire.sql b/src/exchangedb/0002-prewire.sql
index e26475c12..fb8dc2212 100644
--- a/src/exchangedb/0002-prewire.sql
+++ b/src/exchangedb/0002-prewire.sql
@@ -14,8 +14,8 @@
 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 --
 
-CREATE OR REPLACE FUNCTION create_table_prewire(
-  IN shard_suffix VARCHAR DEFAULT NULL
+CREATE FUNCTION create_table_prewire(
+  IN partition_suffix VARCHAR DEFAULT NULL
 )
 RETURNS VOID
 LANGUAGE plpgsql
@@ -23,9 +23,8 @@ AS $$
 DECLARE
   table_name VARCHAR DEFAULT 'prewire';
 BEGIN
-
   PERFORM create_partitioned_table(
-    'CREATE TABLE IF NOT EXISTS %I'
+    'CREATE TABLE %I'
       '(prewire_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
       ',wire_method TEXT NOT NULL'
       ',finished BOOLEAN NOT NULL DEFAULT false'
@@ -34,13 +33,47 @@ BEGIN
     ') %s ;'
     ,table_name
     ,'PARTITION BY HASH (prewire_uuid)'
-    ,shard_suffix
+    ,partition_suffix
+  );
+  PERFORM comment_partitioned_table(
+     'pre-commit data for wire transfers we are about to execute'
+    ,table_name
+    ,partition_suffix
   );
+  PERFORM comment_partitioned_column(
+     'set to TRUE if the bank responded with a non-transient failure to our transfer request'
+    ,'failed'
+    ,table_name
+    ,partition_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'set to TRUE once bank confirmed receiving the wire transfer request'
+    ,'finished'
+    ,table_name
+    ,partition_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'serialized data to send to the bank to execute the wire transfer'
+    ,'buf'
+    ,table_name
+    ,partition_suffix
+  );
+END
+$$;
 
-  table_name = concat_ws('_', table_name, shard_suffix);
 
+CREATE FUNCTION constrain_table_prewire(
+  IN partition_suffix VARCHAR DEFAULT NULL
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+DECLARE
+  table_name VARCHAR DEFAULT 'prewire';
+BEGIN
+  table_name = concat_ws('_', table_name, partition_suffix);
   EXECUTE FORMAT (
-    'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_finished_index '
+    'CREATE INDEX ' || table_name || '_by_finished_index '
     'ON ' || table_name || ' '
     '(finished);'
   );
@@ -50,7 +83,7 @@ BEGIN
   );
   -- FIXME: find a way to combine these two indices?
   EXECUTE FORMAT (
-    'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_failed_finished_index '
+    'CREATE INDEX ' || table_name || '_by_failed_finished_index '
     'ON ' || table_name || ' '
     '(failed,finished);'
   );
@@ -58,7 +91,6 @@ BEGIN
     'COMMENT ON INDEX ' || table_name || '_by_failed_finished_index '
     'IS ' || quote_literal('for wire_prepare_data_get') || ';'
   );
-
 END
 $$;
 
@@ -74,4 +106,9 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'create'
     ,TRUE
+    ,FALSE),
+    ('prewire'
+    ,'exchange-0002'
+    ,'constrain'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

