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\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) * @ORM\Entity(readOnly=true)
*/ */

View File

@ -137,11 +137,6 @@ final class GeographicalUnitBaseImporter
$this->cachingStatements[$forNumber] = $this->defaultConnection->prepare($sql); $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]; $statement = $this->cachingStatements[$forNumber];
try { try {
$i = 0; $i = 0;
@ -215,7 +210,7 @@ final class GeographicalUnitBaseImporter
layer.id, layer.id,
unitKey unitKey
FROM geographical_unit_temp JOIN chill_main_geographical_unit_layer AS layer ON layer.refid = layerKey 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 DO UPDATE
SET geom = EXCLUDED.geom, unitname = EXCLUDED.unitname SET geom = EXCLUDED.geom, unitname = EXCLUDED.unitname
"); ");
@ -223,10 +218,15 @@ final class GeographicalUnitBaseImporter
//3) Delete units //3) Delete units
$this->logger->info(self::LOG_PREFIX . 'soft delete adresses'); $this->logger->info(self::LOG_PREFIX . 'soft delete adresses');
$affected = $this->defaultConnection->executeStatement('DELETE FROM chill_main_geographical_unit $affected = $this->defaultConnection->executeStatement('WITH to_delete AS (
WHERE SELECT cmgu.id
unitrefid NOT IN (SELECT distinct unitKey FROM geographical_unit_temp) 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]); $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 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_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 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 (geom)'); $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 public function down(Schema $schema): void