From 8996dc68998583071705552d24800a05bdd95003 Mon Sep 17 00:00:00 2001 From: lyf-coder <58352715+lyf-coder@users.noreply.github.com> Date: Fri, 4 Nov 2022 20:44:12 +0800 Subject: [PATCH] test(collection-manager): migration - 20221104151410-update-collections-hidden test optimize (#1040) --- ...04151410-update-collections-hidden.test.ts | 31 ++++++++++++++++--- ...0221104151410-update-collections-hidden.ts | 10 ++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts b/packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts index 871bdba8d..273b6ac32 100644 --- a/packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts @@ -1,4 +1,4 @@ -import { Database, MigrationContext, mockDatabase } from '@nocobase/database'; +import { Database, MigrationContext} from '@nocobase/database'; import UpdateCollectionsHiddenMigration from '../../migrations/20221104151410-update-collections-hidden'; import { createApp } from '../index'; @@ -25,23 +25,44 @@ describe('migration 20221104151410-update-collections-hidden test', () => { }, hidden: false, }); + await db.getCollection('collections').model.create({ + name: 'test1', + hidden: false, + }); + await db.getCollection('collections').model.create({ + name: 'test2', + options: { + isThrough: true, + }, + hidden: false, + }); - const result = await db.getRepository('collections').findOne({ + const result = await db.getRepository('collections').find({ filter: { name: 'test', }, }); - expect(result.get('hidden')).toBeFalsy(); + expect(result.length).toBe(1); + expect(result[0].get('hidden')).toBeFalsy(); const migration = new UpdateCollectionsHiddenMigration({ db } as MigrationContext); migration.context.app = app; await migration.up(); - const upResult = await db.getRepository('collections').findOne({ + + const upResult = await db.getRepository('collections').find({ filter: { name: 'test', }, }); - expect(upResult.get('hidden')).toBeTruthy(); + + expect(upResult[0].get('hidden')).toBeTruthy(); + + const hiddenResult = await db.getRepository('collections').find({ + filter: { + hidden: true, + }, + }); + expect(hiddenResult.length).toBe(1); }); }); diff --git a/packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts b/packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts index e5290acc7..6de62ccc7 100644 --- a/packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts +++ b/packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts @@ -6,10 +6,8 @@ export default class UpdateCollectionsHiddenMigration extends Migration { if (!result) { return; } - const db = this.app.db; - const transaction = await db.sequelize.transaction(); try { - await db.getRepository('collections').update({ + await this.app.db.getRepository('collections').update({ filter: { options: { autoCreate: true, @@ -19,13 +17,9 @@ export default class UpdateCollectionsHiddenMigration extends Migration { values: { hidden: true, }, - transaction, }); - - await transaction.commit(); } catch (error) { - console.log(error); - await transaction.rollback(); + console.error(error); } } }