-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_missing_tables.sql
More file actions
48 lines (42 loc) · 1.94 KB
/
create_missing_tables.sql
File metadata and controls
48 lines (42 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-- Create collectionItems table if it doesn't exist
CREATE TABLE IF NOT EXISTS "collectionItems" (
id SERIAL PRIMARY KEY,
collectionId INTEGER NOT NULL,
snippetId INTEGER NOT NULL,
createdAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE(collectionId, snippetId)
);
-- Create comments table if it doesn't exist
CREATE TABLE IF NOT EXISTS comments (
id SERIAL PRIMARY KEY,
snippetId INTEGER NOT NULL,
userId VARCHAR,
content TEXT NOT NULL,
createdAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Add indexes for performance
CREATE INDEX IF NOT EXISTS idx_collectionitems_collectionid ON "collectionItems"(collectionId);
CREATE INDEX IF NOT EXISTS idx_collectionitems_snippetid ON "collectionItems"(snippetId);
CREATE INDEX IF NOT EXISTS idx_comments_snippetid ON comments(snippetId);
-- Create the collectionItems table (case-sensitive name, requires double quotes)
CREATE TABLE IF NOT EXISTS "collectionItems" (
id SERIAL PRIMARY KEY,
collectionId INTEGER NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
snippetId INTEGER NOT NULL REFERENCES snippets(id) ON DELETE CASCADE,
createdAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE(collectionId, snippetId)
);
-- Create the comments table
CREATE TABLE IF NOT EXISTS comments (
id SERIAL PRIMARY KEY,
snippetId INTEGER NOT NULL REFERENCES snippets(id) ON DELETE CASCADE,
userId VARCHAR,
content TEXT NOT NULL,
createdAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_collectionitems_collectionid ON "collectionItems"("collectionId");
CREATE INDEX IF NOT EXISTS idx_collectionitems_snippetid ON "collectionItems"("snippetId");
CREATE INDEX IF NOT EXISTS idx_comments_snippetid ON comments("snippetId");