Fix schema apply when deleting interrelated collections (#15072)

* Fix apply when deleting interrelated collections

* test

* fix unit test

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
Azri Kahar
2022-08-16 23:30:42 +08:00
committed by GitHub
parent 91a36120fa
commit e255bf80e4
3 changed files with 551 additions and 10 deletions

View File

@@ -84,6 +84,30 @@ export async function applySnapshot(
const deleteCollections = async (collections: CollectionDelta[]) => {
for (const { collection, diff } of collections) {
if (diff?.[0].kind === 'D') {
const relations = schema.relations.filter(
(r) => r.related_collection === collection || r.collection === collection
);
if (relations.length > 0) {
const relationsService = new RelationsService({ knex: trx, schema });
for (const relation of relations) {
try {
await relationsService.deleteOne(relation.collection, relation.field);
} catch (err) {
logger.error(
`Failed to delete collection "${collection}" due to relation "${relation.collection}.${relation.field}"`
);
throw err;
}
}
// clean up deleted relations from existing schema
schema.relations = schema.relations.filter(
(r) => r.related_collection !== collection && r.collection !== collection
);
}
await deleteCollections(getNestedCollectionsToDelete(collection));
try {