From 3a568b87c1a55c68230815ea41b28936c7a4c81b Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 26 Jan 2025 18:21:00 +0800 Subject: [PATCH] chore: remove collection schema options (#6129) * chore: remove schema options in collections * chore: test * chore: unset schema option * chore: test * chore: test * fix: test --------- Co-authored-by: chenos --- .../migrations/remove-schema-options.test.ts | 41 +++++++++++++++++++ .../20250123000001-remove-schema-options.ts | 34 +++++++++++++++ .../repositories/collection-repository.ts | 8 +++- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/remove-schema-options.test.ts create mode 100644 packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20250123000001-remove-schema-options.ts diff --git a/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/remove-schema-options.test.ts b/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/remove-schema-options.test.ts new file mode 100644 index 0000000000..de701919fa --- /dev/null +++ b/packages/plugins/@nocobase/plugin-data-source-main/src/server/__tests__/migrations/remove-schema-options.test.ts @@ -0,0 +1,41 @@ +import { Database, MigrationContext } from '@nocobase/database'; + +import { MockServer } from '@nocobase/test'; +import { createApp } from '../index'; + +import Migrator from '../../migrations/20250123000001-remove-schema-options'; +describe.runIf(process.env.DB_DIALECT === 'postgres')('remove schema options', () => { + let app: MockServer; + let db: Database; + + beforeEach(async () => { + app = await createApp({}); + db = app.db; + }); + + afterEach(async () => { + await app.destroy(); + }); + + it('should remove schema options', async () => { + await db.getRepository('collections').create({ + values: { + name: 'test', + from: 'db2cm', + schema: db.options.schema || 'public', + }, + }); + + const migration = new Migrator({ db } as MigrationContext); + migration.context.app = app; + await migration.up(); + + const collection = await db.getRepository('collections').findOne({ + filter: { + 'options.from': 'db2cm', + }, + }); + + expect(collection?.get('schema')).toBeUndefined(); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20250123000001-remove-schema-options.ts b/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20250123000001-remove-schema-options.ts new file mode 100644 index 0000000000..4421e4a6a9 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-data-source-main/src/server/migrations/20250123000001-remove-schema-options.ts @@ -0,0 +1,34 @@ +/** + * 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. + */ + +/* istanbul ignore file -- @preserve */ + +import { Migration } from '@nocobase/server'; + +export default class extends Migration { + on = 'afterLoad'; + + async up() { + const CollectionRepository = this.context.db.getRepository('collections'); + const collections = await CollectionRepository.find({ + filter: { + 'options.from': 'db2cm', + }, + }); + + for (const collection of collections) { + const collectionSchema = collection.get('schema'); + const dbSchema = this.context.db.options.schema || 'public'; + if (collectionSchema && collectionSchema == dbSchema) { + collection.set('schema', undefined); + await collection.save(); + } + } + } +} diff --git a/packages/plugins/@nocobase/plugin-data-source-main/src/server/repositories/collection-repository.ts b/packages/plugins/@nocobase/plugin-data-source-main/src/server/repositories/collection-repository.ts index fab49bb91f..a86c384d72 100644 --- a/packages/plugins/@nocobase/plugin-data-source-main/src/server/repositories/collection-repository.ts +++ b/packages/plugins/@nocobase/plugin-data-source-main/src/server/repositories/collection-repository.ts @@ -186,9 +186,15 @@ export class CollectionRepository extends Repository { }); } + const collectionOptions = options; + + if (collectionOptions.schema && collectionOptions.schema == (this.database.options.schema || 'public')) { + delete collectionOptions.schema; + } + await this.create({ values: { - ...options, + ...collectionOptions, fields, from: 'db2cm', },