From 44479b0744ec548853fa57fe03a6aa24d8e1af52 Mon Sep 17 00:00:00 2001 From: missinglink Date: Wed, 8 Jul 2020 13:45:44 +0200 Subject: [PATCH] fix(hierarchy_fix): prevent superseded parent from replacing self-references in the hierarchy --- sqlite/fix.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlite/fix.js b/sqlite/fix.js index c5be08e..6c00b62 100644 --- a/sqlite/fix.js +++ b/sqlite/fix.js @@ -106,6 +106,7 @@ module.exports.hierarchies = (db, options) => { } // fix orphaned hierarchies + const placetype = feature.getPlacetype(feat) const hierarchies = _.get(feat, 'properties.wof:hierarchy', []) _.forEach(hierarchies, (hierarchy, branch) => { _.forEach(hierarchy, (hierarchyId, key) => { @@ -116,8 +117,15 @@ module.exports.hierarchies = (db, options) => { console.error(`${id} has an incorrect ${key}, replacing ${hierarchyId} with ${replacementKey}=${replacement.id}`) // handle the case where the placetype changed when superseded - if (key !== replacementKey) { _.unset(feat, `properties.wof:hierarchy[${branch}][${key}]`) } + if (key !== replacementKey) { + _.unset(feat, `properties.wof:hierarchy[${branch}][${key}]`) + reindex = true + } + // do not update self-references (those with the same placetype as the record itself) + if (replacement.placetype === placetype) { return } + + // update hierarchy _.set(feat, `properties.wof:hierarchy[${branch}][${replacementKey}]`, replacement.id) reindex = true }