From 3223d93cb3c77ab02aa0a35f2a8314e447cee9a4 Mon Sep 17 00:00:00 2001
From: Benedikt Peetz <benedikt.peetz@b-peetz.de>
Date: Thu, 9 Jul 2026 21:43:23 +0200
Subject: chore: Separate daemon, client, server, and lib into crates

---
 .../20230515221038_trigger-delete-only.sql         | 30 ++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 crates/server/db/server-pg-migrations/20230515221038_trigger-delete-only.sql

(limited to 'crates/server/db/server-pg-migrations/20230515221038_trigger-delete-only.sql')

diff --git a/crates/server/db/server-pg-migrations/20230515221038_trigger-delete-only.sql b/crates/server/db/server-pg-migrations/20230515221038_trigger-delete-only.sql
new file mode 100644
index 00000000..3d0bba52
--- /dev/null
+++ b/crates/server/db/server-pg-migrations/20230515221038_trigger-delete-only.sql
@@ -0,0 +1,30 @@
+-- We do not need to run the trigger on deletes, as the only time we are deleting history is when the user
+-- has already been deleted
+-- This actually slows down deleting all the history a good bit!
+
+create or replace function user_history_count()
+returns trigger as 
+$func$
+begin
+	if (TG_OP='INSERT') then
+		update total_history_count_user set total = total + 1 where user_id = new.user_id;
+
+		if not found then
+			insert into total_history_count_user(user_id, total) 
+			values (
+				new.user_id, 
+				(select count(1) from history where user_id = new.user_id)
+			);
+		end if;
+	end if;
+
+	return NEW; -- this is actually ignored for an after trigger, but oh well
+end;
+$func$
+language plpgsql volatile -- pldfplplpflh
+cost 100; -- default value
+
+create or replace trigger tg_user_history_count 
+	after insert on history 
+	for each row 
+	execute procedure user_history_count();
-- 
cgit v1.3.1

