fix: update inherited collection performance issue (#3070)
This commit is contained in:
parent
19bad669af
commit
c81fb46071
62
packages/core/database/src/__tests__/sync.test.ts
Normal file
62
packages/core/database/src/__tests__/sync.test.ts
Normal 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');
|
||||
});
|
||||
});
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user