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 () => { afterEach(async () => {
await db.close(); 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 () => { test('removeFromDb', async () => {
await db.clean({ drop: true }); await db.clean({ drop: true });
const collection = db.collection({ const collection = db.collection({

View File

@ -1,5 +1,5 @@
import { Database, MigrationContext } from '@nocobase/database'; 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); const excludeSqlite = () => (process.env.DB_DIALECT != 'sqlite' ? describe : describe.skip);

View File

@ -7,8 +7,18 @@ export default class UpdateIdToBigIntMigrator extends Migration {
await db.getCollection('fields').repository.update({ await db.getCollection('fields').repository.update({
filter: { filter: {
name: 'id', $or: [
type: 'integer', {
name: 'id',
type: 'integer',
},
{
options: {
isForeignKey: true,
},
type: 'integer',
},
],
}, },
values: { values: {
type: 'bigInt', type: 'bigInt',
@ -62,6 +72,29 @@ export default class UpdateIdToBigIntMigrator extends Migration {
throw err; 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); 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; const associations = model.associations;
for (const associationName of Object.keys(associations)) { for (const associationName of Object.keys(associations)) {
const association = associations[associationName]; const association = associations[associationName];