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 <chenlinxh@gmail.com>
This commit is contained in:
ChengLei Shao 2025-01-26 18:21:00 +08:00 committed by chenos
parent 554cad3353
commit 3a568b87c1
3 changed files with 82 additions and 1 deletions

View File

@ -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();
});
});

View File

@ -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();
}
}
}
}

View File

@ -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',
},