From a0cb35ffa49255fe75d6edaddbd0df687154a21b Mon Sep 17 00:00:00 2001 From: chareice Date: Sun, 20 Nov 2022 21:27:19 +0800 Subject: [PATCH] fix: handle column not exists error --- .../20221117111110-update-id-to-bigint.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts b/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts index 2abd03585d..c4d1ff5cea 100644 --- a/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts +++ b/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts @@ -26,13 +26,12 @@ export default class UpdateIdToBigIntMigrator extends Migration { const queryGenerator = queryInterface.queryGenerator as any; const updateToBigInt = async (model, fieldName) => { + let sql; + const tableName = model.tableName; if (model.rawAttributes[fieldName].type instanceof DataTypes.INTEGER) { if (db.inDialect('postgres')) { - await this.sequelize.query( - `ALTER TABLE "${tableName}" ALTER COLUMN "${fieldName}" SET DATA TYPE BIGINT;`, - {}, - ); + sql = `ALTER TABLE "${tableName}" ALTER COLUMN "${fieldName}" SET DATA TYPE BIGINT;`; } else if (db.inDialect('mysql')) { const dataTypeOrOptions = model.rawAttributes[fieldName]; const attributeName = fieldName; @@ -49,9 +48,18 @@ export default class UpdateIdToBigIntMigrator extends Migration { table: tableName, }, ); - const sql = queryGenerator.changeColumnQuery(tableName, query); + sql = queryGenerator.changeColumnQuery(tableName, query); - await this.sequelize.query(sql.replace(' PRIMARY KEY;', ' ;'), {}); + sql = sql.replace(' PRIMARY KEY;', ' ;'); + } + + try { + await this.sequelize.query(sql, {}); + } catch (err) { + if (err.message.includes('does not exist')) { + return; + } + throw err; } this.app.log.info(`updated ${tableName}.${fieldName} to BIGINT`, tableName, fieldName); @@ -67,6 +75,12 @@ export default class UpdateIdToBigIntMigrator extends Migration { try { const primaryKeyField = model.tableAttributes[model.primaryKeyField]; + const collection = db.modelCollection.get(model); + + if (!collection) { + continue; + } + if (primaryKeyField && primaryKeyField.primaryKey) { await updateToBigInt(model, model.primaryKeyField); }