Merge pull request #1110 from nocobase/fix/update-to-bigint

fix: handle column does not exist error
This commit is contained in:
ChengLei Shao 2022-11-20 21:45:42 +08:00 committed by GitHub
commit de22ae00a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}