Skip to content

Commit 1f682eb

Browse files
committed
readd migrations
1 parent 8d43947 commit 1f682eb

File tree

3 files changed

+10765
-0
lines changed

3 files changed

+10765
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
CREATE TABLE "user_table_definitions" (
2+
"id" text PRIMARY KEY NOT NULL,
3+
"workspace_id" text NOT NULL,
4+
"name" text NOT NULL,
5+
"description" text,
6+
"schema" jsonb NOT NULL,
7+
"max_rows" integer DEFAULT 10000 NOT NULL,
8+
"row_count" integer DEFAULT 0 NOT NULL,
9+
"created_by" text NOT NULL,
10+
"created_at" timestamp DEFAULT now() NOT NULL,
11+
"updated_at" timestamp DEFAULT now() NOT NULL
12+
);
13+
--> statement-breakpoint
14+
CREATE TABLE "user_table_rows" (
15+
"id" text PRIMARY KEY NOT NULL,
16+
"table_id" text NOT NULL,
17+
"workspace_id" text NOT NULL,
18+
"data" jsonb NOT NULL,
19+
"created_at" timestamp DEFAULT now() NOT NULL,
20+
"updated_at" timestamp DEFAULT now() NOT NULL,
21+
"created_by" text
22+
);
23+
--> statement-breakpoint
24+
ALTER TABLE "user_table_definitions" ADD CONSTRAINT "user_table_definitions_workspace_id_workspace_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspace"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
25+
ALTER TABLE "user_table_definitions" ADD CONSTRAINT "user_table_definitions_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
26+
ALTER TABLE "user_table_rows" ADD CONSTRAINT "user_table_rows_table_id_user_table_definitions_id_fk" FOREIGN KEY ("table_id") REFERENCES "public"."user_table_definitions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
27+
ALTER TABLE "user_table_rows" ADD CONSTRAINT "user_table_rows_workspace_id_workspace_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspace"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
28+
ALTER TABLE "user_table_rows" ADD CONSTRAINT "user_table_rows_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
29+
CREATE INDEX "user_table_def_workspace_id_idx" ON "user_table_definitions" USING btree ("workspace_id");--> statement-breakpoint
30+
CREATE UNIQUE INDEX "user_table_def_workspace_name_unique" ON "user_table_definitions" USING btree ("workspace_id","name");--> statement-breakpoint
31+
CREATE INDEX "user_table_rows_table_id_idx" ON "user_table_rows" USING btree ("table_id");--> statement-breakpoint
32+
CREATE INDEX "user_table_rows_workspace_id_idx" ON "user_table_rows" USING btree ("workspace_id");--> statement-breakpoint
33+
CREATE INDEX "user_table_rows_data_gin_idx" ON "user_table_rows" USING gin ("data");--> statement-breakpoint
34+
CREATE INDEX "user_table_rows_workspace_table_idx" ON "user_table_rows" USING btree ("workspace_id","table_id");--> statement-breakpoint
35+
36+
-- Trigger function to increment row_count on INSERT
37+
CREATE OR REPLACE FUNCTION increment_user_table_row_count()
38+
RETURNS TRIGGER AS $$
39+
DECLARE
40+
current_count INTEGER;
41+
max_allowed INTEGER;
42+
BEGIN
43+
-- Get current count and max_rows
44+
SELECT row_count, max_rows INTO current_count, max_allowed
45+
FROM user_table_definitions
46+
WHERE id = NEW.table_id;
47+
48+
-- Check if we would exceed max_rows
49+
IF current_count >= max_allowed THEN
50+
RAISE EXCEPTION 'Maximum row limit (%) reached for table %', max_allowed, NEW.table_id;
51+
END IF;
52+
53+
-- Increment the row count
54+
UPDATE user_table_definitions
55+
SET row_count = row_count + 1,
56+
updated_at = now()
57+
WHERE id = NEW.table_id;
58+
59+
RETURN NEW;
60+
END;
61+
$$ LANGUAGE plpgsql;
62+
--> statement-breakpoint
63+
64+
-- Trigger function to decrement row_count on DELETE
65+
CREATE OR REPLACE FUNCTION decrement_user_table_row_count()
66+
RETURNS TRIGGER AS $$
67+
BEGIN
68+
UPDATE user_table_definitions
69+
SET row_count = GREATEST(row_count - 1, 0),
70+
updated_at = now()
71+
WHERE id = OLD.table_id;
72+
73+
RETURN OLD;
74+
END;
75+
$$ LANGUAGE plpgsql;
76+
--> statement-breakpoint
77+
78+
-- Create trigger for INSERT
79+
CREATE TRIGGER user_table_rows_insert_trigger
80+
BEFORE INSERT ON user_table_rows
81+
FOR EACH ROW
82+
EXECUTE FUNCTION increment_user_table_row_count();
83+
--> statement-breakpoint
84+
85+
-- Create trigger for DELETE
86+
CREATE TRIGGER user_table_rows_delete_trigger
87+
AFTER DELETE ON user_table_rows
88+
FOR EACH ROW
89+
EXECUTE FUNCTION decrement_user_table_row_count();

0 commit comments

Comments
 (0)