mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
fix: field history with reverse field (#2776)
This commit is contained in:
parent
0140e623a6
commit
bebe1d15e5
@ -17,10 +17,52 @@ describe('actions', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await app.cleanDb();
|
|
||||||
await app.destroy();
|
await app.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not throw error when create field with reverse field', async () => {
|
||||||
|
const agent = app.agent();
|
||||||
|
|
||||||
|
await app.db.getRepository('fieldsHistory').create({
|
||||||
|
values: {
|
||||||
|
key: 'testKey',
|
||||||
|
collectionName: 'targets',
|
||||||
|
name: 'test',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await agent.resource('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'tests',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await agent.resource('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'targets',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await agent.resource('fields').create({
|
||||||
|
values: {
|
||||||
|
type: 'hasMany',
|
||||||
|
name: 'targets',
|
||||||
|
collectionName: 'tests',
|
||||||
|
foreignKey: 'test_id',
|
||||||
|
onDelete: 'SET NULL',
|
||||||
|
target: 'targets',
|
||||||
|
interface: 'o2m',
|
||||||
|
reverseField: {
|
||||||
|
interface: 'm2o',
|
||||||
|
type: 'belongsTo',
|
||||||
|
name: 'test',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
it('fieldsHistory collectionName and name conflict between tables', async () => {
|
it('fieldsHistory collectionName and name conflict between tables', async () => {
|
||||||
const agent = app.agent();
|
const agent = app.agent();
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ export class SnapshotFieldPlugin extends Plugin {
|
|||||||
filter: {
|
filter: {
|
||||||
name: collectionDoc.name,
|
name: collectionDoc.name,
|
||||||
},
|
},
|
||||||
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existCollection) {
|
if (existCollection) {
|
||||||
@ -38,20 +39,30 @@ export class SnapshotFieldPlugin extends Plugin {
|
|||||||
|
|
||||||
this.app.db.on('collections.afterCreateWithAssociations', collectionHandler);
|
this.app.db.on('collections.afterCreateWithAssociations', collectionHandler);
|
||||||
|
|
||||||
const fieldHandler = async (model: Model, { transaction }) => {
|
const deleteField = async (field, transaction) => {
|
||||||
const fieldDoc = model.get();
|
|
||||||
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
||||||
const existField: Model = await fieldsHistoryRepository.findOne({
|
|
||||||
filter: {
|
const { name, collectionName } = field;
|
||||||
name: fieldDoc.name,
|
|
||||||
collectionName: fieldDoc.collectionName,
|
await fieldsHistoryRepository.destroy({
|
||||||
},
|
filter: { name, collectionName },
|
||||||
});
|
|
||||||
if (existField) {
|
|
||||||
await existField.destroy({
|
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const fieldHandler = async (model: Model, { transaction }) => {
|
||||||
|
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
||||||
|
|
||||||
|
const fieldDoc = model.get();
|
||||||
|
|
||||||
|
await deleteField(fieldDoc, transaction);
|
||||||
|
|
||||||
|
const reverseField = fieldDoc.reverseField;
|
||||||
|
|
||||||
|
if (reverseField) {
|
||||||
|
await deleteField(reverseField, transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
await fieldsHistoryRepository.create({
|
await fieldsHistoryRepository.create({
|
||||||
values: JSON.parse(JSON.stringify(fieldDoc)),
|
values: JSON.parse(JSON.stringify(fieldDoc)),
|
||||||
transaction,
|
transaction,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user