fix: update to bigint test

This commit is contained in:
Chareice 2023-02-14 20:15:32 +08:00
parent 1589d11e82
commit ee3db22c8a
3 changed files with 12 additions and 11 deletions

View File

@ -97,7 +97,7 @@ describe('collections repository', () => {
const testCollection = app.db.getCollection('test'); const testCollection = app.db.getCollection('test');
const tableInfo = await app.db.sequelize.getQueryInterface().describeTable(testCollection.model.tableName); const tableInfo = await app.db.sequelize.getQueryInterface().describeTable(testCollection.addSchemaTableName());
expect(tableInfo['field']).toBeDefined(); expect(tableInfo['field']).toBeDefined();
}); });

View File

@ -68,17 +68,16 @@ excludeSqlite()('update id to bigint test', () => {
await db.sync(); await db.sync();
const assertBigInt = async (collectionName, fieldName) => { const assertBigInt = async (collectionName, fieldName) => {
const tableInfo = await db.sequelize const tableName = db.getCollection(collectionName)
.getQueryInterface() ? db.getCollection(collectionName).addSchemaTableName()
.describeTable( : collectionName;
db.getCollection(collectionName) ? db.getCollection(collectionName).model.tableName : collectionName,
); const tableInfo = await db.sequelize.getQueryInterface().describeTable(tableName);
if (db.options.underscored) { if (db.options.underscored) {
fieldName = lodash.snakeCase(fieldName); fieldName = lodash.snakeCase(fieldName);
} }
console.log(`${collectionName}, ${fieldName}`, tableInfo[fieldName].type);
expect(tableInfo[fieldName].type).toBe('BIGINT'); expect(tableInfo[fieldName].type).toBe('BIGINT');
}; };
@ -92,7 +91,7 @@ excludeSqlite()('update id to bigint test', () => {
let usersTableInfo = await db.sequelize let usersTableInfo = await db.sequelize
.getQueryInterface() .getQueryInterface()
.describeTable(db.getCollection('users').model.tableName); .describeTable(db.getCollection('users').addSchemaTableName());
assertInteger(usersTableInfo.id.type); assertInteger(usersTableInfo.id.type);

View File

@ -34,6 +34,8 @@ export default class UpdateIdToBigIntMigrator extends Migration {
let sql; let sql;
const tableName = model.tableName; const tableName = model.tableName;
const addSchemaTableName = db.utils.addSchema(tableName);
const quoteTableName = db.utils.quoteTable(addSchemaTableName);
const collection = db.modelCollection.get(model); const collection = db.modelCollection.get(model);
@ -52,7 +54,7 @@ export default class UpdateIdToBigIntMigrator extends Migration {
if (model.rawAttributes[fieldName].type instanceof DataTypes.INTEGER) { if (model.rawAttributes[fieldName].type instanceof DataTypes.INTEGER) {
if (db.inDialect('postgres')) { if (db.inDialect('postgres')) {
sql = `ALTER TABLE "${tableName}" ALTER COLUMN "${columnName}" SET DATA TYPE BIGINT;`; sql = `ALTER TABLE ${quoteTableName} ALTER COLUMN "${columnName}" SET DATA TYPE BIGINT;`;
} else if (db.inDialect('mysql')) { } else if (db.inDialect('mysql')) {
const dataTypeOrOptions = model.rawAttributes[fieldName]; const dataTypeOrOptions = model.rawAttributes[fieldName];
const attributeName = fieldName; const attributeName = fieldName;
@ -70,7 +72,7 @@ export default class UpdateIdToBigIntMigrator extends Migration {
}, },
); );
sql = queryGenerator.changeColumnQuery(tableName, query); sql = queryGenerator.changeColumnQuery(addSchemaTableName, query);
sql = sql.replace(' PRIMARY KEY;', ' ;'); sql = sql.replace(' PRIMARY KEY;', ' ;');
} }
@ -85,7 +87,7 @@ export default class UpdateIdToBigIntMigrator extends Migration {
} }
if (db.inDialect('postgres')) { if (db.inDialect('postgres')) {
const sequenceQuery = `SELECT pg_get_serial_sequence('"${model.tableName}"', '${columnName}');`; const sequenceQuery = `SELECT pg_get_serial_sequence('${quoteTableName}', '${columnName}');`;
const [result] = await this.sequelize.query(sequenceQuery, {}); const [result] = await this.sequelize.query(sequenceQuery, {});
const sequenceName = result[0]['pg_get_serial_sequence']; const sequenceName = result[0]['pg_get_serial_sequence'];