mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 05:25:52 +00:00
fix(tree): issue of creating path collection for inheritance tree collection (#5486)
This commit is contained in:
parent
749b28cef3
commit
475e219943
@ -10,6 +10,7 @@
|
|||||||
import { Repository } from '@nocobase/database';
|
import { Repository } from '@nocobase/database';
|
||||||
import { MockDatabase, MockServer, createMockServer } from '@nocobase/test';
|
import { MockDatabase, MockServer, createMockServer } from '@nocobase/test';
|
||||||
import Migration from '../migrations/20240802141435-collection-tree';
|
import Migration from '../migrations/20240802141435-collection-tree';
|
||||||
|
import { isPg } from '@nocobase/test';
|
||||||
|
|
||||||
describe('tree collection sync', async () => {
|
describe('tree collection sync', async () => {
|
||||||
let app: MockServer;
|
let app: MockServer;
|
||||||
@ -51,6 +52,45 @@ describe('tree collection sync', async () => {
|
|||||||
expect(pathCollection).toBeTruthy();
|
expect(pathCollection).toBeTruthy();
|
||||||
expect(await pathCollection.existsInDb()).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', () => {
|
describe('collection tree migrate test', () => {
|
||||||
|
@ -16,16 +16,20 @@ export default class extends Migration {
|
|||||||
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
|
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
|
||||||
appVersion = '<=1.3.0-beta';
|
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() {
|
async up() {
|
||||||
await this.db.sequelize.transaction(async (transaction) => {
|
await this.db.sequelize.transaction(async (transaction) => {
|
||||||
const treeCollections = await this.app.db.getRepository('collections').find({
|
const treeCollections = await this.getTreeCollections({ transaction });
|
||||||
appends: ['fields'],
|
|
||||||
filter: {
|
|
||||||
'options.tree': 'adjacencyList',
|
|
||||||
},
|
|
||||||
transaction,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const treeCollection of treeCollections) {
|
for (const treeCollection of treeCollections) {
|
||||||
const name = `main_${treeCollection.name}_path`;
|
const name = `main_${treeCollection.name}_path`;
|
||||||
const collectionOptions = {
|
const collectionOptions = {
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user