Skip to content

Commit 9f8f43e

Browse files
authored
Merge pull request #91 from keboola/adamvyborny-PST-1966
Component should fail when there are set PKs for non-existing columns in manifest
2 parents 434c136 + 90b5722 commit 9f8f43e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"phpcs": "phpcs --extensions=php src tests example",
5050
"phpcbf": "phpcbf --extensions=php src tests example",
5151
"build": [
52-
"@phplint",
5352
"@phpcs",
5453
"@phpstan",
5554
"@tests"

src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Component\Manifest\ManifestManager\Options\OptionsValidationException;
88
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptions;
99
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptionsSchema;
10+
use Keboola\Component\UserException;
1011
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1112
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1213

@@ -181,6 +182,7 @@ private function setSchema(
181182
): void {
182183
$schema = [];
183184

185+
$primaryKeysSet = [];
184186
foreach ($data['columns'] as $columnName) {
185187
$columnMetadata = $data['column_metadata'][$columnName] ?? [];
186188
$dataTypes = [];
@@ -198,6 +200,9 @@ private function setSchema(
198200
}
199201

200202
$isPK = $primaryKey ?: (isset($data['primary_key']) && in_array($columnName, $data['primary_key']));
203+
if ($isPK) {
204+
$primaryKeysSet[] = $columnName;
205+
}
201206
$schema[] = new ManifestOptionsSchema(
202207
$columnName,
203208
$dataTypes,
@@ -208,6 +213,13 @@ private function setSchema(
208213
);
209214
}
210215

216+
if (isset($data['primary_key']) && count($primaryKeysSet) !== count($data['primary_key'])) {
217+
throw new UserException(sprintf(
218+
'Primary keys do not match columns. Missing columns: %s',
219+
implode(', ', array_diff($data['primary_key'], $primaryKeysSet)),
220+
));
221+
}
222+
211223
$manifestOptions->setSchema($schema);
212224
}
213225

tests/Manifest/ManifestManager/Options/OutTableManifestOptionsTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Keboola\Component\Manifest\ManifestManager\Options\OptionsValidationException;
88
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptions;
99
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptionsSchema;
10+
use Keboola\Component\UserException;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class OutTableManifestOptionsTest extends TestCase
@@ -182,6 +183,30 @@ public function validNamesProvider(): array
182183
];
183184
}
184185

186+
public function testFromArrayWithNonExistingPrimaryKey(): void
187+
{
188+
$this->expectException(UserException::class);
189+
$this->expectExceptionMessage('Primary keys do not match columns. Missing columns: number');
190+
191+
ManifestOptions::fromArray([
192+
'destination' => 'my.table',
193+
'columns' => ['id', 'number #', 'other_column'],
194+
'incremental' => true,
195+
'primary_key' => ['id', 'number'],
196+
]);
197+
198+
$this->expectException(UserException::class);
199+
$this->expectExceptionMessage('Primary keys do not match columns. ' .
200+
'Missing columns: non-existing-column-1, non-existing-column-2');
201+
202+
ManifestOptions::fromArray([
203+
'destination' => 'my.table',
204+
'columns' => ['id', 'number', 'other_column'],
205+
'incremental' => true,
206+
'primary_key' => ['id', 'non-existing-column-1', 'non-existing-column-2'],
207+
]);
208+
}
209+
185210
/**
186211
* @dataProvider provideInvalidOptions
187212
*/

0 commit comments

Comments
 (0)