Merge branch 'main' into next

This commit is contained in:
nocobase[bot] 2024-10-22 10:29:37 +00:00
commit ce6664d64e
3 changed files with 78 additions and 8 deletions

View File

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

View File

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

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