From dd20951323dd2846bc85286e8f480b324c8cda68 Mon Sep 17 00:00:00 2001 From: lyf-coder <58352715+lyf-coder@users.noreply.github.com> Date: Fri, 4 Nov 2022 17:17:35 +0800 Subject: [PATCH] chore(collection-manager): migration - 20221104151410-update-collections-hidden (#1039) --- ...04151410-update-collections-hidden.test.ts | 47 +++++++++++++++++++ ...0221104151410-update-collections-hidden.ts | 31 ++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts create mode 100644 packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts 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 new file mode 100644 index 0000000000..871bdba8db --- /dev/null +++ b/packages/plugins/collection-manager/src/__tests__/migrations/20221104151410-update-collections-hidden.test.ts @@ -0,0 +1,47 @@ +import { Database, MigrationContext, mockDatabase } from '@nocobase/database'; +import UpdateCollectionsHiddenMigration from '../../migrations/20221104151410-update-collections-hidden'; + +import { createApp } from '../index'; +import { MockServer } from '@nocobase/test'; + +describe('migration 20221104151410-update-collections-hidden test', () => { + let app: MockServer; + let db: Database; + + beforeEach(async () => { + app = await createApp(); + db = app.db; + }); + + afterEach(async () => { + await app.destroy(); + }); + it('20221104151410-update-collections-hidden up method test', async () => { + await db.getCollection('collections').model.create({ + name: 'test', + options: { + autoCreate: true, + isThrough: true, + }, + hidden: false, + }); + + const result = await db.getRepository('collections').findOne({ + filter: { + name: 'test', + }, + }); + expect(result.get('hidden')).toBeFalsy(); + + const migration = new UpdateCollectionsHiddenMigration({ db } as MigrationContext); + migration.context.app = app; + await migration.up(); + + const upResult = await db.getRepository('collections').findOne({ + filter: { + name: 'test', + }, + }); + expect(upResult.get('hidden')).toBeTruthy(); + }); +}); 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 new file mode 100644 index 0000000000..e5290acc77 --- /dev/null +++ b/packages/plugins/collection-manager/src/migrations/20221104151410-update-collections-hidden.ts @@ -0,0 +1,31 @@ +import { Migration } from '@nocobase/server'; + +export default class UpdateCollectionsHiddenMigration extends Migration { + async up() { + const result = await this.app.version.satisfies('<=0.8.0-alpha.9'); + if (!result) { + return; + } + const db = this.app.db; + const transaction = await db.sequelize.transaction(); + try { + await db.getRepository('collections').update({ + filter: { + options: { + autoCreate: true, + isThrough: true, + }, + }, + values: { + hidden: true, + }, + transaction, + }); + + await transaction.commit(); + } catch (error) { + console.log(error); + await transaction.rollback(); + } + } +}