Skip to content

Commit

Permalink
chore: Move avatars using cursor (calcom#14257)
Browse files Browse the repository at this point in the history
Co-authored-by: Udit Takkar <[email protected]>
  • Loading branch information
emrysal and Udit-takkar authored Apr 2, 2024
1 parent b967861 commit 2aada9a
Showing 1 changed file with 49 additions and 0 deletions.
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;

0 comments on commit 2aada9a

Please sign in to comment.