From 94a313d74b403f7afa130a999ab2171414f86a46 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sat, 16 Sep 2023 14:04:36 +0200
Subject: create coin history table via triggers

---
 src/exchangedb/0002-refresh_commitments.sql | 36 +++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

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

diff --git a/src/exchangedb/0002-refresh_commitments.sql b/src/exchangedb/0002-refresh_commitments.sql
index 614fc6373..50e298c2a 100644
--- a/src/exchangedb/0002-refresh_commitments.sql
+++ b/src/exchangedb/0002-refresh_commitments.sql
@@ -106,6 +106,37 @@ END
 $$;
 
 
+CREATE OR REPLACE FUNCTION refresh_commitments_insert_trigger()
+  RETURNS trigger
+  LANGUAGE plpgsql
+  AS $$
+BEGIN
+  INSERT INTO exchange.coin_history
+    (coin_pub
+    ,table_name
+    ,serial_id)
+ VALUES
+     (NEW.old_coin_pub
+    ,'refresh_commitments'
+    ,NEW.melt_serial_id);
+  RETURN NEW;
+END $$;
+COMMENT ON FUNCTION refresh_commitments_insert_trigger()
+  IS 'Automatically generate coin history entry.';
+
+
+CREATE FUNCTION master_table_refresh_commitments()
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+BEGIN
+  CREATE TRIGGER refresh_commitments_on_insert
+    AFTER INSERT
+     ON refresh_commitments
+     FOR EACH ROW EXECUTE FUNCTION refresh_commitments_insert_trigger();
+END $$;
+
+
 INSERT INTO exchange_tables
     (name
     ,version
@@ -127,4 +158,9 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'foreign'
     ,TRUE
+    ,FALSE),
+   ('refresh_commitments'
+    ,'exchange-0002'
+    ,'master'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

