From c861a6884a3b1e659d6031744ec04e549f302c53 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 21 Nov 2022 18:07:58 +0800 Subject: [PATCH] fix: update sequence and foreignKey (#1116) * fix: update sequence and foreignKey * chore: rename migration --- .../database/src/__tests__/collection.test.ts | 17 --------- .../migrations/update-id-to-bigint.test.ts | 2 +- ... => 20221121111110-update-id-to-bigint.ts} | 38 ++++++++++++++++++- 3 files changed, 37 insertions(+), 20 deletions(-) rename packages/plugins/collection-manager/src/migrations/{20221117111110-update-id-to-bigint.ts => 20221121111110-update-id-to-bigint.ts} (78%) diff --git a/packages/core/database/src/__tests__/collection.test.ts b/packages/core/database/src/__tests__/collection.test.ts index 6bf90e1c6..5c51fbdf0 100644 --- a/packages/core/database/src/__tests__/collection.test.ts +++ b/packages/core/database/src/__tests__/collection.test.ts @@ -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({ diff --git a/packages/plugins/collection-manager/src/__tests__/migrations/update-id-to-bigint.test.ts b/packages/plugins/collection-manager/src/__tests__/migrations/update-id-to-bigint.test.ts index 96d5c0075..c15c0db7f 100644 --- a/packages/plugins/collection-manager/src/__tests__/migrations/update-id-to-bigint.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/migrations/update-id-to-bigint.test.ts @@ -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); diff --git a/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts b/packages/plugins/collection-manager/src/migrations/20221121111110-update-id-to-bigint.ts similarity index 78% rename from packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts rename to packages/plugins/collection-manager/src/migrations/20221121111110-update-id-to-bigint.ts index c4d1ff5ce..7d90120b0 100644 --- a/packages/plugins/collection-manager/src/migrations/20221117111110-update-id-to-bigint.ts +++ b/packages/plugins/collection-manager/src/migrations/20221121111110-update-id-to-bigint.ts @@ -7,8 +7,18 @@ export default class UpdateIdToBigIntMigrator extends Migration { await db.getCollection('fields').repository.update({ filter: { - name: 'id', - type: 'integer', + $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];