diff --git a/README.md b/README.md index 09400d8..1127416 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ # Keboola PHP Component -[![Build Status](https://travis-ci.com/keboola/php-component.svg?branch=master)](https://travis-ci.com/keboola/php-component) -[![Code Climate](https://codeclimate.com/github/keboola/php-component/badges/gpa.svg)](https://codeclimate.com/github/keboola/php-component) - General library for php component running in KBC. The library provides function related to [Docker Runner](https://github.com/keboola/docker-bundle). ## Installation @@ -48,7 +45,8 @@ class Component extends \Keboola\Component\BaseComponent 'data.csv', (new OutTableManifestOptions()) ->setPrimaryKeyColumns(['id']) - ->setDestination('out.report') + ->setDestination('out.report'), + true // legacy manifest format flag ); } diff --git a/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php b/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php index e00bd77..d04cd53 100644 --- a/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php +++ b/src/Manifest/ManifestManager/Options/OutTable/Serializer/LegacyManifestNormalizer.php @@ -65,6 +65,11 @@ private function normalizeSchema(ManifestOptions $object, array &$data): void $data['primary_key'][] = $schema->getName(); } + $columnMetadata[] = ['key' => 'KBC.datatype.nullable', 'value' => $schema->isNullable()]; + if ($schema->getDescription() !== null) { + $columnMetadata[] = ['key' => 'KBC.description', 'value' => $schema->getDescription()]; + } + $this->normalizeDataTypes($schema, $columnMetadata); $this->normalizeColumnMetadata($schema, $columnMetadata); @@ -79,9 +84,13 @@ private function normalizeDataTypes(ManifestOptionsSchema $schema, array &$colum { if ($schema->getDataType() !== null) { foreach ($schema->getDataType() as $backend => $typeInfo) { - foreach ($typeInfo as $key => $value) { - $metaKey = ($backend === 'base' ? 'KBC.datatype.basetype' : 'KBC.datatype.' . $key); - $columnMetadata[] = ['key' => $metaKey, 'value' => $value]; + $typeMetaKey = ($backend === 'base' ? 'KBC.datatype.basetype' : 'KBC.datatype.type'); + $columnMetadata[] = ['key' => $typeMetaKey, 'value' => $typeInfo->getType()]; + if ($typeInfo->getLength() !== null) { + $columnMetadata[] = ['key' => 'KBC.datatype.length', 'value' => $typeInfo->getLength()]; + } + if ($typeInfo->getDefault() !== null) { + $columnMetadata[] = ['key' => 'KBC.datatype.default', 'value' => $typeInfo->getDefault()]; } } } @@ -176,7 +185,7 @@ private function setSchema( $description = null; foreach ($columnMetadata as $meta) { - if (strpos($meta['key'], 'KBC.datatype.') === 0 && $meta['key'] !== 'KBC.datatype.nullable') { + if (str_starts_with($meta['key'], 'KBC.datatype.') && $meta['key'] !== 'KBC.datatype.nullable') { $this->setDataType($meta, $dataTypes, $metadataBackend); } else { $this->setMetadata($meta, $metadata, $description, $primaryKey, $isNullable); diff --git a/tests/Manifest/ManifestManagerTest.php b/tests/Manifest/ManifestManagerTest.php index 64a6403..8e88bb9 100644 --- a/tests/Manifest/ManifestManagerTest.php +++ b/tests/Manifest/ManifestManagerTest.php @@ -103,6 +103,44 @@ public function testWillLoadTableManifest(): void 'id', 'number', ], + 'column_metadata' => [ + 'id' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'number' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'name' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'description' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'created_at' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'updated_at' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + ], ]; $this->assertSame($expectedManifest, $manager->getTableManifest('people.csv')->toArray()); @@ -140,6 +178,26 @@ public function testWillLoadTableManifestWithoutCsv(): void 'id', 'number', ], + 'column_metadata' => [ + 'id' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'number' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + 'name' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + ], + ], ]; $this->assertSame($expectedManifest, $manager->getTableManifest('products')->toArray()); @@ -429,11 +487,59 @@ public function testConvertNewDataTypesManifestToLegacyArray(): void 'columns' => ['id', 'number', 'other_column'], 'column_metadata' => [ 'id' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => false, + ], + [ + 'key' => 'KBC.description', + 'value' => 'This is a primary key', + ], + [ + 'key' => 'KBC.datatype.basetype', + 'value' => 'INTEGER', + ], + [ + 'key' => 'KBC.datatype.length', + 'value' => '11', + ], + [ + 'key' => 'KBC.datatype.default', + 'value' => '123', + ], [ 'key' => 'yet.another.key', 'value' => 'Some other value', ], ], + 'number' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + [ + 'key' => 'KBC.datatype.basetype', + 'value' => 'VARCHAR', + ], + [ + 'key' => 'KBC.datatype.length', + 'value' => '255', + ], + ], + 'other_column' => [ + [ + 'key' => 'KBC.datatype.nullable', + 'value' => true, + ], + [ + 'key' => 'KBC.datatype.basetype', + 'value' => 'VARCHAR', + ], + [ + 'key' => 'KBC.datatype.length', + 'value' => '255', + ], + ], ], ];