fix(tree): issue of creating path collection for inheritance tree collection (#5486)

This commit is contained in:
YANG QIA 2024-10-22 17:54:34 +08:00 committed by GitHub
parent 749b28cef3
commit 475e219943
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 78 additions and 8 deletions

View File

@ -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', () => {

View File

@ -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 = {

View File

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