From 7045d3df1694d92c98369402adee675238833d6c Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Sat, 16 Sep 2023 20:48:25 +0200
Subject: add triggers to populate reserve history table

---
 src/exchangedb/0002-history_requests.sql | 38 +++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

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

diff --git a/src/exchangedb/0002-history_requests.sql b/src/exchangedb/0002-history_requests.sql
index d6a81c458..0714e1bea 100644
--- a/src/exchangedb/0002-history_requests.sql
+++ b/src/exchangedb/0002-history_requests.sql
@@ -1,6 +1,6 @@
 --
 -- This file is part of TALER
--- Copyright (C) 2014--2022 Taler Systems SA
+-- Copyright (C) 2014--2023 Taler Systems SA
 --
 -- TALER is free software; you can redistribute it and/or modify it under the
 -- terms of the GNU General Public License as published by the Free Software
@@ -99,6 +99,37 @@ BEGIN
 END $$;
 
 
+CREATE OR REPLACE FUNCTION history_requests_insert_trigger()
+  RETURNS trigger
+  LANGUAGE plpgsql
+  AS $$
+BEGIN
+  INSERT INTO reserve_history
+    (reserve_pub
+    ,table_name
+    ,serial_id)
+  VALUES
+    (NEW.reserve_pub
+    ,'history_requests'
+    ,NEW.history_request_serial_id);
+  RETURN NEW;
+END $$;
+COMMENT ON FUNCTION history_requests_insert_trigger()
+  IS 'Automatically generate reserve history entry.';
+
+
+CREATE FUNCTION master_table_history_requests()
+RETURNS VOID
+LANGUAGE plpgsql
+AS $$
+BEGIN
+  CREATE TRIGGER history_requests_on_insert
+    AFTER INSERT
+     ON history_requests
+     FOR EACH ROW EXECUTE FUNCTION history_requests_insert_trigger();
+END $$;
+
+
 INSERT INTO exchange_tables
     (name
     ,version
@@ -120,4 +151,9 @@ INSERT INTO exchange_tables
     ,'exchange-0002'
     ,'foreign'
     ,TRUE
+    ,FALSE),
+    ('history_requests'
+    ,'exchange-0002'
+    ,'master'
+    ,TRUE
     ,FALSE);
-- 
cgit v1.2.3

