From de4cac6da8678dde18e95e68fed450e2a0e7bfa0 Mon Sep 17 00:00:00 2001 From: Ondra Date: Sun, 4 Feb 2024 23:39:48 +0100 Subject: [PATCH] fix after snowflake implementation --- src/Application.php | 59 ++++++++++++------- src/WriterFactory.php | 4 +- .../expected-stdout | 3 +- .../expected-stdout | 3 +- .../expected-stdout | 3 +- .../incremental-write/expected-stdout | 5 +- .../reorder-columns/expected-stdout | 3 +- .../functional/run-action-row/expected-stdout | 3 +- tests/functional/run-action/expected-stdout | 6 +- .../ssh-connection-disabled/expected-stdout | 3 +- .../functional/ssh-connection/expected-stdout | 3 +- tests/phpunit/WriterFactoryTest.php | 5 +- 12 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/Application.php b/src/Application.php index 8e06904..c82a05d 100644 --- a/src/Application.php +++ b/src/Application.php @@ -47,7 +47,7 @@ protected function run(): void { $parameters = $this->getConfig()->getParameters(); $writerFactory = new WriterFactory($this->getConfig()); - $writer = $writerFactory->create($this->getLogger()); + $writer = $writerFactory->create($this->getLogger(), $this->createDatabaseConfig($parameters['db'])); if (!$this->isRowConfiguration($parameters)) { $filteredTables = array_filter($parameters['tables'], fn($table) => $table['export']); @@ -68,8 +68,12 @@ protected function run(): void */ public function testConnectionAction(): array { - $writerFactory = new WriterFactory($this->getConfig()); - $writer = $writerFactory->create($this->getLogger()); + $config = $this->getConfig(); + $writerFactory = new WriterFactory($config); + $writer = $writerFactory->create( + $this->getLogger(), + $this->createDatabaseConfig($config->getParameters()['db']), + ); try { $writer->testConnection(); } catch (Throwable $e) { @@ -86,8 +90,12 @@ public function testConnectionAction(): array */ public function getTablesInfoAction(): array { - $writerFactory = new WriterFactory($this->getConfig()); - $writer = $writerFactory->create($this->getLogger()); + $config = $this->getConfig(); + $writerFactory = new WriterFactory($config); + $writer = $writerFactory->create( + $this->getLogger(), + $this->createDatabaseConfig($config->getParameters()['db']), + ); $tables = $writer->showTables(); @@ -104,7 +112,16 @@ public function getTablesInfoAction(): array protected function createExportConfig(array $table): ExportConfig { - return ExportConfig::fromArray($table, $this->getConfig()->getInputTables()); + return ExportConfig::fromArray( + $table, + $this->getConfig()->getInputTables(), + $this->createDatabaseConfig($table['db']), + ); + } + + protected function createDatabaseConfig(array $dbParams): DatabaseConfig + { + return DatabaseConfig::fromArray($dbParams); } protected function getSyncActions(): array @@ -134,6 +151,21 @@ protected function getValidator(): Validator return new Validator($this->getLogger()); } + protected function isRowConfiguration(array $parameters): bool + { + return !isset($parameters['tables']); + } + + /** + * @throws UserException|ApplicationException + */ + protected function validateTableItems(array $table): array + { + $validator = $this->getValidator(); + $table['items'] = $validator->validateTableItems($this->getInputTablePath($table['tableId']), $table['items']); + return $table; + } + private function getInputTablePath(string $tableId): string { $inputMapping = $this->getConfig()->getInputTables(); @@ -169,19 +201,4 @@ private function checkDatabaseHost(): void $checker = $this->getValidator(); $checker->validateDatabaseHost($this->getConfig()); } - - private function isRowConfiguration(array $parameters): bool - { - return !isset($parameters['tables']); - } - - /** - * @throws UserException|ApplicationException - */ - private function validateTableItems(array $table): array - { - $validator = $this->getValidator(); - $table['items'] = $validator->validateTableItems($this->getInputTablePath($table['tableId']), $table['items']); - return $table; - } } diff --git a/src/WriterFactory.php b/src/WriterFactory.php index 65c3fc8..405a41e 100644 --- a/src/WriterFactory.php +++ b/src/WriterFactory.php @@ -19,7 +19,7 @@ public function __construct(public BaseConfig $config) /** * @throws UserException */ - public function create(LoggerInterface $logger): BaseWriter + public function create(LoggerInterface $logger, DatabaseConfig $databaseConfig): BaseWriter { $parameters = $this->config->getParameters(); $writerClass = __NAMESPACE__ . '\\Writer\\' . $parameters['writer_class']; @@ -28,7 +28,7 @@ public function create(LoggerInterface $logger): BaseWriter } /** @var BaseWriter $writer */ - $writer = new $writerClass(DatabaseConfig::fromArray($parameters['db']), $logger); + $writer = new $writerClass($databaseConfig, $logger); return $writer; } diff --git a/tests/functional/incremental-write-insert-data/expected-stdout b/tests/functional/incremental-write-insert-data/expected-stdout index 53e2f79..7638b78 100644 --- a/tests/functional/incremental-write-insert-data/expected-stdout +++ b/tests/functional/incremental-write-insert-data/expected-stdout @@ -1,5 +1,6 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Temporary Table "%s" created +Dropping table "%s" +Creating temporary table "%s" Data written to table "%s". Table "simple" has primary key, using upsert. Data upserted to table "simple". \ No newline at end of file diff --git a/tests/functional/incremental-write-update-data/expected-stdout b/tests/functional/incremental-write-update-data/expected-stdout index 53e2f79..7638b78 100644 --- a/tests/functional/incremental-write-update-data/expected-stdout +++ b/tests/functional/incremental-write-update-data/expected-stdout @@ -1,5 +1,6 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Temporary Table "%s" created +Dropping table "%s" +Creating temporary table "%s" Data written to table "%s". Table "simple" has primary key, using upsert. Data upserted to table "simple". \ No newline at end of file diff --git a/tests/functional/incremental-write-without-pk/expected-stdout b/tests/functional/incremental-write-without-pk/expected-stdout index 4a18fba..58acd3b 100644 --- a/tests/functional/incremental-write-without-pk/expected-stdout +++ b/tests/functional/incremental-write-without-pk/expected-stdout @@ -1,4 +1,5 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Temporary Table "%s" created +Dropping table "%s" +Creating temporary table "%s" Data written to table "%s". Data upserted to table "simple". \ No newline at end of file diff --git a/tests/functional/incremental-write/expected-stdout b/tests/functional/incremental-write/expected-stdout index 3063943..9780bd9 100644 --- a/tests/functional/incremental-write/expected-stdout +++ b/tests/functional/incremental-write/expected-stdout @@ -1,6 +1,7 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Temporary Table "%s" created +Dropping table "%s" +Creating temporary table "%s" Data written to table "%s". -Table "simple" created +Creating table "simple" Table "simple" has primary key, using upsert. Data upserted to table "simple". \ No newline at end of file diff --git a/tests/functional/reorder-columns/expected-stdout b/tests/functional/reorder-columns/expected-stdout index 02fe412..c8623c6 100644 --- a/tests/functional/reorder-columns/expected-stdout +++ b/tests/functional/reorder-columns/expected-stdout @@ -1,3 +1,4 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Table "simple" created +Dropping table "simple" +Creating table "simple" Data written to table "simple". \ No newline at end of file diff --git a/tests/functional/run-action-row/expected-stdout b/tests/functional/run-action-row/expected-stdout index 02fe412..c8623c6 100644 --- a/tests/functional/run-action-row/expected-stdout +++ b/tests/functional/run-action-row/expected-stdout @@ -1,3 +1,4 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Table "simple" created +Dropping table "simple" +Creating table "simple" Data written to table "simple". \ No newline at end of file diff --git a/tests/functional/run-action/expected-stdout b/tests/functional/run-action/expected-stdout index 1aa7968..03aa9dd 100644 --- a/tests/functional/run-action/expected-stdout +++ b/tests/functional/run-action/expected-stdout @@ -1,5 +1,7 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Table "encoding" created +Dropping table "encoding" +Creating table "encoding" Data written to table "encoding". -Table "simple" created +Dropping table "simple" +Creating table "simple" Data written to table "simple". \ No newline at end of file diff --git a/tests/functional/ssh-connection-disabled/expected-stdout b/tests/functional/ssh-connection-disabled/expected-stdout index 3a10a86..d6c92ae 100644 --- a/tests/functional/ssh-connection-disabled/expected-stdout +++ b/tests/functional/ssh-connection-disabled/expected-stdout @@ -1,3 +1,4 @@ Creating PDO connection to "mysql:host=mysql;port=3306;dbname=testdb;charset=utf8". -Table "simple" created +Dropping table "simple" +Creating table "simple" Data written to table "simple". diff --git a/tests/functional/ssh-connection/expected-stdout b/tests/functional/ssh-connection/expected-stdout index ca88b09..f2eef42 100644 --- a/tests/functional/ssh-connection/expected-stdout +++ b/tests/functional/ssh-connection/expected-stdout @@ -1,4 +1,5 @@ Creating SSH tunnel to 'sshproxy' on local port '33006' Creating PDO connection to "mysql:host=127.0.0.1;port=33006;dbname=testdb;charset=utf8". -Table "simple" created +Dropping table "simple" +Creating table "simple" Data written to table "simple". \ No newline at end of file diff --git a/tests/phpunit/WriterFactoryTest.php b/tests/phpunit/WriterFactoryTest.php index a536a34..3857273 100644 --- a/tests/phpunit/WriterFactoryTest.php +++ b/tests/phpunit/WriterFactoryTest.php @@ -8,6 +8,7 @@ use Keboola\DbWriter\WriterFactory; use Keboola\DbWriterConfig\Config; use Keboola\DbWriterConfig\Configuration\ActionConfigDefinition; +use Keboola\DbWriterConfig\Configuration\ValueObject\DatabaseConfig; use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; use Psr\Log\Test\TestLogger; @@ -35,7 +36,7 @@ public function testCreateWriterClass(): void ); $writerFactory = new WriterFactory($config); - $writer = $writerFactory->create(new TestLogger()); + $writer = $writerFactory->create(new TestLogger(), DatabaseConfig::fromArray($config->getParameters()['db'])); Assert::assertSame('Keboola\DbWriter\Writer\Common', get_class($writer)); } @@ -63,6 +64,6 @@ public function testUnknownWriterClass(): void $writerFactory = new WriterFactory($config); $this->expectException(UserException::class); $this->expectExceptionMessage("Writer class 'Keboola\DbWriter\Writer\Unknown' doesn't exist"); - $writerFactory->create(new TestLogger()); + $writerFactory->create(new TestLogger(), DatabaseConfig::fromArray($config->getParameters()['db'])); } }