fix: update sequence and foreignKey (#1116)

* fix: update sequence and foreignKey

* chore: rename migration
This commit is contained in:
ChengLei Shao 2022-11-21 18:07:58 +08:00 committed by GitHub
parent fc7da4a04c
commit c861a6884a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 20 deletions

View File

@ -17,23 +17,6 @@ describe('collection', () => {
afterEach(async () => {
await db.close();
});
test.skip('indexes', async () => {
await db.clean({ drop: true });
const collection = db.collection({
name: 'test',
fields: [
{
type: 'string',
name: 'name',
index: true,
},
],
});
collection.removeField('name');
await db.sync();
});
test('removeFromDb', async () => {
await db.clean({ drop: true });
const collection = db.collection({

View File

@ -1,5 +1,5 @@
import { Database, MigrationContext } from '@nocobase/database';
import Migrator from '../../migrations/20221117111110-update-id-to-bigint';
import Migrator from '../../migrations/20221121111110-update-id-to-bigint';
const excludeSqlite = () => (process.env.DB_DIALECT != 'sqlite' ? describe : describe.skip);

View File

@ -7,9 +7,19 @@ export default class UpdateIdToBigIntMigrator extends Migration {
await db.getCollection('fields').repository.update({
filter: {
$or: [
{
name: 'id',
type: 'integer',
},
{
options: {
isForeignKey: true,
},
type: 'integer',
},
],
},
values: {
type: 'bigInt',
},
@ -62,6 +72,29 @@ export default class UpdateIdToBigIntMigrator extends Migration {
throw err;
}
const collection = db.modelCollection.get(model);
const fieldRecord = await db.getCollection('fields').repository.findOne({
filter: {
collectionName: collection.name,
name: fieldName,
type: 'integer',
},
});
if (fieldRecord) {
fieldRecord.set('type', 'bigInt');
await fieldRecord.save();
}
if (db.inDialect('postgres')) {
const sequenceQuery = `SELECT pg_get_serial_sequence('"${model.tableName}"', '${fieldName}');`;
const [result] = await this.sequelize.query(sequenceQuery, {});
const sequenceName = result[0]['pg_get_serial_sequence'];
if (sequenceName) {
await this.sequelize.query(`ALTER SEQUENCE ${sequenceName} AS BIGINT;`, {});
}
}
this.app.log.info(`updated ${tableName}.${fieldName} to BIGINT`, tableName, fieldName);
}
};
@ -90,6 +123,7 @@ export default class UpdateIdToBigIntMigrator extends Migration {
}
const associations = model.associations;
for (const associationName of Object.keys(associations)) {
const association = associations[associationName];