diff --git a/libs/model/src/models/community.ts b/libs/model/src/models/community.ts index 6dae2529ede..53fedc6ee79 100644 --- a/libs/model/src/models/community.ts +++ b/libs/model/src/models/community.ts @@ -71,7 +71,7 @@ export default ( }, base: { type: Sequelize.STRING, allowNull: false, defaultValue: '' }, ss58_prefix: { type: Sequelize.INTEGER, allowNull: true }, - icon_url: { type: Sequelize.STRING }, + icon_url: { type: Sequelize.STRING, allowNull: false }, active: { type: Sequelize.BOOLEAN, defaultValue: false }, stages_enabled: { type: Sequelize.BOOLEAN, diff --git a/libs/schemas/src/entities/community.schemas.ts b/libs/schemas/src/entities/community.schemas.ts index ac821262641..41bddb1974a 100644 --- a/libs/schemas/src/entities/community.schemas.ts +++ b/libs/schemas/src/entities/community.schemas.ts @@ -22,7 +22,7 @@ export const Community = z.object({ default_symbol: z.string().default(''), network: z.string().default(ChainNetwork.Ethereum), base: z.nativeEnum(ChainBase), - icon_url: z.string().nullish(), + icon_url: z.string().optional(), active: z.boolean(), type: z.nativeEnum(ChainType).default(ChainType.Chain), description: z.string().nullish(), 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) {}, +};