From 864f585e5bfd419e458dfb116d2f7092a3491e61 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 25 Aug 2022 19:52:01 +0100 Subject: [PATCH] chore: Remove long deprecated database columns (#3821) * chore: Remove long deprecated database columns * test * Update 20220720221531-remove-deprecated-columns.js * fix rollback * Add guard for upgrading past v0.54.0 --- ...0220720221531-remove-deprecated-columns.js | 61 +++++++++++++++++++ server/models/Attachment.ts | 11 ---- server/test/factories.ts | 1 - 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 server/migrations/20220720221531-remove-deprecated-columns.js diff --git a/server/migrations/20220720221531-remove-deprecated-columns.js b/server/migrations/20220720221531-remove-deprecated-columns.js new file mode 100644 index 000000000..f237d9173 --- /dev/null +++ b/server/migrations/20220720221531-remove-deprecated-columns.js @@ -0,0 +1,61 @@ +'use strict'; + +module.exports = { + async up(queryInterface) { + await queryInterface.sequelize.transaction(async (transaction) => { + // This guard prevents the migration from running and destroying this + // service data if the script from v0.54.0 has not yet been run. + if (process.env.DEPLOYMENT !== "hosted") { + const [teams] = await queryInterface.sequelize.query( + `SELECT COUNT(*) FROM teams`, + { transaction } + ); + const [authenticationProviders] = await queryInterface.sequelize.query( + `SELECT COUNT(*) FROM authentication_providers`, + { transaction } + ); + + if (teams[0].count > 0 && authenticationProviders[0].count === 0) { + throw Error("Refusing to destroy deprecated columns without authentication providers"); + } + } + + await queryInterface.removeColumn("attachments", "url", { transaction }); + await queryInterface.removeColumn("users", "service", { transaction }); + await queryInterface.removeColumn("users", "serviceId", { transaction }); + await queryInterface.removeColumn("teams", "slackId", { transaction }); + await queryInterface.removeColumn("teams", "googleId", { transaction }); + }); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async (transaction) => { + await queryInterface.addColumn("attachments", "url", { + type: Sequelize.STRING(4096), + allowNull: false, + defaultValue: "", + transaction + }); + await queryInterface.addColumn("users", "service", { + type: Sequelize.STRING, + allowNull: true, + transaction + }); + await queryInterface.addColumn("users", "serviceId", { + type: Sequelize.STRING, + allowNull: true, + transaction + }); + await queryInterface.addColumn("teams", "slackId", { + type: Sequelize.STRING, + allowNull: true, + transaction + }); + await queryInterface.addColumn("teams", "googleId", { + type: Sequelize.STRING, + allowNull: true, + transaction + }); + }); + } +}; diff --git a/server/models/Attachment.ts b/server/models/Attachment.ts index 65774881b..e742a3efb 100644 --- a/server/models/Attachment.ts +++ b/server/models/Attachment.ts @@ -28,13 +28,6 @@ class Attachment extends IdModel { @Column key: string; - @Length({ - max: 4096, - msg: "url must be 4096 characters or less", - }) - @Column - url: string; - @Length({ max: 255, msg: "contentType must be 255 characters or less", @@ -69,10 +62,6 @@ class Attachment extends IdModel { return getFileByKey(this.key); } - /** - * Use this instead of url which will be deleted soon, the column is unneccessary - * and was not updated with the migraiton to the new s3 bucket. - */ get canonicalUrl() { return `${publicS3Endpoint()}/${this.key}`; } diff --git a/server/test/factories.ts b/server/test/factories.ts index f536e5e1e..426052007 100644 --- a/server/test/factories.ts +++ b/server/test/factories.ts @@ -377,7 +377,6 @@ export async function buildAttachment(overrides: Partial = {}) { count++; return Attachment.create({ key: `uploads/key/to/file ${count}.png`, - url: `https://redirect.url.com/uploads/key/to/file${count}.png`, contentType: "image/png", size: 100, acl: "public-read",