Skip to content

Commit abae19c

Browse files
committed
include user tables triggers
1 parent c2edb50 commit abae19c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

packages/db/migrations/0157_wandering_lockheed.sql

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
1+
-- ============================================================
2+
-- Triggers for user_table_rows (from 0156 schema addition)
3+
-- Drizzle does not generate trigger SQL, so they are applied here.
4+
-- ============================================================
5+
6+
CREATE OR REPLACE FUNCTION increment_user_table_row_count()
7+
RETURNS TRIGGER AS $$
8+
DECLARE
9+
current_count INTEGER;
10+
max_allowed INTEGER;
11+
BEGIN
12+
SELECT row_count, max_rows INTO current_count, max_allowed
13+
FROM user_table_definitions
14+
WHERE id = NEW.table_id;
15+
16+
IF current_count >= max_allowed THEN
17+
RAISE EXCEPTION 'Maximum row limit (%) reached for table %', max_allowed, NEW.table_id;
18+
END IF;
19+
20+
UPDATE user_table_definitions
21+
SET row_count = row_count + 1,
22+
updated_at = now()
23+
WHERE id = NEW.table_id;
24+
25+
RETURN NEW;
26+
END;
27+
$$ LANGUAGE plpgsql;
28+
--> statement-breakpoint
29+
30+
CREATE OR REPLACE FUNCTION decrement_user_table_row_count()
31+
RETURNS TRIGGER AS $$
32+
BEGIN
33+
UPDATE user_table_definitions
34+
SET row_count = GREATEST(row_count - 1, 0),
35+
updated_at = now()
36+
WHERE id = OLD.table_id;
37+
38+
RETURN OLD;
39+
END;
40+
$$ LANGUAGE plpgsql;
41+
--> statement-breakpoint
42+
43+
CREATE TRIGGER user_table_rows_insert_trigger
44+
BEFORE INSERT ON user_table_rows
45+
FOR EACH ROW
46+
EXECUTE FUNCTION increment_user_table_row_count();
47+
--> statement-breakpoint
48+
49+
CREATE TRIGGER user_table_rows_delete_trigger
50+
AFTER DELETE ON user_table_rows
51+
FOR EACH ROW
52+
EXECUTE FUNCTION decrement_user_table_row_count();
53+
--> statement-breakpoint
54+
55+
-- ============================================================
56+
-- Credential system schema + backfill
57+
-- ============================================================
58+
159
CREATE TYPE "public"."credential_member_role" AS ENUM('admin', 'member');--> statement-breakpoint
260
CREATE TYPE "public"."credential_member_status" AS ENUM('active', 'pending', 'revoked');--> statement-breakpoint
361
CREATE TYPE "public"."credential_type" AS ENUM('oauth', 'env_workspace', 'env_personal');--> statement-breakpoint

0 commit comments

Comments
 (0)