Skip to content

Commit

Permalink
fix after snowflake implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrajodas committed Feb 4, 2024
1 parent 4d57251 commit de4cac6
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 36 deletions.
59 changes: 38 additions & 21 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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) {
Expand All @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/WriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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".
Original file line number Diff line number Diff line change
@@ -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".
Original file line number Diff line number Diff line change
@@ -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".
5 changes: 3 additions & 2 deletions tests/functional/incremental-write/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
3 changes: 2 additions & 1 deletion tests/functional/reorder-columns/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
3 changes: 2 additions & 1 deletion tests/functional/run-action-row/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
6 changes: 4 additions & 2 deletions tests/functional/run-action/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
3 changes: 2 additions & 1 deletion tests/functional/ssh-connection-disabled/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
3 changes: 2 additions & 1 deletion tests/functional/ssh-connection/expected-stdout
Original file line number Diff line number Diff line change
@@ -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".
5 changes: 3 additions & 2 deletions tests/phpunit/WriterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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']));
}
}

0 comments on commit de4cac6

Please sign in to comment.