From fd7c83922853dc43d39c2b0dfbcf2ad49c63daa1 Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 15 Mar 2025 23:37:46 +0800 Subject: [PATCH] fix: migration error (#6470) * fix: migration error * fix: test case * fix(plugin-workflow-manual): fix migration * fix(plugin-workflow-manual): remove useless code * test(plugin-workflow-manual): add more case --------- Co-authored-by: mytharcher --- ... 20250312100513-change-table-name.test.ts} | 155 +++++++++++++++++- ...ts => 20250312100513-change-table-name.ts} | 22 ++- .../20250313133421-remove-m2m-fields.ts | 2 - 3 files changed, 167 insertions(+), 12 deletions(-) rename packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/{20250312100512-change-table-name.test.ts => 20250312100513-change-table-name.test.ts} (50%) rename packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/{20250312100512-change-table-name.ts => 20250312100513-change-table-name.ts} (84%) diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100512-change-table-name.test.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts similarity index 50% rename from packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100512-change-table-name.test.ts rename to packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts index af8a96f156..f971fa7cc9 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100512-change-table-name.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts @@ -10,7 +10,7 @@ import { createMockServer } from '@nocobase/test'; import { describe, test } from 'vitest'; import workflowManualTasks from '../../collections/workflowManualTasks'; -import Migration from '../../migrations/20250312100512-change-table-name'; +import Migration from '../../migrations/20250312100513-change-table-name'; const skipSqlite = process.env.DB_DIALECT === 'sqlite' ? test.skip : test; @@ -86,11 +86,6 @@ describe('20250225175712-change-table-name.test', () => { skipSqlite(`multiple primary keys`, async () => { const app = await createMockServer(); await app.version.update('1.5.0'); - // mock m2m collections - app.db.collection({ - ...workflowManualTasks, - name: 'users_jobs', - }); app.db.collection({ name: 'users', fields: [{ name: 'id', type: 'bigInt', primaryKey: true }], @@ -128,4 +123,152 @@ describe('20250225175712-change-table-name.test', () => { await app.destroy(); }); + + skipSqlite(`legacy multiple primary keys (without id)`, async () => { + const app = await createMockServer(); + await app.version.update('1.5.0'); + + app.db.collection({ + name: 'users', + fields: [ + { name: 'id', type: 'bigInt', primaryKey: true }, + { + type: 'belongsToMany', + name: 'jobs', + through: 'users_jobs', + }, + ], + }); + app.db.collection({ + name: 'jobs', + fields: [ + { name: 'id', type: 'bigInt', primaryKey: true }, + { + type: 'belongsToMany', + name: 'users', + through: 'users_jobs', + }, + ], + }); + + app.db.collection({ + name: 'users_jobs', + fields: [ + { + type: 'belongsTo', + name: 'job', + target: 'jobs', + foreignKey: 'jobId', + primaryKey: true, + }, + { + type: 'belongsTo', + name: 'user', + target: 'users', + foreignKey: 'userId', + primaryKey: true, + }, + ], + }); + + await app.db.sync(); + + const migration = new Migration({ db: app.db, app } as any); + await migration.up(); + + app.db.collection({ + ...workflowManualTasks, + }); + app.db.removeCollection('jobs'); + app.db.collection({ + name: 'jobs', + fields: [{ name: 'id', type: 'bigInt', primaryKey: true }], + }); + await app.db.sync(); + const columns = await app.db.sequelize + .getQueryInterface() + .describeTable(app.db.getCollection(workflowManualTasks.name).getTableNameWithSchema()); + const primaryKeys = Object.keys(columns).filter((c) => columns[c].primaryKey); + expect(primaryKeys.length).toBe(1); + expect(primaryKeys[0]).toBe('id'); + + await app.destroy(); + }); + + skipSqlite(`width id as primary key`, async () => { + const app = await createMockServer(); + await app.version.update('1.5.0'); + + app.db.collection({ + name: 'users_jobs', + fields: [ + { + type: 'bigInt', + name: 'id', + primaryKey: true, + autoIncrement: true, + }, + { + type: 'belongsTo', + name: 'job', + target: 'jobs', + foreignKey: 'jobId', + primaryKey: false, + }, + { + type: 'belongsTo', + name: 'user', + target: 'users', + foreignKey: 'userId', + primaryKey: false, + }, + ], + }); + + app.db.collection({ + name: 'users', + fields: [ + { name: 'id', type: 'bigInt', primaryKey: true }, + { + type: 'belongsToMany', + name: 'jobs', + through: 'users_jobs', + }, + ], + }); + app.db.collection({ + name: 'jobs', + fields: [ + { name: 'id', type: 'bigInt', primaryKey: true }, + { + type: 'belongsToMany', + name: 'users', + through: 'users_jobs', + }, + ], + }); + + await app.db.sync(); + + const migration = new Migration({ db: app.db, app } as any); + await migration.up(); + + app.db.collection({ + ...workflowManualTasks, + }); + app.db.removeCollection('jobs'); + app.db.collection({ + name: 'jobs', + fields: [{ name: 'id', type: 'bigInt', primaryKey: true }], + }); + await app.db.sync(); + const columns = await app.db.sequelize + .getQueryInterface() + .describeTable(app.db.getCollection(workflowManualTasks.name).getTableNameWithSchema()); + const primaryKeys = Object.keys(columns).filter((c) => columns[c].primaryKey); + expect(primaryKeys.length).toBe(1); + expect(primaryKeys[0]).toBe('id'); + + await app.destroy(); + }); }); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100512-change-table-name.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts similarity index 84% rename from packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100512-change-table-name.ts rename to packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts index 08e5b00aed..2ca38f625c 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100512-change-table-name.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts @@ -7,8 +7,8 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { Transaction } from 'sequelize'; import { Migration } from '@nocobase/server'; +import { Transaction } from 'sequelize'; import workflowManualTasks from '../collections/workflowManualTasks'; export default class extends Migration { @@ -88,9 +88,23 @@ export default class extends Migration { } } } else if (this.db.isMySQLCompatibleDialect()) { - await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} DROP PRIMARY KEY, ADD PRIMARY KEY (id)`, { - transaction, - }); + const idExists = await workflowManualTasksCollection.getField('id').existsInDb(); + if (!idExists) { + await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} ADD COLUMN id BIGINT;`, { + transaction, + }); + await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} DROP PRIMARY KEY`, { + transaction, + }); + await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} ADD PRIMARY KEY (id)`, { + transaction, + }); + await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} MODIFY COLUMN id BIGINT AUTO_INCREMENT`, { + transaction, + }); + } else { + console.log('------------------ id exists', idExists); + } } const indexes: any = await queryInterface.showIndex(newTableName, { transaction }); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts index 036a8336ed..7f1382f239 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts @@ -31,8 +31,6 @@ export default class extends Migration { }, transaction, }); - - db.removeCollection('fields'); }); } }