Fixed: take layer into account for unicity of geographical unit's keys

This commit is contained in:
2022-10-03 17:19:11 +02:00
parent 9c3ac72426
commit 65f6712a15
3 changed files with 13 additions and 13 deletions

View File

@@ -137,11 +137,6 @@ final class GeographicalUnitBaseImporter
$this->cachingStatements[$forNumber] = $this->defaultConnection->prepare($sql);
}
$this->logger->debug(self::LOG_PREFIX . ' inserting pending addresses', [
'number' => $forNumber,
'first' => $this->waitingForInsert[0] ?? null,
]);
$statement = $this->cachingStatements[$forNumber];
try {
$i = 0;
@@ -215,7 +210,7 @@ final class GeographicalUnitBaseImporter
layer.id,
unitKey
FROM geographical_unit_temp JOIN chill_main_geographical_unit_layer AS layer ON layer.refid = layerKey
ON CONFLICT (unitrefid)
ON CONFLICT (layer_id, unitrefid)
DO UPDATE
SET geom = EXCLUDED.geom, unitname = EXCLUDED.unitname
");
@@ -223,10 +218,15 @@ final class GeographicalUnitBaseImporter
//3) Delete units
$this->logger->info(self::LOG_PREFIX . 'soft delete adresses');
$affected = $this->defaultConnection->executeStatement('DELETE FROM chill_main_geographical_unit
WHERE
unitrefid NOT IN (SELECT distinct unitKey FROM geographical_unit_temp)
');
$affected = $this->defaultConnection->executeStatement('WITH to_delete AS (
SELECT cmgu.id
FROM chill_main_geographical_unit AS cmgu
JOIN chill_main_geographical_unit_layer AS cmgul ON cmgul.id = cmgu.layer_id
JOIN geographical_unit_temp AS gut ON cmgul.refid = gut.layerKey AND cmgu.unitrefid = gut.unitKey
)
DELETE FROM chill_main_geographical_unit
WHERE id NOT IN (SELECT id FROM to_delete)
');
$this->logger->info(self::LOG_PREFIX . 'addresses deleted', ['deleted' => $affected]);
}
);