fix: update inherited collection performance issue (#3070)

This commit is contained in:
ChengLei Shao 2023-11-21 21:36:08 +08:00 committed by GitHub
parent 19bad669af
commit c81fb46071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 126 additions and 62 deletions

View File

@ -0,0 +1,62 @@
import { Database, mockDatabase } from '@nocobase/database';
describe('sync', () => {
let db: Database;
beforeEach(async () => {
db = mockDatabase({
logging: console.log,
});
await db.clean({ drop: true });
});
afterEach(async () => {
await db.close();
});
it('should not sync id fields when inherits not changed', async () => {
if (!db.inDialect('postgres')) {
return;
}
const Parent = db.collection({
name: 'parent',
fields: [
{
type: 'string',
name: 'name',
},
],
});
const fn = jest.fn();
const Child = db.collection({
name: 'child',
inherits: ['parent'],
fields: [
{
type: 'string',
name: 'name',
},
],
});
expect(await Child.existsInDb()).toBeFalsy();
await db.sync();
expect(await Child.existsInDb()).toBeTruthy();
Child.setField('age', {
type: 'integer',
});
await db.sync({});
const tableColumns = await db.sequelize.getQueryInterface().describeTable(Child.getTableNameWithSchema());
expect(tableColumns).toHaveProperty('age');
});
});

View File

@ -20,22 +20,23 @@ export class SyncRunner {
if (!parents) {
throw new Error(
`Inherit model ${
inheritedCollection.name
} can't be created without parents, parents option is ${lodash
`Inherit model ${inheritedCollection.name} can't be created without parents, parents option is ${lodash
.castArray(inheritedCollection.options.inherits)
.join(', ')}`,
);
}
const tableName = inheritedCollection.getTableNameWithSchema();
const attributes = model.tableAttributes;
const childAttributes = lodash.pickBy(attributes, (value) => {
return !value.inherit;
});
if (
!(await inheritedCollection.existsInDb({
transaction,
}))
) {
let maxSequenceVal = 0;
let maxSequenceName;
@ -122,6 +123,7 @@ export class SyncRunner {
);
}
}
}
if (options.alter) {
const columns = await queryInterface.describeTable(tableName, options);