From 764003934ca45b80d94761319209c9a9ca47d42c Mon Sep 17 00:00:00 2001 From: israellund Date: Thu, 14 Nov 2024 13:42:07 -0500 Subject: [PATCH] added migration file --- .../20241114170102-icon-url-non-null.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/commonwealth/server/migrations/20241114170102-icon-url-non-null.js diff --git a/packages/commonwealth/server/migrations/20241114170102-icon-url-non-null.js b/packages/commonwealth/server/migrations/20241114170102-icon-url-non-null.js new file mode 100644 index 00000000000..f727900d6da --- /dev/null +++ b/packages/commonwealth/server/migrations/20241114170102-icon-url-non-null.js @@ -0,0 +1,42 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ + +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async (transaction) => { + await queryInterface.sequelize.query( + ` + WITH random_avatars AS ( + SELECT id, (ARRAY[ + 'https://assets.commonwealth.im/6588c98e-8c2f-4ba1-a312-0ebda16861b7.png', + 'https://assets.commonwealth.im/66422349-c5b2-4323-9f55-4750eaac5162.png', + 'https://assets.commonwealth.im/ea18afa6-158f-4fa2-b28b-1f51d88a1ad1.png', + 'https://assets.commonwealth.im/beaf8336-eb27-43e8-9d81-feaefcdb8db9.png', + 'https://assets.commonwealth.im/5a2f2012-22a5-469e-9214-a0a2d84ecbef.png', + 'https://assets.commonwealth.im/128e3b82-fd5d-4f1d-bdcc-5e23336f299f.png', + ])[floor(random() * 6 + 1)] AS avatar_url + FROM "Communities" + WHERE 'icon_url' IS NULL + OR 'icon_url' = '' + ) + UPDATE "Communities" + SET icon_url = random_avatars.avatar_url + FROM random_avatars + WHERE "Communities".id = random_avatars.id + `, + { transaction }, + ); + + await queryInterface.sequelize.query( + ` + ALTER TABLE "Communities" + ALTER COLUMN "icon_url" SET NOT NULL; + `, + { transaction }, + ); + }); + }, + + async down(queryInterface, Sequelize) {}, +};