-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigration-update.sql
More file actions
78 lines (69 loc) · 2.78 KB
/
Copy pathmigration-update.sql
File metadata and controls
78 lines (69 loc) · 2.78 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Migration SQL - Run this to add new features
-- Only run this if you already have the basic tables (users, posts, comments, votes)
-- Add new column to posts table for app reviews
ALTER TABLE posts ADD COLUMN IF NOT EXISTS app_id UUID;
-- Update posts type constraint to include new type
ALTER TABLE posts DROP CONSTRAINT IF EXISTS posts_type_check;
ALTER TABLE posts ADD CONSTRAINT posts_type_check
CHECK (type IN ('Bug Report', 'Feature Request', 'Complaint', 'App Review Request'));
-- Create connections/follow table
CREATE TABLE IF NOT EXISTS connections (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
follower_id UUID REFERENCES users(id) ON DELETE CASCADE,
following_id UUID REFERENCES users(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(follower_id, following_id)
);
-- Create apps/software table
CREATE TABLE IF NOT EXISTS apps (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL,
app_url TEXT,
logo_url TEXT,
category TEXT NOT NULL,
average_rating DECIMAL(3,2) DEFAULT 0,
total_reviews INTEGER DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Create app reviews table
CREATE TABLE IF NOT EXISTS app_reviews (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
app_id UUID REFERENCES apps(id) ON DELETE CASCADE,
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
review_text TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(app_id, user_id)
);
-- Add foreign key for app_id in posts (if not exists)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'posts_app_id_fkey'
) THEN
ALTER TABLE posts ADD CONSTRAINT posts_app_id_fkey
FOREIGN KEY (app_id) REFERENCES apps(id) ON DELETE SET NULL;
END IF;
END $$;
-- Create new indexes
CREATE INDEX IF NOT EXISTS idx_posts_company ON posts(company);
CREATE INDEX IF NOT EXISTS idx_connections_follower ON connections(follower_id);
CREATE INDEX IF NOT EXISTS idx_connections_following ON connections(following_id);
CREATE INDEX IF NOT EXISTS idx_apps_user_id ON apps(user_id);
CREATE INDEX IF NOT EXISTS idx_app_reviews_app_id ON app_reviews(app_id);
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
CREATE INDEX IF NOT EXISTS idx_users_display_name ON users(display_name);
-- Create triggers for new tables
CREATE TRIGGER update_apps_updated_at
BEFORE UPDATE ON apps
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_app_reviews_updated_at
BEFORE UPDATE ON app_reviews
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();