Skip to content

Commit

Permalink
Merge pull request #9676 from hicommonwealth/tim/fix-avatar-migration
Browse files Browse the repository at this point in the history
Fix avatar migration + image urls
  • Loading branch information
ilijabojanovic authored Oct 24, 2024
2 parents ba1fd43 + 7f72da0 commit 10acb49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
12 changes: 6 additions & 6 deletions libs/model/src/utils/defaultAvatar.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const defaultAvatars: string[] = [
'https://s3.us-east-1.amazonaws.com/local.assets/fb3289b0-38cb-4883-908b-7af0c1626ece.png',
'https://s3.us-east-1.amazonaws.com/local.assets/794bb7a3-17d7-407a-b52e-2987501221b5.png',
'https://s3.us-east-1.amazonaws.com/local.assets/181e25ad-ce08-427d-8d3a-d290af3be44b.png',
'https://s3.us-east-1.amazonaws.com/local.assets/9f40b221-e2c7-4052-a7de-e580222baaa9.png',
'https://s3.us-east-1.amazonaws.com/local.assets/ef919936-8554-42e5-8590-118e8cb68101.png',
'https://s3.us-east-1.amazonaws.com/local.assets/0847e7f5-4d96-4406-8f30-c3082fa2f27c.png',
'https://assets.commonwealth.im/fb3289b0-38cb-4883-908b-7af0c1626ece.png',
'https://assets.commonwealth.im/794bb7a3-17d7-407a-b52e-2987501221b5.png',
'https://assets.commonwealth.im/181e25ad-ce08-427d-8d3a-d290af3be44b.png',
'https://assets.commonwealth.im/9f40b221-e2c7-4052-a7de-e580222baaa9.png',
'https://assets.commonwealth.im/ef919936-8554-42e5-8590-118e8cb68101.png',
'https://assets.commonwealth.im/0847e7f5-4d96-4406-8f30-c3082fa2f27c.png',
];

export function getRandomAvatar(): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
'use strict';

const defaultAvatars = [
'https://s3.us-east-1.amazonaws.com/local.assets/fb3289b0-38cb-4883-908b-7af0c1626ece.png',
'https://s3.us-east-1.amazonaws.com/local.assets/794bb7a3-17d7-407a-b52e-2987501221b5.png',
'https://s3.us-east-1.amazonaws.com/local.assets/181e25ad-ce08-427d-8d3a-d290af3be44b.png',
'https://s3.us-east-1.amazonaws.com/local.assets/9f40b221-e2c7-4052-a7de-e580222baaa9.png',
'https://s3.us-east-1.amazonaws.com/local.assets/ef919936-8554-42e5-8590-118e8cb68101.png',
'https://s3.us-east-1.amazonaws.com/local.assets/0847e7f5-4d96-4406-8f30-c3082fa2f27c.png',
];

function getRandomAvatar() {
const randomIndex = Math.floor(Math.random() * defaultAvatars.length);
return defaultAvatars[randomIndex];
}

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.sequelize.query(
`
UPDATE "Users"
SET "profile" = jsonb_set("profile", '{avatar_url}', '"${getRandomAvatar()}"')
WHERE ("profile" ->> 'avatar_url') IS NULL
OR "profile" ->> 'avatar_url' = ''
`,
WITH random_avatars AS (
SELECT id, (ARRAY[
'https://assets.commonwealth.im/fb3289b0-38cb-4883-908b-7af0c1626ece.png',
'https://assets.commonwealth.im/794bb7a3-17d7-407a-b52e-2987501221b5.png',
'https://assets.commonwealth.im/181e25ad-ce08-427d-8d3a-d290af3be44b.png',
'https://assets.commonwealth.im/9f40b221-e2c7-4052-a7de-e580222baaa9.png',
'https://assets.commonwealth.im/ef919936-8554-42e5-8590-118e8cb68101.png',
'https://assets.commonwealth.im/0847e7f5-4d96-4406-8f30-c3082fa2f27c.png'
])[floor(random() * 6 + 1)] AS avatar_url
FROM "Users"
WHERE ("profile" ->> 'avatar_url') IS NULL
OR "profile" ->> 'avatar_url' = ''
)
UPDATE "Users"
SET "profile" = jsonb_set("profile", '{avatar_url}', to_jsonb(random_avatars.avatar_url))
FROM random_avatars
WHERE "Users".id = random_avatars.id
`,
{ transaction },
);

Expand Down

0 comments on commit 10acb49

Please sign in to comment.