fix: field history with reverse field (#2776)
This commit is contained in:
parent
0140e623a6
commit
bebe1d15e5
@ -17,10 +17,52 @@ describe('actions', () => {
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.cleanDb();
|
||||
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 () => {
|
||||
const agent = app.agent();
|
||||
|
||||
|
@ -16,6 +16,7 @@ export class SnapshotFieldPlugin extends Plugin {
|
||||
filter: {
|
||||
name: collectionDoc.name,
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (existCollection) {
|
||||
@ -38,20 +39,30 @@ export class SnapshotFieldPlugin extends Plugin {
|
||||
|
||||
this.app.db.on('collections.afterCreateWithAssociations', collectionHandler);
|
||||
|
||||
const fieldHandler = async (model: Model, { transaction }) => {
|
||||
const fieldDoc = model.get();
|
||||
const deleteField = async (field, transaction) => {
|
||||
const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory');
|
||||
const existField: Model = await fieldsHistoryRepository.findOne({
|
||||
filter: {
|
||||
name: fieldDoc.name,
|
||||
collectionName: fieldDoc.collectionName,
|
||||
},
|
||||
});
|
||||
if (existField) {
|
||||
await existField.destroy({
|
||||
|
||||
const { name, collectionName } = field;
|
||||
|
||||
await fieldsHistoryRepository.destroy({
|
||||
filter: { name, collectionName },
|
||||
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({
|
||||
values: JSON.parse(JSON.stringify(fieldDoc)),
|
||||
transaction,
|
||||
|
Loading…
Reference in New Issue
Block a user