From ec618b06c859784694eb21e878cfc3f9ddbfdee4 Mon Sep 17 00:00:00 2001 From: Junyi Date: Mon, 17 Mar 2025 01:05:26 +0800 Subject: [PATCH] fix(plugin-workflow-manaul): fix migration (#6478) * fix(plugin-workflow-manaul): fix migration * fix(plugin-workflow-manaul): fix migration --- .../20250312100513-change-table-name.test.ts | 10 +++ .../20250316181621-remove-m2m-fields.test.ts | 66 ++++++++++++++++++ .../20250312100513-change-table-name.ts | 11 ++- .../20250313133421-remove-m2m-fields.ts | 36 ---------- .../20250316181621-remove-m2m-fields.ts | 67 +++++++++++++++++++ 5 files changed, 153 insertions(+), 37 deletions(-) create mode 100644 packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250316181621-remove-m2m-fields.test.ts delete mode 100644 packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts create mode 100644 packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250316181621-remove-m2m-fields.ts diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts index f971fa7cc9..07317d2869 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250312100513-change-table-name.test.ts @@ -52,6 +52,11 @@ function matrixTest() { }); expect(r2).toBeTruthy(); + const oldTableExists = await app.db.sequelize + .getQueryInterface() + .tableExists(oldCollection.getTableNameWithSchema()); + expect(oldTableExists).toBe(false); + await app.destroy(); }); } @@ -80,6 +85,11 @@ describe('20250225175712-change-table-name.test', () => { }); expect(r2).toBeTruthy(); + const oldTableExists = await app.db.sequelize + .getQueryInterface() + .tableExists(oldCollection.getTableNameWithSchema()); + expect(oldTableExists).toBe(false); + await app.destroy(); }); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250316181621-remove-m2m-fields.test.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250316181621-remove-m2m-fields.test.ts new file mode 100644 index 0000000000..3a4ade23f3 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/migrations/20250316181621-remove-m2m-fields.test.ts @@ -0,0 +1,66 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { createMockServer } from '@nocobase/test'; +import { describe, test } from 'vitest'; +import workflowManualTasks from '../../collections/workflowManualTasks'; + +import Migration from '../../migrations/20250316181621-remove-m2m-fields'; + +describe('20250225175712-change-table-name.test', () => { + test(`old table and fields should not exist after migrated with fields removed`, async () => { + const app = await createMockServer(); + await app.version.update('1.5.0'); + const fieldCollection = app.db.collection({ + name: 'fields', + fields: [ + { + name: 'collectionName', + type: 'string', + }, + { + name: 'name', + type: 'string', + }, + ], + }); + + const oldCollection = app.db.collection({ + ...workflowManualTasks, + name: 'users_jobs', + }); + await app.db.sync(); + + const migration = new Migration({ db: app.db, app } as any); + await migration.up(); + + const oldTableExists = await app.db.sequelize + .getQueryInterface() + .tableExists(oldCollection.getTableNameWithSchema()); + expect(oldTableExists).toBe(false); + + const f1 = await fieldCollection.repository.find({ + filter: { + collectionName: 'users', + name: ['jobs', 'usersJobs'], + }, + }); + expect(f1.length).toBe(0); + + const f2 = await fieldCollection.repository.find({ + filter: { + collectionName: 'jobs', + name: ['users', 'usersJobs'], + }, + }); + expect(f2.length).toBe(0); + + await app.destroy(); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts index 2ca38f625c..6651150bab 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250312100513-change-table-name.ts @@ -24,6 +24,7 @@ export default class extends Migration { const workflowManualTasksCollection = db.collection({ ...workflowManualTasks, }); + const oldTableName = usersJobsCollection.getTableNameWithSchema(); const oldTableNameWithQuotes = usersJobsCollection.getRealTableName(true); const newTableName = workflowManualTasksCollection.getTableNameWithSchema(); @@ -33,6 +34,7 @@ export default class extends Migration { if (!exists) { return; } + const oldColumns = await queryInterface.describeTable(oldTableName); // @ts-ignore const constraints: any = await queryInterface.showConstraint(oldTableName); // PG: @@ -64,7 +66,14 @@ export default class extends Migration { async (transaction) => { const newExists = await queryInterface.tableExists(newTableName, { transaction }); if (newExists) { - await queryInterface.dropTable(newTableName, { transaction }); + // NOTE: old column status exists means not migrated + if (oldColumns.status) { + await queryInterface.dropTable(newTableName, { transaction }); + } else { + // NOTE: means this table was synchronized from collectionManager, and should not exists. + await queryInterface.dropTable(oldTableName, { transaction }); + return; + } } if (this.db.isPostgresCompatibleDialect()) { 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 deleted file mode 100644 index 7f1382f239..0000000000 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250313133421-remove-m2m-fields.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * This file is part of the NocoBase (R) project. - * Copyright (c) 2020-2024 NocoBase Co., Ltd. - * Authors: NocoBase Team. - * - * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. - * For more information, please refer to: https://www.nocobase.com/agreement. - */ - -import { Migration } from '@nocobase/server'; - -export default class extends Migration { - appVersion = '<1.7.0'; - on = 'afterLoad'; - async up() { - const { db } = this.context; - await db.sequelize.transaction(async (transaction) => { - const FieldRepo = db.getCollection('fields').repository; - await FieldRepo.destroy({ - filter: { - collectionName: 'users', - name: 'jobs', - }, - transaction, - }); - - await FieldRepo.destroy({ - filter: { - collectionName: 'jobs', - name: 'users', - }, - transaction, - }); - }); - } -} diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250316181621-remove-m2m-fields.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250316181621-remove-m2m-fields.ts new file mode 100644 index 0000000000..37120b5241 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/migrations/20250316181621-remove-m2m-fields.ts @@ -0,0 +1,67 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { Migration } from '@nocobase/server'; + +export default class extends Migration { + appVersion = '<1.7.0'; + on = 'beforeLoad'; + async up() { + const { db } = this.context; + const usersJobsCollection = db.collection({ + name: 'users_jobs', + }); + const fieldCollection = db.collection({ + name: 'fields', + autoGenId: false, + createdAt: false, + updatedAt: false, + filterTargetKey: ['collectionName', 'name'], + fields: [ + { + name: 'collectionName', + type: 'string', + }, + { + name: 'name', + type: 'string', + }, + ], + }); + + const oldTableExists = await db.sequelize + .getQueryInterface() + .tableExists(usersJobsCollection.getTableNameWithSchema()); + + await db.sequelize.transaction(async (transaction) => { + await fieldCollection.repository.destroy({ + filter: { + collectionName: 'users', + name: ['jobs', 'usersJobs'], + }, + transaction, + }); + + await fieldCollection.repository.destroy({ + filter: { + collectionName: 'jobs', + name: ['users', 'usersJobs'], + }, + transaction, + }); + + if (oldTableExists) { + await db.sequelize.getQueryInterface().dropTable(usersJobsCollection.getTableNameWithSchema(), { transaction }); + } + }); + + db.removeCollection('fields'); + db.removeCollection('users_jobs'); + } +}