Skip to content

Commit

Permalink
Remove ixExtractors when they have no templates (#7011)
Browse files Browse the repository at this point in the history
* Remove ixExtractors when they have no templates

* Bump version

* Fix lint errors

* Delete extractors without templates only if they match the extractors
being modified
  • Loading branch information
daneryl authored Jul 18, 2024
1 parent 0f96181 commit af1587b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/api/services/informationextraction/ixextractors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const Extractors = {
await model.updateMany({ _id: { $in: extractorIds } }, { $pull: { templates: templateId } });

await Suggestions.delete({ entityTemplate: templateId, extractorId: { $in: extractorIds } });
await model.delete({ _id: { $in: extractorIds }, templates: { $size: 0 } });
},
};

Expand Down
76 changes: 65 additions & 11 deletions app/api/suggestions/specs/eventListeners.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,10 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
})
);

const extractors = await testingDB.mongodb?.collection('ixextractors').find({}).toArray();
const extractors = await testingDB.mongodb
?.collection('ixextractors')
.find({ templates: { $not: { $size: 0 } } })
.toArray();

expect(extractors).toEqual([
fixturesFactory.ixExtractor('title_extractor', 'title', [
Expand All @@ -884,7 +887,6 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
extractedTemplateName,
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor2', 'extracted_property_2', []),
fixturesFactory.ixExtractor('extractor3', 'some_property', ['some_other_template']),
fixturesFactory.ixExtractor('extractor4', 'extracted_property_2_1', [
otherExtractedTemplateName,
Expand All @@ -894,7 +896,6 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
]),
fixturesFactory.ixExtractor('extractor6', 'select_property', [extractedTemplateName]),
fixturesFactory.ixExtractor('extractor7', 'multiselect_property', [extractedTemplateName]),
fixturesFactory.ixExtractor('extractor8', 'relationship_property', []),
]);

const suggestions = await testingDB.mongodb?.collection('ixsuggestions').find({}).toArray();
Expand Down Expand Up @@ -959,7 +960,10 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
})
);

const extractors = await testingDB.mongodb?.collection('ixextractors').find({}).toArray();
const extractors = await testingDB.mongodb
?.collection('ixextractors')
.find({ templates: { $not: { $size: 0 } } })
.toArray();

expect(extractors).toEqual([
fixturesFactory.ixExtractor('title_extractor', 'title', [
Expand All @@ -970,7 +974,6 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
extractedTemplateName,
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor2', 'extracted_property_2', []),
fixturesFactory.ixExtractor('extractor3', 'some_property', ['some_other_template']),
fixturesFactory.ixExtractor('extractor4', 'extracted_property_2_1', [
otherExtractedTemplateName,
Expand All @@ -979,7 +982,6 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor6', 'select_property', [extractedTemplateName]),
fixturesFactory.ixExtractor('extractor7', 'multiselect_property', []),
fixturesFactory.ixExtractor('extractor8', 'relationship_property', [extractedTemplateName]),
]);

Expand Down Expand Up @@ -1012,6 +1014,45 @@ describe(`On ${TemplateUpdatedEvent.name}`, () => {
},
]);
});

it('should delete the extractor itself if it does not contain any templates', async () => {
await applicationEventsBus.emit(
new TemplateUpdatedEvent({
before: {
_id: fixturesFactory.id(extractedTemplateName),
name: extractedTemplateName,
properties: [
fixturesFactory.property('not_extracted_property_1', propertyTypes.text),
fixturesFactory.property('not_extracted_property_2', propertyTypes.numeric),
fixturesFactory.property('extracted_property_1', propertyTypes.text),
fixturesFactory.property('extracted_property_2', propertyTypes.numeric),
fixturesFactory.property('select_property', propertyTypes.select),
fixturesFactory.property('multiselect_property', propertyTypes.multiselect),
fixturesFactory.property('relationship_property', propertyTypes.relationship),
],
},
after: {
_id: fixturesFactory.id(extractedTemplateName),
name: extractedTemplateName,
properties: [
fixturesFactory.property('not_extracted_property_1', propertyTypes.text),
fixturesFactory.property('not_extracted_property_2', propertyTypes.numeric),
fixturesFactory.property('extracted_property_1', propertyTypes.text),
fixturesFactory.property('extracted_property_2_renamed', propertyTypes.numeric),
fixturesFactory.property('select_property', propertyTypes.select),
fixturesFactory.property('multiselect_property_renamed', propertyTypes.multiselect),
fixturesFactory.property('relationship_property', propertyTypes.relationship),
],
},
})
);

const extractorsWithoutTemplates = await testingDB.mongodb
?.collection('ixextractors')
.countDocuments({ templates: { $size: 0 } });

expect(extractorsWithoutTemplates).toEqual(0);
});
});

describe(`On ${TemplateDeletedEvent.name}`, () => {
Expand All @@ -1022,27 +1063,40 @@ describe(`On ${TemplateDeletedEvent.name}`, () => {
})
);

const extractors = await testingDB.mongodb?.collection('ixextractors').find({}).toArray();
const extractors = await testingDB.mongodb
?.collection('ixextractors')
.find({ templates: { $not: { $size: 0 } } })
.toArray();

expect(extractors).toEqual([
fixturesFactory.ixExtractor('title_extractor', 'title', [otherExtractedTemplateName]),
fixturesFactory.ixExtractor('extractor1', 'extracted_property_1', [
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor2', 'extracted_property_2', []),
fixturesFactory.ixExtractor('extractor3', 'some_property', ['some_other_template']),
fixturesFactory.ixExtractor('extractor4', 'extracted_property_2_1', [
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor5', 'extracted_property_2_2', [
otherExtractedTemplateName,
]),
fixturesFactory.ixExtractor('extractor6', 'select_property', []),
fixturesFactory.ixExtractor('extractor7', 'multiselect_property', []),
fixturesFactory.ixExtractor('extractor8', 'relationship_property', []),
]);
});

it('should delete the extractor itself if it does not contain any templates', async () => {
await applicationEventsBus.emit(
new TemplateDeletedEvent({
templateId: fixturesFactory.id(extractedTemplateName).toString(),
})
);

const extractorsWithoutTemplates = await testingDB.mongodb
?.collection('ixextractors')
.countDocuments({ templates: { $size: 0 } });

expect(extractorsWithoutTemplates).toEqual(0);
});

it('should delete the suggestions related to the template', async () => {
await applicationEventsBus.emit(
new TemplateDeletedEvent({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.175.1",
"version": "1.175.2",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit af1587b

Please sign in to comment.