@@ -129,54 +129,60 @@ WITH provider_names(pid, sname) AS (
129129 (' vertex-ai' , ' Vertex AI' ),
130130 (' supabase' , ' Supabase' )
131131),
132- workspace_user_access AS (
133- SELECT DISTINCT w .id AS workspace_id, p .user_id , p .permission_type
132+ oauth_targets AS (
133+ SELECT
134+ ' cred_' || md5(wua .workspace_id || ' :' || a .id ) AS cred_id,
135+ wua .workspace_id ,
136+ a .id AS account_id,
137+ a .user_id AS account_owner_id,
138+ a .provider_id ,
139+ COALESCE(u .name , ' User' ) || ' ' ' s ' || COALESCE(pn .sname , a .provider_id ) AS display_name
140+ FROM " account" a
141+ INNER JOIN (
142+ SELECT DISTINCT w .id AS workspace_id, p .user_id
143+ FROM " permissions" p
144+ INNER JOIN " workspace" w ON w .id = p .entity_id
145+ WHERE p .entity_type = ' workspace'
146+ UNION
147+ SELECT w .id , w .owner_id FROM " workspace" w
148+ ) wua ON wua .user_id = a .user_id
149+ INNER JOIN " user" u ON u .id = a .user_id
150+ LEFT JOIN provider_names pn ON pn .pid = a .provider_id
151+ WHERE a .provider_id NOT IN (' credential' , ' github' , ' google' )
152+ ),
153+ oauth_workspace_members AS (
154+ SELECT DISTINCT w .id AS workspace_id, p .user_id
134155 FROM " permissions" p
135156 INNER JOIN " workspace" w ON w .id = p .entity_id
136157 WHERE p .entity_type = ' workspace'
137158 UNION
138- SELECT w .id , w .owner_id , ' admin' ::" permission_type"
139- FROM " workspace" w
159+ SELECT w .id , w .owner_id FROM " workspace" w
140160),
141- oauth_creds AS (
161+ _oauth_insert AS (
142162 INSERT INTO " credential" (
143163 " id" , " workspace_id" , " type" , " display_name" , " provider_id" , " account_id" ,
144164 " created_by" , " created_at" , " updated_at"
145165 )
146- SELECT
147- ' cred_' || md5(wua .workspace_id || ' :' || a .id ) AS id,
148- wua .workspace_id ,
149- ' oauth' ::" credential_type" ,
150- COALESCE(u .name , ' User' ) || ' ' ' s ' || COALESCE(pn .sname , a .provider_id ),
151- a .provider_id ,
152- a .id ,
153- a .user_id ,
154- now(),
155- now()
156- FROM " account" a
157- INNER JOIN workspace_user_access wua ON wua .user_id = a .user_id
158- INNER JOIN " user" u ON u .id = a .user_id
159- LEFT JOIN provider_names pn ON pn .pid = a .provider_id
160- WHERE a .provider_id NOT IN (' credential' , ' github' , ' google' )
166+ SELECT cred_id, workspace_id, ' oauth' ::" credential_type" , display_name,
167+ provider_id, account_id, account_owner_id, now(), now()
168+ FROM oauth_targets
161169 ON CONFLICT DO NOTHING
162- RETURNING id, workspace_id, account_id
163170)
164171INSERT INTO " credential_member" (
165172 " id" , " credential_id" , " user_id" , " role" , " status" , " joined_at" , " invited_by" , " created_at" , " updated_at"
166173)
167174SELECT
168- ' credm_' || md5(oc . id || ' :' || wua .user_id ),
169- oc . id ,
170- wua .user_id ,
171- CASE WHEN a . user_id = wua .user_id THEN ' admin' ::" credential_member_role" ELSE ' member' ::" credential_member_role" END,
175+ ' credm_' || md5(ot . cred_id || ' :' || owm .user_id ),
176+ ot . cred_id ,
177+ owm .user_id ,
178+ CASE WHEN ot . account_owner_id = owm .user_id THEN ' admin' ::" credential_member_role" ELSE ' member' ::" credential_member_role" END,
172179 ' active' ::" credential_member_status" ,
173180 now(),
174- a . user_id ,
181+ ot . account_owner_id ,
175182 now(),
176183 now()
177- FROM oauth_creds oc
178- INNER JOIN " account" a ON a .id = oc .account_id
179- INNER JOIN workspace_user_access wua ON wua .workspace_id = oc .workspace_id
184+ FROM oauth_targets ot
185+ INNER JOIN oauth_workspace_members owm ON owm .workspace_id = ot .workspace_id
180186ON CONFLICT DO NOTHING;
181187
182188-- > statement-breakpoint
@@ -186,16 +192,7 @@ ON CONFLICT DO NOTHING;
186192-- For each key in workspace_environment.variables JSON,
187193-- create a credential. Workspace admins = admin, others = member.
188194
189- WITH workspace_user_access AS (
190- SELECT DISTINCT w .id AS workspace_id, p .user_id , p .permission_type
191- FROM " permissions" p
192- INNER JOIN " workspace" w ON w .id = p .entity_id
193- WHERE p .entity_type = ' workspace'
194- UNION
195- SELECT w .id , w .owner_id , ' admin' ::" permission_type"
196- FROM " workspace" w
197- ),
198- ws_env_keys AS (
195+ WITH ws_env_keys AS (
199196 SELECT
200197 we .workspace_id ,
201198 key AS env_key,
@@ -204,39 +201,53 @@ ws_env_keys AS (
204201 INNER JOIN " workspace" w ON w .id = we .workspace_id
205202 CROSS JOIN LATERAL json_object_keys(we .variables ::json) AS key
206203),
207- ws_env_creds AS (
208- INSERT INTO " credential" (
209- " id" , " workspace_id" , " type" , " display_name" , " env_key" ,
210- " created_by" , " created_at" , " updated_at"
211- )
204+ ws_env_targets AS (
212205 SELECT
213- ' cred_' || md5(wek .workspace_id || ' :env_workspace:' || wek .env_key ),
206+ ' cred_' || md5(wek .workspace_id || ' :env_workspace:' || wek .env_key ) AS cred_id ,
214207 wek .workspace_id ,
215- ' env_workspace' ::" credential_type" ,
216- wek .env_key ,
217208 wek .env_key ,
218- wek .owner_id ,
219- now(),
220- now()
209+ wek .owner_id
221210 FROM ws_env_keys wek
211+ ),
212+ ws_workspace_members AS (
213+ SELECT DISTINCT ON (workspace_id, user_id)
214+ workspace_id, user_id, permission_type
215+ FROM (
216+ SELECT w .id AS workspace_id, p .user_id , p .permission_type
217+ FROM " permissions" p
218+ INNER JOIN " workspace" w ON w .id = p .entity_id
219+ WHERE p .entity_type = ' workspace'
220+ UNION ALL
221+ SELECT w .id , w .owner_id , ' admin' ::" permission_type"
222+ FROM " workspace" w
223+ ) sub
224+ ORDER BY workspace_id, user_id, (permission_type = ' admin' ) DESC
225+ ),
226+ _ws_env_insert AS (
227+ INSERT INTO " credential" (
228+ " id" , " workspace_id" , " type" , " display_name" , " env_key" ,
229+ " created_by" , " created_at" , " updated_at"
230+ )
231+ SELECT cred_id, workspace_id, ' env_workspace' ::" credential_type" ,
232+ env_key, env_key, owner_id, now(), now()
233+ FROM ws_env_targets
222234 ON CONFLICT DO NOTHING
223- RETURNING id, workspace_id
224235)
225236INSERT INTO " credential_member" (
226237 " id" , " credential_id" , " user_id" , " role" , " status" , " joined_at" , " invited_by" , " created_at" , " updated_at"
227238)
228239SELECT
229- ' credm_' || md5(wec . id || ' :' || wua .user_id ),
230- wec . id ,
231- wua .user_id ,
232- CASE WHEN wua .permission_type = ' admin' THEN ' admin' ::" credential_member_role" ELSE ' member' ::" credential_member_role" END,
240+ ' credm_' || md5(wet . cred_id || ' :' || wm .user_id ),
241+ wet . cred_id ,
242+ wm .user_id ,
243+ CASE WHEN wm .permission_type = ' admin' THEN ' admin' ::" credential_member_role" ELSE ' member' ::" credential_member_role" END,
233244 ' active' ::" credential_member_status" ,
234245 now(),
235- ( SELECT w .owner_id FROM " workspace " w WHERE w . id = wec . workspace_id LIMIT 1 ) ,
246+ wet .owner_id ,
236247 now(),
237248 now()
238- FROM ws_env_creds wec
239- INNER JOIN workspace_user_access wua ON wua .workspace_id = wec .workspace_id
249+ FROM ws_env_targets wet
250+ INNER JOIN ws_workspace_members wm ON wm .workspace_id = wet .workspace_id
240251ON CONFLICT DO NOTHING;
241252
242253-- > statement-breakpoint
@@ -246,55 +257,51 @@ ON CONFLICT DO NOTHING;
246257-- For each key in environment.variables JSON, for each workspace
247258-- the user belongs to, create a credential with the user as admin.
248259
249- WITH workspace_user_access AS (
250- SELECT DISTINCT w .id AS workspace_id, p .user_id
251- FROM " permissions" p
252- INNER JOIN " workspace" w ON w .id = p .entity_id
253- WHERE p .entity_type = ' workspace'
254- UNION
255- SELECT w .id , w .owner_id
256- FROM " workspace" w
257- ),
258- personal_env_keys AS (
260+ WITH personal_env_keys AS (
259261 SELECT
260262 e .user_id ,
261263 key AS env_key
262264 FROM " environment" e
263265 CROSS JOIN LATERAL json_object_keys(e .variables ::json) AS key
264266),
265- personal_env_creds AS (
266- INSERT INTO " credential" (
267- " id" , " workspace_id" , " type" , " display_name" , " env_key" , " env_owner_user_id" ,
268- " created_by" , " created_at" , " updated_at"
269- )
267+ personal_env_targets AS (
270268 SELECT
271- ' cred_' || md5(wua .workspace_id || ' :env_personal:' || pek .env_key || ' :' || pek .user_id ),
269+ ' cred_' || md5(wua .workspace_id || ' :env_personal:' || pek .env_key || ' :' || pek .user_id ) AS cred_id ,
272270 wua .workspace_id ,
273- ' env_personal' ::" credential_type" ,
274271 pek .env_key ,
275- pek .env_key ,
276- pek .user_id ,
277- pek .user_id ,
278- now(),
279- now()
272+ pek .user_id
280273 FROM personal_env_keys pek
281- INNER JOIN workspace_user_access wua ON wua .user_id = pek .user_id
274+ INNER JOIN (
275+ SELECT DISTINCT w .id AS workspace_id, p .user_id
276+ FROM " permissions" p
277+ INNER JOIN " workspace" w ON w .id = p .entity_id
278+ WHERE p .entity_type = ' workspace'
279+ UNION
280+ SELECT w .id , w .owner_id FROM " workspace" w
281+ ) wua ON wua .user_id = pek .user_id
282+ ),
283+ _personal_env_insert AS (
284+ INSERT INTO " credential" (
285+ " id" , " workspace_id" , " type" , " display_name" , " env_key" , " env_owner_user_id" ,
286+ " created_by" , " created_at" , " updated_at"
287+ )
288+ SELECT cred_id, workspace_id, ' env_personal' ::" credential_type" ,
289+ env_key, env_key, user_id, user_id, now(), now()
290+ FROM personal_env_targets
282291 ON CONFLICT DO NOTHING
283- RETURNING id, workspace_id
284292)
285293INSERT INTO " credential_member" (
286294 " id" , " credential_id" , " user_id" , " role" , " status" , " joined_at" , " invited_by" , " created_at" , " updated_at"
287295)
288296SELECT
289- ' credm_' || md5(pec . id || ' :' || c . env_owner_user_id ),
290- pec . id ,
291- c . env_owner_user_id ,
297+ ' credm_' || md5(pet . cred_id || ' :' || pet . user_id ),
298+ pet . cred_id ,
299+ pet . user_id ,
292300 ' admin' ::" credential_member_role" ,
293301 ' active' ::" credential_member_status" ,
294302 now(),
295- c . env_owner_user_id ,
303+ pet . user_id ,
296304 now(),
297305 now()
298- FROM personal_env_creds pec
299- INNER JOIN " credential" c ON c .id = pec .id
306+ FROM personal_env_targets pet
300307ON CONFLICT DO NOTHING;
0 commit comments