diff --git a/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php b/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php index 45a7fc54e..b4c4388b6 100644 --- a/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php +++ b/src/Bundle/ChillMainBundle/Entity/GeographicalUnit.php @@ -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) */ diff --git a/src/Bundle/ChillMainBundle/Service/Import/GeographicalUnitBaseImporter.php b/src/Bundle/ChillMainBundle/Service/Import/GeographicalUnitBaseImporter.php index 9df865a28..539e69028 100644 --- a/src/Bundle/ChillMainBundle/Service/Import/GeographicalUnitBaseImporter.php +++ b/src/Bundle/ChillMainBundle/Service/Import/GeographicalUnitBaseImporter.php @@ -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]); } ); diff --git a/src/Bundle/ChillMainBundle/migrations/Version20221003132620.php b/src/Bundle/ChillMainBundle/migrations/Version20221003132620.php index 39b01a0bc..e07885dce 100644 --- a/src/Bundle/ChillMainBundle/migrations/Version20221003132620.php +++ b/src/Bundle/ChillMainBundle/migrations/Version20221003132620.php @@ -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