diff --git a/packages/plugins/@nocobase/plugin-collection-tree/src/server/__tests__/sync.test.ts b/packages/plugins/@nocobase/plugin-collection-tree/src/server/__tests__/sync.test.ts index 2291fc041c..3a1dfac150 100644 --- a/packages/plugins/@nocobase/plugin-collection-tree/src/server/__tests__/sync.test.ts +++ b/packages/plugins/@nocobase/plugin-collection-tree/src/server/__tests__/sync.test.ts @@ -10,6 +10,7 @@ import { Repository } from '@nocobase/database'; import { MockDatabase, MockServer, createMockServer } from '@nocobase/test'; import Migration from '../migrations/20240802141435-collection-tree'; +import { isPg } from '@nocobase/test'; describe('tree collection sync', async () => { let app: MockServer; @@ -51,6 +52,45 @@ describe('tree collection sync', async () => { expect(pathCollection).toBeTruthy(); expect(await pathCollection.existsInDb()).toBeTruthy(); }); + + it.runIf(isPg())('should create path collection when creating inherit tree collection', async () => { + const root = db.collection({ + name: 'root', + fields: [ + { name: 'name', type: 'string' }, + { + name: 'bs', + type: 'hasMany', + target: 'b', + foreignKey: 'root_id', + }, + ], + }); + await root.sync(); + + const collection = db.collection({ + name: 'test_tree', + tree: 'adjacency-list', + inherits: ['root'], + fields: [ + { + type: 'belongsTo', + name: 'parent', + treeParent: true, + }, + { + type: 'hasMany', + name: 'children', + treeChildren: true, + }, + ], + }); + await collection.sync(); + const name = `main_${collection.name}_path`; + const pathCollection = db.getCollection(name); + expect(pathCollection).toBeTruthy(); + expect(await pathCollection.existsInDb()).toBeTruthy(); + }); }); describe('collection tree migrate test', () => { diff --git a/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20240802141435-collection-tree.ts b/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20240802141435-collection-tree.ts index 480ed9651c..2eee5b68a4 100644 --- a/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20240802141435-collection-tree.ts +++ b/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20240802141435-collection-tree.ts @@ -16,16 +16,20 @@ export default class extends Migration { on = 'afterLoad'; // 'beforeLoad' or 'afterLoad' appVersion = '<=1.3.0-beta'; + async getTreeCollections({ transaction }) { + const treeCollections = await this.app.db.getRepository('collections').find({ + appends: ['fields'], + filter: { + 'options.tree': 'adjacencyList', + }, + transaction, + }); + return treeCollections; + } + async up() { await this.db.sequelize.transaction(async (transaction) => { - const treeCollections = await this.app.db.getRepository('collections').find({ - appends: ['fields'], - filter: { - 'options.tree': 'adjacencyList', - }, - transaction, - }); - + const treeCollections = await this.getTreeCollections({ transaction }); for (const treeCollection of treeCollections) { const name = `main_${treeCollection.name}_path`; const collectionOptions = { diff --git a/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20241022161700-inherit-collection-tree.ts b/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20241022161700-inherit-collection-tree.ts new file mode 100644 index 0000000000..20de58b5ba --- /dev/null +++ b/packages/plugins/@nocobase/plugin-collection-tree/src/server/migrations/20241022161700-inherit-collection-tree.ts @@ -0,0 +1,26 @@ +/** + * 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 './20240802141435-collection-tree'; + +export default class extends Migration { + on = 'afterLoad'; // 'beforeLoad' or 'afterLoad' + appVersion = '<=1.3.36-beta'; + + async getTreeCollections({ transaction }) { + const treeCollections = await this.app.db.getRepository('collections').find({ + appends: ['fields'], + filter: { + 'options.tree': 'adjacencyList', + }, + transaction, + }); + return treeCollections.filter((collection) => collection.options.inherits?.length > 0); + } +}