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) {
|
if (!parents) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Inherit model ${
|
`Inherit model ${inheritedCollection.name} can't be created without parents, parents option is ${lodash
|
||||||
inheritedCollection.name
|
|
||||||
} can't be created without parents, parents option is ${lodash
|
|
||||||
.castArray(inheritedCollection.options.inherits)
|
.castArray(inheritedCollection.options.inherits)
|
||||||
.join(', ')}`,
|
.join(', ')}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tableName = inheritedCollection.getTableNameWithSchema();
|
const tableName = inheritedCollection.getTableNameWithSchema();
|
||||||
|
|
||||||
const attributes = model.tableAttributes;
|
const attributes = model.tableAttributes;
|
||||||
|
|
||||||
const childAttributes = lodash.pickBy(attributes, (value) => {
|
const childAttributes = lodash.pickBy(attributes, (value) => {
|
||||||
return !value.inherit;
|
return !value.inherit;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!(await inheritedCollection.existsInDb({
|
||||||
|
transaction,
|
||||||
|
}))
|
||||||
|
) {
|
||||||
let maxSequenceVal = 0;
|
let maxSequenceVal = 0;
|
||||||
let maxSequenceName;
|
let maxSequenceName;
|
||||||
|
|
||||||
@ -122,6 +123,7 @@ export class SyncRunner {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (options.alter) {
|
if (options.alter) {
|
||||||
const columns = await queryInterface.describeTable(tableName, options);
|
const columns = await queryInterface.describeTable(tableName, options);
|
||||||
|
Loading…
Reference in New Issue
Block a user