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-purse_decision.sql | 37 ++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 11 deletions(-)

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

diff --git a/src/exchangedb/0002-purse_decision.sql b/src/exchangedb/0002-purse_decision.sql
index 2039cd931..f7a82810b 100644
--- a/src/exchangedb/0002-purse_decision.sql
+++ b/src/exchangedb/0002-purse_decision.sql
@@ -15,7 +15,7 @@
 --
 
 
-CREATE OR REPLACE FUNCTION create_table_purse_decision(
+CREATE FUNCTION create_table_purse_decision(
   IN shard_suffix VARCHAR DEFAULT NULL
 )
 RETURNS VOID
@@ -24,10 +24,9 @@ AS $$
 DECLARE
   table_name VARCHAR DEFAULT 'purse_decision';
 BEGIN
-
   PERFORM create_partitioned_table(
-    'CREATE TABLE IF NOT EXISTS %I '
-      '(purse_decision_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' --UNIQUE
+    'CREATE TABLE %I '
+      '(purse_decision_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
       ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
       ',action_timestamp INT8 NOT NULL'
       ',refunded BOOL NOT NULL'
@@ -37,23 +36,34 @@ BEGIN
     ,'PARTITION BY HASH (purse_pub)'
     ,shard_suffix
   );
-
-  table_name = concat_ws('_', table_name, shard_suffix);
-
+  PERFORM comment_partitioned_table(
+     'Purses that were decided upon (refund or merge)'
+    ,table_name
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Public key of the purse'
+    ,'purse_pub'
+    ,table_name
+    ,shard_suffix
+  );
 END
 $$;
 
-CREATE OR REPLACE FUNCTION add_constraints_to_purse_decision_partition(
+CREATE FUNCTION constrain_table_purse_decision(
   IN partition_suffix VARCHAR
 )
 RETURNS VOID
 LANGUAGE plpgsql
 AS $$
+DECLARE
+  table_name VARCHAR DEFAULT 'purse_decision';
 BEGIN
+  table_name = concat_ws('_', table_name, shard_suffix);
   EXECUTE FORMAT (
-    'ALTER TABLE purse_decision_' || partition_suffix || ' '
-      'ADD CONSTRAINT purse_decision_' || partition_suffix || '_purse_action_serial_id_key '
-        'UNIQUE (purse_decision_serial_id) '
+    'ALTER TABLE ' || table_name ||
+    ' ADD CONSTRAINT ' || table_name || '_purse_action_serial_id_key'
+    ' UNIQUE (purse_decision_serial_id) '
   );
 END
 $$;
@@ -70,4 +80,9 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'create'
     ,TRUE
+    ,FALSE),
+    ('purse_decision'
+    ,'exchange-0002'
+    ,'constrain'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

