forked from QuivrHQ/quivr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20230620183620_use_supabase_user_id.sql
218 lines (195 loc) · 5.34 KB
/
20230620183620_use_supabase_user_id.sql
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
-- Add the 'supabase_id' column to the 'users' table if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS(
SELECT 1
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name = 'supabase_id'
) THEN
ALTER TABLE users ADD COLUMN supabase_id UUID;
END IF;
END
$$;
-- Update the 'supabase_id' column with the corresponding 'id' from 'auth.users'
-- Fails if there's no matching email in auth.users
UPDATE users
SET supabase_id = au.id
FROM auth.users au
WHERE users.email = au.email;
-- Create a copy of old users table for safety
-- Fails if 'users_old' table already exists
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_name = 'users_old'
) THEN
CREATE TABLE users_old AS TABLE users;
END IF;
END
$$;
-- Drop the old primary key if it exists
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'users_pkey'
) THEN
ALTER TABLE users DROP CONSTRAINT users_pkey CASCADE;
END IF;
END
$$;
-- Rename columns if not already renamed
DO $$
BEGIN
IF EXISTS(
SELECT 1
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name = 'user_id'
) AND NOT EXISTS(
SELECT 1
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name = 'old_user_id'
) THEN
ALTER TABLE users RENAME COLUMN user_id TO old_user_id;
ALTER TABLE users RENAME COLUMN supabase_id TO user_id;
END IF;
END
$$;
-- Create a new primary key with user_id and date if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'users_pkey'
) THEN
ALTER TABLE users ADD PRIMARY KEY (user_id, date);
END IF;
END
$$;
-- Update the 'chats' table
-- Drop old foreign key constraint if it exists
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'chats_user_id_fkey'
) THEN
ALTER TABLE chats DROP CONSTRAINT chats_user_id_fkey;
END IF;
END
$$;
-- Update user_id in chats
-- Fails if there's no matching old_user_id in users
UPDATE chats
SET user_id = u.user_id::uuid
FROM users u
WHERE chats.user_id::uuid = u.old_user_id::uuid;
-- Add new foreign key constraint if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'chats_user_id_fkey'
) THEN
ALTER TABLE chats ADD CONSTRAINT chats_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id);
END IF;
END
$$;
-- Update the 'brains_users' table
-- Add a new 'new_user_id' column to the 'brains_users' table
ALTER TABLE brains_users ADD COLUMN new_user_id UUID;
-- Update 'new_user_id' in the 'brains_users' table based on the 'email' in the 'users' table
UPDATE brains_users bu
SET new_user_id = u.user_id
FROM users u
WHERE bu.user_id = u.email;
-- Once you are sure that 'new_user_id' has been correctly populated, drop the old 'user_id' column
ALTER TABLE brains_users DROP COLUMN user_id;
-- Rename 'new_user_id' column to 'user_id'
ALTER TABLE brains_users RENAME COLUMN new_user_id TO user_id;
-- Delete users with user_id not in supabase auth
DELETE FROM brains_users
WHERE NOT EXISTS (
SELECT 1
FROM auth.users
WHERE brains_users.user_id = auth.users.id
);
-- Drop old foreign key constraint if it exists
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'brains_users_user_id_fkey'
) THEN
ALTER TABLE brains_users DROP CONSTRAINT brains_users_user_id_fkey;
END IF;
END
$$;
-- Update user_id in brains_users
-- Fails if there's no matching old_user_id in users
UPDATE brains_users
SET user_id = u.user_id::uuid
FROM users u
WHERE brains_users.user_id::uuid = u.old_user_id::uuid;
-- Add new foreign key constraints if they don't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'brains_users_user_id_fkey'
) THEN
ALTER TABLE brains_users ADD CONSTRAINT brains_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id);
--ALTER TABLE brains_users ADD CONSTRAINT brains_users_brain_id_fkey FOREIGN KEY (brain_id) REFERENCES brains (brain_id);
END IF;
END
$$;
-- Update the 'api_keys' table
-- Drop old foreign key constraint if it exists
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'api_keys_user_id_fkey'
) THEN
ALTER TABLE api_keys DROP CONSTRAINT api_keys_user_id_fkey;
END IF;
END
$$;
-- Update user_id in api_keys
-- Fails if there's no matching old_user_id in users
UPDATE api_keys
SET user_id = u.user_id::uuid
FROM users u
WHERE api_keys.user_id::uuid = u.old_user_id::uuid;
-- Add new foreign key constraint if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'api_keys_user_id_fkey'
) THEN
ALTER TABLE api_keys ADD CONSTRAINT api_keys_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id);
END IF;
END
$$;
-- Optionally Drop the 'old_user_id' column from the 'users' table
-- Uncomment if you are sure that it is no longer needed.
--ALTER TABLE users DROP COLUMN old_user_id;
INSERT INTO migrations (name)
SELECT '20230627151100_update_match_vectors'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20230627151100_update_match_vectors'
);