forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move avatars using cursor (calcom#14257)
Co-authored-by: Udit Takkar <[email protected]>
- Loading branch information
1 parent
b967861
commit 2aada9a
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/prisma/migrations/20240321143215_move_avatars_cols_to_avatar_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
DO $$ | ||
DECLARE | ||
cur CURSOR FOR SELECT "id", "avatar" FROM "users" WHERE "avatar" IS NOT NULL AND "avatar" != ''; | ||
rec RECORD; | ||
objectKey UUID; | ||
BEGIN | ||
OPEN cur; | ||
|
||
LOOP | ||
FETCH cur INTO rec; | ||
EXIT WHEN NOT FOUND; | ||
|
||
objectKey := gen_random_uuid(); | ||
|
||
BEGIN | ||
INSERT INTO "avatars" ("userId", "data", "objectKey", "teamId") | ||
VALUES (rec."id", rec."avatar", objectKey, 0) | ||
ON CONFLICT ("teamId", "userId", "isBanner") DO NOTHING; | ||
|
||
UPDATE "users" | ||
SET "avatarUrl" = '/api/avatar/' || objectKey || '.png' | ||
WHERE "id" = rec."id"; | ||
EXCEPTION WHEN UNIQUE_VIOLATION THEN | ||
-- If there's a unique violation error, do nothing. | ||
END; | ||
END LOOP; | ||
|
||
CLOSE cur; | ||
END $$; | ||
|
||
BEGIN; | ||
|
||
WITH inserted_avatars AS ( | ||
SELECT t."id", t."logo" as "data", gen_random_uuid() AS "objectKey" | ||
FROM "Team" t WHERE t."logo" IS NOT NULL AND t."logo" != '' | ||
), | ||
inserted AS ( | ||
INSERT INTO "avatars" ("teamId", "data", "objectKey", "userId") | ||
SELECT "id", "data", "objectKey", 0 AS "userId" | ||
FROM inserted_avatars | ||
ON CONFLICT ("teamId", "userId", "isBanner") DO NOTHING | ||
RETURNING "teamId", "objectKey" | ||
) | ||
UPDATE "Team" t | ||
SET "logoUrl" = '/api/avatar/' || i."objectKey" || '.png' | ||
FROM inserted i | ||
WHERE t."id" = i."teamId"; | ||
|
||
COMMIT; |