1
+ CREATE TABLE "projects " (
2
+ " id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name " projects_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1 ),
3
+ " name" varchar (255 ) NOT NULL ,
4
+ " description" text ,
5
+ " shared_user_ids" text [] DEFAULT ' {}' NOT NULL ,
6
+ " created_at" timestamp with time zone DEFAULT now() NOT NULL ,
7
+ " owner_id" text NOT NULL
8
+ );
9
+ -- > statement-breakpoint
10
+ CREATE TABLE "todos " (
11
+ " id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name " todos_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1 ),
12
+ " text" varchar (500 ) NOT NULL ,
13
+ " completed" boolean DEFAULT false NOT NULL ,
14
+ " created_at" timestamp with time zone DEFAULT now() NOT NULL ,
15
+ " user_id" text NOT NULL ,
16
+ " project_id" integer NOT NULL ,
17
+ " user_ids" text [] DEFAULT ' {}' NOT NULL
18
+ );
19
+ -- > statement-breakpoint
20
+ CREATE TABLE "accounts " (
21
+ " id" text PRIMARY KEY NOT NULL ,
22
+ " account_id" text NOT NULL ,
23
+ " provider_id" text NOT NULL ,
24
+ " user_id" text NOT NULL ,
25
+ " access_token" text ,
26
+ " refresh_token" text ,
27
+ " id_token" text ,
28
+ " access_token_expires_at" timestamp ,
29
+ " refresh_token_expires_at" timestamp ,
30
+ " scope" text ,
31
+ " password" text ,
32
+ " created_at" timestamp NOT NULL ,
33
+ " updated_at" timestamp NOT NULL
34
+ );
35
+ -- > statement-breakpoint
36
+ CREATE TABLE "sessions " (
37
+ " id" text PRIMARY KEY NOT NULL ,
38
+ " expires_at" timestamp NOT NULL ,
39
+ " token" text NOT NULL ,
40
+ " created_at" timestamp NOT NULL ,
41
+ " updated_at" timestamp NOT NULL ,
42
+ " ip_address" text ,
43
+ " user_agent" text ,
44
+ " user_id" text NOT NULL ,
45
+ CONSTRAINT " sessions_token_unique" UNIQUE(" token" )
46
+ );
47
+ -- > statement-breakpoint
48
+ CREATE TABLE "users " (
49
+ " id" text PRIMARY KEY NOT NULL ,
50
+ " name" text NOT NULL ,
51
+ " email" text NOT NULL ,
52
+ " email_verified" boolean NOT NULL ,
53
+ " image" text ,
54
+ " created_at" timestamp NOT NULL ,
55
+ " updated_at" timestamp NOT NULL ,
56
+ CONSTRAINT " users_email_unique" UNIQUE(" email" )
57
+ );
58
+ -- > statement-breakpoint
59
+ CREATE TABLE "verifications " (
60
+ " id" text PRIMARY KEY NOT NULL ,
61
+ " identifier" text NOT NULL ,
62
+ " value" text NOT NULL ,
63
+ " expires_at" timestamp NOT NULL ,
64
+ " created_at" timestamp ,
65
+ " updated_at" timestamp
66
+ );
67
+ -- > statement-breakpoint
68
+ ALTER TABLE " projects" ADD CONSTRAINT " projects_owner_id_users_id_fk" FOREIGN KEY (" owner_id" ) REFERENCES " public" ." users" (" id" ) ON DELETE cascade ON UPDATE no action;-- > statement-breakpoint
69
+ ALTER TABLE " todos" ADD CONSTRAINT " todos_user_id_users_id_fk" FOREIGN KEY (" user_id" ) REFERENCES " public" ." users" (" id" ) ON DELETE cascade ON UPDATE no action;-- > statement-breakpoint
70
+ ALTER TABLE " todos" ADD CONSTRAINT " todos_project_id_projects_id_fk" FOREIGN KEY (" project_id" ) REFERENCES " public" ." projects" (" id" ) ON DELETE cascade ON UPDATE no action;-- > statement-breakpoint
71
+ ALTER TABLE " accounts" ADD CONSTRAINT " accounts_user_id_users_id_fk" FOREIGN KEY (" user_id" ) REFERENCES " public" ." users" (" id" ) ON DELETE cascade ON UPDATE no action;-- > statement-breakpoint
72
+ ALTER TABLE " sessions" ADD CONSTRAINT " sessions_user_id_users_id_fk" FOREIGN KEY (" user_id" ) REFERENCES " public" ." users" (" id" ) ON DELETE cascade ON UPDATE no action;-- > statement-breakpoint
73
+
74
+ -- Function to sync user_ids in todos when project shared_user_ids changes
75
+ CREATE OR REPLACE FUNCTION sync_todo_user_ids ()
76
+ RETURNS TRIGGER AS $$
77
+ BEGIN
78
+ -- Update all todos in the project with the new user_ids
79
+ UPDATE todos
80
+ SET user_ids = ARRAY(SELECT DISTINCT unnest(ARRAY[NEW .owner_id ] || NEW .shared_user_ids ))
81
+ WHERE project_id = NEW .id ;
82
+
83
+ RETURN NEW;
84
+ END;
85
+ $$ LANGUAGE plpgsql;
86
+
87
+ -- Function to populate user_ids when a todo is inserted
88
+ CREATE OR REPLACE FUNCTION populate_todo_user_ids ()
89
+ RETURNS TRIGGER AS $$
90
+ BEGIN
91
+ -- Get the project's owner_id and shared_user_ids
92
+ SELECT ARRAY(SELECT DISTINCT unnest(ARRAY[owner_id] || shared_user_ids))
93
+ INTO NEW .user_ids
94
+ FROM projects
95
+ WHERE id = NEW .project_id ;
96
+
97
+ RETURN NEW;
98
+ END;
99
+ $$ LANGUAGE plpgsql;
100
+
101
+ -- Trigger to sync user_ids when project shared_user_ids changes
102
+ CREATE TRIGGER sync_todo_user_ids_trigger
103
+ AFTER UPDATE OF shared_user_ids, owner_id ON projects
104
+ FOR EACH ROW
105
+ EXECUTE FUNCTION sync_todo_user_ids();
106
+
107
+ -- Trigger to populate user_ids when todo is inserted
108
+ CREATE TRIGGER populate_todo_user_ids_trigger
109
+ BEFORE INSERT ON todos
110
+ FOR EACH ROW
111
+ EXECUTE FUNCTION populate_todo_user_ids();
0 commit comments