From be2c11a1797d8d16b86439a80a4f110f82bb5829 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sun, 27 Nov 2022 02:00:38 +0100
Subject: more sql refactoring

---
 src/exchangedb/0002-wire_targets.sql | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

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

diff --git a/src/exchangedb/0002-wire_targets.sql b/src/exchangedb/0002-wire_targets.sql
index 5e8f19059..08bc468d3 100644
--- a/src/exchangedb/0002-wire_targets.sql
+++ b/src/exchangedb/0002-wire_targets.sql
@@ -23,7 +23,7 @@ AS $$
 BEGIN
   PERFORM create_partitioned_table(
     'CREATE TABLE IF NOT EXISTS %I'
-      '(wire_target_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
+      '(wire_target_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
       ',wire_target_h_payto BYTEA PRIMARY KEY CHECK (LENGTH(wire_target_h_payto)=32)'
       ',payto_uri VARCHAR NOT NULL'
     ') %s ;'
@@ -31,19 +31,34 @@ BEGIN
     ,'PARTITION BY HASH (wire_target_h_payto)'
     ,shard_suffix
   );
+  PERFORM comment_partitioned_table(
+     'All senders and recipients of money via the exchange'
+    ,'wire_targets'
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Can be a regular bank account, or also be a URI identifying a reserve-account (for P2P payments)'
+    ,'payto_uri'
+    ,'wire_targets'
+    ,shard_suffix
+  );
+  PERFORM comment_partitioned_column(
+     'Unsalted hash of payto_uri'
+    ,'wire_target_h_payto'
+    ,shard_suffix
+  );
+
 END
 $$;
 
--- We need a separate function for this, as we call create_table only once but need to add
--- those constraints to each partition which gets created
-CREATE OR REPLACE FUNCTION add_constraints_to_wire_targets_partition(
+
+CREATE OR REPLACE FUNCTION constrain_table_wire_targets(
   IN partition_suffix VARCHAR
 )
 RETURNS void
 LANGUAGE plpgsql
 AS $$
 BEGIN
-
   EXECUTE FORMAT (
     'ALTER TABLE wire_targets_' || partition_suffix || ' '
       'ADD CONSTRAINT wire_targets_' || partition_suffix || '_wire_target_serial_id_key '
@@ -64,4 +79,9 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'create'
     ,TRUE
+    ,FALSE),
+    ('wire_targets'
+    ,'exchange-0002'
+    ,'constrain'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

