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

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

View File

@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="chill_main_geographical_unit", uniqueConstraints={
* @ORM\UniqueConstraint(name="geographical_unit_refid", columns={"unitRefId"})
* @ORM\UniqueConstraint(name="geographical_unit_refid", columns={"layer_id", "unitRefId"})
* })
* @ORM\Entity(readOnly=true)
*/

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]);
}
);

View File

@ -17,8 +17,8 @@ final class Version20221003132620 extends AbstractMigration
public function up(Schema $schema): void
{
$this->addSql('CREATE UNIQUE INDEX geographical_unit_layer_refid ON chill_main_geographical_unit_layer (refId)');
$this->addSql('CREATE UNIQUE INDEX geographical_unit_refid ON chill_main_geographical_unit (unitRefId)');
$this->addSql('CREATE INDEX chill_internal_geographical_unit_layer_geom_idx ON chill_main_geographical_unit USING GIST (geom)');
$this->addSql('CREATE UNIQUE INDEX geographical_unit_refid ON chill_main_geographical_unit (layer_id, unitRefId)');
$this->addSql('CREATE INDEX chill_internal_geographical_unit_layer_geom_idx ON chill_main_geographical_unit USING GIST (layer_id, geom)');
}
public function down(Schema $schema): void