Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ 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);
if (!isset($data['primary_key'])) {
$data['primary_key'] = [];
}
$data['primary_key'] = array_unique(array_merge($data['primary_key'], $object->getLegacyPrimaryKeys()));
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to mi dalo trochu k zamyšlení co to vlastně dělá :D

}

return $data;
Expand Down
161 changes: 161 additions & 0 deletions tests/Manifest/ManifestManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Keboola\Component\Tests\Manifest;

use Generator;
use Keboola\Component\Manifest\ManifestManager;
use Keboola\Component\Manifest\ManifestManager\Options\OptionsValidationException;
use Keboola\Component\Manifest\ManifestManager\Options\OutFileManifestOptions;
Expand Down Expand Up @@ -168,6 +169,166 @@ public function testManifestWithOnlyPrimaryKeysSpecified(): void
);
}

/**
* @dataProvider legacyPrimaryKeysDataProvider
*/
public function testManifestObjectWithExtraPrimaryKeysSpecified(
array $legacyPrimaryKeys,
array $schema,
array $expectedManifest,
): void {
$manifest = (new ManifestOptions())
->setDestination('destination-table')
->setLegacyPrimaryKeys($legacyPrimaryKeys)
->setSchema($schema);

$this->assertEqualsCanonicalizing($expectedManifest, $manifest->toArray());

// Test that toArray(legacy: false) force fallbacks to the same result
$this->assertEqualsCanonicalizing(
$expectedManifest,
$manifest->toArray(false),
);
}

public function legacyPrimaryKeysDataProvider(): Generator
{
yield 'two columns, three legacy primary keys merging with two non-legacy' => [
['id', 'number', 'name'],
[
new ManifestOptionsSchema(
'id',
[],
false,
true,
),
new ManifestOptionsSchema(
'number',
['base' => ['type' => 'INTEGER', 'default' => '0']],
false,
true,
),
],
[
'destination' => 'destination-table',
'columns' => ['id', 'number'],
'primary_key' => ['id', 'number', 'name'],
'column_metadata' => [
'id' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
],
'number' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
[
'key' => 'KBC.datatype.basetype',
'value' => 'INTEGER',
],
[
'key' => 'KBC.datatype.default',
'value' => '0',
],
],
],
],
];

yield 'two columns, one legacy primary key, two non-legacy' => [
['name'],
[
new ManifestOptionsSchema(
'id',
[],
false,
true,
),
new ManifestOptionsSchema(
'number',
['base' => ['type' => 'INTEGER', 'default' => '0']],
false,
true,
),
],
[
'destination' => 'destination-table',
'columns' => ['id', 'number'],
'primary_key' => ['id', 'number', 'name'],
'column_metadata' => [
'id' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
],
'number' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
[
'key' => 'KBC.datatype.basetype',
'value' => 'INTEGER',
],
[
'key' => 'KBC.datatype.default',
'value' => '0',
],
],
],
],
];

yield 'two columns, three legacy primary keys' => [
['id', 'number', 'name'],
[
new ManifestOptionsSchema(
'id',
[],
false,
false,
),
new ManifestOptionsSchema(
'number',
['base' => ['type' => 'INTEGER', 'default' => '0']],
false,
false,
),
],
[
'destination' => 'destination-table',
'columns' => ['id', 'number'],
'primary_key' => ['id', 'number', 'name'],
'column_metadata' => [
'id' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
],
'number' => [
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
[
'key' => 'KBC.datatype.basetype',
'value' => 'INTEGER',
],
[
'key' => 'KBC.datatype.default',
'value' => '0',
],
],
],
],
];
}

public function testNonexistentManifestReturnsEmptyArray(): void
{
$manager = new ManifestManager(__DIR__ . '/fixtures/manifest-data-dir');
Expand Down
Loading