diff --git a/src/Manifest/ManifestManager/Options/OutTable/ManifestOptions.php b/src/Manifest/ManifestManager/Options/OutTable/ManifestOptions.php index 820f18e..80c3757 100644 --- a/src/Manifest/ManifestManager/Options/OutTable/ManifestOptions.php +++ b/src/Manifest/ManifestManager/Options/OutTable/ManifestOptions.php @@ -43,6 +43,8 @@ class ManifestOptions private ?array $tableMetadata = null; + private ?array $legacyPrimaryKeys = null; + private static function getLegacySerializer(): Serializer { $normalizer = new ObjectNormalizer( @@ -78,6 +80,9 @@ private static function getNewNativeTypesSerializer(): Serializer public function toArray(bool $legacy = true): array { $serializer = $legacy ? self::getLegacySerializer() : self::getNewNativeTypesSerializer(); + if ($this->getLegacyPrimaryKeys() !== null) { + $serializer = self::getLegacySerializer(); + } return (array) $serializer->normalize($this, null, [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); } @@ -381,4 +386,15 @@ public function setDeleteWhereOperator(?string $deleteWhereOperator): ManifestOp $this->deleteWhereOperator = $deleteWhereOperator; return $this; } + + public function setLegacyPrimaryKeys(array $primaryKey): ManifestOptions + { + $this->legacyPrimaryKeys = $primaryKey; + return $this; + } + + public function getLegacyPrimaryKeys(): ?array + { + return $this->legacyPrimaryKeys; + } } diff --git a/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php b/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php index f52b85e..b283c84 100644 --- a/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php +++ b/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php @@ -19,7 +19,11 @@ public function normalize($object, ?string $format = null, array $context = []): /** @var ManifestOptions $object */ $this->normalizeBasicProperties($object, $data); $this->normalizeTableMetadata($object, $data); - $this->normalizeSchema($object, $data); + if ($object->getLegacyPrimaryKeys() !== null) { + $data['primary_key'] = $object->getLegacyPrimaryKeys(); + } else { + $this->normalizeSchema($object, $data); + } return $data; } @@ -119,7 +123,7 @@ public function denormalize($data, string $type, ?string $format = null, array $ $metadataBackend = $this->setTableMetadata($manifestOptions, $data); if (isset($data['primary_key']) && !isset($data['columns'])) { - throw new OptionsValidationException('Columns must be specified when primary key is specified.'); + $manifestOptions->setLegacyPrimaryKeys($data['primary_key']); } if (isset($data['columns'])) { diff --git a/tests/Manifest/ManifestManagerTest.php b/tests/Manifest/ManifestManagerTest.php index 37cdd56..a079f56 100644 --- a/tests/Manifest/ManifestManagerTest.php +++ b/tests/Manifest/ManifestManagerTest.php @@ -150,10 +150,21 @@ public function testManifestWithOnlyPrimaryKeysSpecified(): void { $manager = new ManifestManager(__DIR__ . '/fixtures/manifest-data-dir'); - $this->expectException(OptionsValidationException::class); - $this->expectExceptionMessage('Columns must be specified when primary key is specified.'); + $expectedManifest = [ + 'destination' => 'destination-table', + 'primary_key' => [ + 'id', + 'number', + ], + ]; + + $this->assertSame($expectedManifest, $manager->getTableManifest('onlyPrimaryKeys.csv')->toArray()); - $manager->getTableManifest('onlyPrimaryKeys.csv'); + // Test that toArray(legacy: false) force fallbacks to the same result + $this->assertSame( + $expectedManifest, + $manager->getTableManifest('onlyPrimaryKeys.csv')->toArray(false), + ); } public function testNonexistentManifestReturnsEmptyArray(): void