Skip to content

Commit

Permalink
fix phpstan/phpcs, implement getTables action
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrajodas committed Oct 30, 2023
1 parent f58a6d3 commit 5811506
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 184 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"tests": [
"@tests-phpunit"
],
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
"phpcs": "phpcs -n --ignore=vendor,tests --extensions=php .",
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
"phpstan": "phpstan analyse ./src ./tests --level=max --no-progress -c phpstan.neon",
"phpstan": "phpstan analyse ./src --level=max --no-progress -c phpstan.neon",
"build": [
"@phpstan",
"@phpcs",
Expand All @@ -54,5 +54,10 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require-dev": {
"keboola/coding-standard": "^15.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.4"
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
checkMissingIterableValueType: false
11 changes: 6 additions & 5 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getTablesInfoAction(): array
$writerFactory = new WriterFactory($this->getConfig());
$writer = $writerFactory->create($this->getLogger());

$tables = $writer->showTables(DatabaseConfig::fromArray($this->getConfig()->getParameters()['db']));
$tables = $writer->showTables();

$tablesInfo = [];
foreach ($tables as $tableName) {
Expand Down Expand Up @@ -131,17 +131,18 @@ private function getInputTablePath(string $tableId): string
});

if (count($filteredStorageInputMapping) === 0) {
throw new UserException(
sprintf('Table "%s" in storage input mapping cannot be found.', $tableId)
);
throw new UserException(sprintf(
'Table "%s" in storage input mapping cannot be found.',
$tableId,
));
}

$tableFromInputMapping = current($filteredStorageInputMapping);

return sprintf(
'%s/in/tables/%s',
$this->getDataDir(),
$tableFromInputMapping['destination']
$tableFromInputMapping['destination'],
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidDatabaseHostException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Keboola\DbWriter\Exception;

use Exception;
Expand Down
14 changes: 10 additions & 4 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Keboola\DbWriter;

use Keboola\CommonExceptions\UserExceptionInterface;
Expand Down Expand Up @@ -34,8 +36,8 @@ public function validateDatabaseHost(): void
sprintf(
'Hostname "%s" with port "%s" is not approved.',
$db['host'],
$db['port']
)
$db['port'],
),
);
}
}
Expand All @@ -48,10 +50,14 @@ public function validateTableItems(string $tablePath, array $items): array
}

$manifest = @json_decode((string) file_get_contents($manifestPath), true);
if (!$manifestPath) {
if (!is_array($manifest)) {
throw new ApplicationException(sprintf('Manifest "%s" is not valid JSON.', $manifestPath));
}
if (!isset($manifest['columns'])) {
throw new ApplicationException(sprintf('Manifest "%s" is missing "columns" key.', $manifestPath));
}

/** @var iterable $csvHeader */
$csvHeader = $manifest['columns'];
$reordered = [];
foreach ($csvHeader as $csvCol) {
Expand All @@ -64,4 +70,4 @@ public function validateTableItems(string $tablePath, array $items): array

return $reordered;
}
}
}
121 changes: 0 additions & 121 deletions src/Writer.php

This file was deleted.

24 changes: 15 additions & 9 deletions src/Writer/BaseWriter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Keboola\DbWriter\Writer;

use Keboola\DbWriter\Exception\SshException;
Expand All @@ -22,20 +24,16 @@ abstract class BaseWriter
*/
public function __construct(
DatabaseConfig $databaseConfig,
readonly protected LoggerInterface $logger
readonly protected LoggerInterface $logger,
) {
$databaseConfig = $this->createSshTunnel($databaseConfig);
$this->connection = $this->createConnection($databaseConfig);
$this->adapter = $this->createWriteAdapter();
}

abstract protected function createWriteAdapter(): WriteAdapter;

abstract public function getTableInfo(string $tableName): array;

abstract protected function createConnection(DatabaseConfig $databaseConfig): Connection;

abstract protected static function getAllowedTypes(): array;
abstract protected function createWriteAdapter(): WriteAdapter;

public function write(ExportConfig $exportConfig): void
{
Expand All @@ -51,6 +49,16 @@ public function testConnection(): void
$this->connection->testConnection();
}

public function showTables(): array
{
return $this->adapter->showTables();
}

public function getTableInfo(string $tableName): array
{
return $this->adapter->getTableInfo($tableName);
}

protected function writeIncremental(ExportConfig $exportConfig): void
{
// write to staging table
Expand All @@ -75,7 +83,6 @@ protected function writeFull(ExportConfig $exportConfig): void
$this->adapter->drop($exportConfig->getDbName());
$this->adapter->create($exportConfig->getDbName(), false, $exportConfig->getItems());
$this->adapter->writeData($exportConfig->getDbName(), $exportConfig->getTableFilePath());
var_dump($this->adapter->tableExists($exportConfig->getDbName()));
}

/**
Expand All @@ -87,5 +94,4 @@ protected function createSshTunnel(DatabaseConfig $databaseConfig): DatabaseConf
$sshTunnel = new SshTunnel($this->logger);
return $sshTunnel->createSshTunnel($databaseConfig);
}

}
}
22 changes: 4 additions & 18 deletions src/Writer/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

class Common extends BaseWriter
{
protected const ALLOWED_TYPES = [
'int', 'smallint', 'bigint',
'decimal', 'float', 'double',
'date', 'datetime', 'timestamp',
'char', 'varchar', 'text', 'blob',
];
/** @var PdoConnection $connection */
protected Connection $connection;

/**
* @throws UserException|PropertyNotSetException
Expand All @@ -33,7 +29,7 @@ public function createConnection(DatabaseConfig $databaseConfig): Connection
'mysql:host=%s;port=%s;dbname=%s;charset=utf8',
$databaseConfig->getHost(),
$port,
$databaseConfig->getDatabase()
$databaseConfig->getDatabase(),
);

return new PdoConnection(
Expand All @@ -45,7 +41,7 @@ public function createConnection(DatabaseConfig $databaseConfig): Connection
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
],
function (PDO $connection) use ($databaseConfig): void {
function (PDO $connection): void {
$connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$connection->exec('SET NAMES utf8;');
},
Expand All @@ -59,14 +55,4 @@ protected function createWriteAdapter(): WriteAdapter
new DefaultQueryBuilder(),
);
}

protected static function getAllowedTypes(): array
{
return self::ALLOWED_TYPES;
}

public function getTableInfo(string $tableName): array
{
// TODO: Implement getTableInfo() method.
}
}
Loading

0 comments on commit 5811506

Please sign in to comment.