From a046899b2ccd452f786051a41bc385cfb574bbcb Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sat, 20 Aug 2022 21:29:29 +0200
Subject: -major KYC update, fixes misc. issues

---
 src/exchangedb/common-0001.sql | 66 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 8 deletions(-)

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

diff --git a/src/exchangedb/common-0001.sql b/src/exchangedb/common-0001.sql
index 58c15d0b..6dc661fd 100644
--- a/src/exchangedb/common-0001.sql
+++ b/src/exchangedb/common-0001.sql
@@ -84,9 +84,9 @@ END
 $$;
 
 
------------------------ legitimizations ---------------------------
+----------------------- legitimization_processes ---------------------------
 
-CREATE OR REPLACE FUNCTION create_table_legitimizations(
+CREATE OR REPLACE FUNCTION create_table_legitimization_processes(
   IN shard_suffix VARCHAR DEFAULT NULL
 )
 RETURNS VOID
@@ -96,7 +96,7 @@ BEGIN
 
   PERFORM create_partitioned_table(
     'CREATE TABLE IF NOT EXISTS %I'
-      '(legitimization_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
+      '(legitimization_process_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
       ',h_payto BYTEA NOT NULL CHECK (LENGTH(h_payto)=32)'
       ',expiration_time INT8 NOT NULL DEFAULT (0)'
       ',provider_section VARCHAR NOT NULL'
@@ -104,7 +104,7 @@ BEGIN
       ',provider_legitimization_id VARCHAR DEFAULT NULL'
       ',UNIQUE (h_payto, provider_section)'
     ') %s ;'
-    ,'legitimizations'
+    ,'legitimization_processes'
     ,'PARTITION BY HASH (h_payto)'
     ,shard_suffix
   );
@@ -114,7 +114,7 @@ $$;
 
 -- 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_legitimizations_partition(
+CREATE OR REPLACE FUNCTION add_constraints_to_legitimization_processes_partition(
   IN partition_suffix VARCHAR
 )
 RETURNS void
@@ -124,13 +124,13 @@ DECLARE
   partition_name VARCHAR;
 BEGIN
 
-  partition_name = concat_ws('_', 'legitimizations', partition_suffix);
+  partition_name = concat_ws('_', 'legitimization_processes', partition_suffix);
 
   EXECUTE FORMAT (
     'ALTER TABLE ' || partition_name
     || ' '
-      'ADD CONSTRAINT ' || partition_name || '_legitimization_serial_id_key '
-        'UNIQUE (legitimization_serial_id)');
+      'ADD CONSTRAINT ' || partition_name || '_serial_key '
+        'UNIQUE (legitimization_process_serial_id)');
   EXECUTE FORMAT (
     'CREATE INDEX IF NOT EXISTS ' || partition_name || '_by_provider_and_legi_index '
         'ON '|| partition_name || ' '
@@ -143,6 +143,55 @@ BEGIN
 END
 $$;
 
+
+----------------------- legitimization_requirements ---------------------------
+
+CREATE OR REPLACE FUNCTION create_table_legitimization_requirements(
+  IN shard_suffix VARCHAR DEFAULT NULL
+)
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+BEGIN
+
+  PERFORM create_partitioned_table(
+    'CREATE TABLE IF NOT EXISTS %I'
+      '(legitimization_requirement_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
+      ',h_payto BYTEA NOT NULL CHECK (LENGTH(h_payto)=32)'
+      ',required_checks VARCHAR NOT NULL'
+      ',UNIQUE (h_payto, required_checks)'
+    ') %s ;'
+    ,'legitimization_requirements'
+    ,'PARTITION BY HASH (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_legitimization_requirements_partition(
+  IN partition_suffix VARCHAR
+)
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+DECLARE
+  partition_name VARCHAR;
+BEGIN
+
+  partition_name = concat_ws('_', 'legitimization_requirements', partition_suffix);
+
+  EXECUTE FORMAT (
+    'ALTER TABLE ' || partition_name
+    || ' '
+      'ADD CONSTRAINT ' || partition_name || '_serial_id_key '
+        'UNIQUE (legitimization_requirement_serial_id)');
+END
+$$;
+
+
 ------------------------ reserves -------------------------------
 
 CREATE OR REPLACE FUNCTION create_table_reserves(
@@ -895,6 +944,7 @@ BEGIN
       ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
       ',merchant_pub BYTEA CHECK (LENGTH(merchant_pub)=32)'
       ',exchange_account_section TEXT NOT NULL'
+      ',legitimization_requirement_serial_id INT8 NOT NULL DEFAULT(0)'
       ',wtid_raw BYTEA NOT NULL CHECK (LENGTH(wtid_raw)=32)'
       ') %s ;'
       ,table_name
-- 
cgit v1.2.3

