Skip to content

Commit

Permalink
Merge pull request #10 from netbrothers-gmbh/8-warn-on-unsupported-pl…
Browse files Browse the repository at this point in the history
…atforms-instead-of-failing

Resolves #8
  • Loading branch information
netbrothers-tr authored Nov 18, 2022
2 parents 3e82537 + 7dd881b commit cfa4584
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 85 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@
"name": "Thilo Ratnaweera",
"email": "[email protected]"
}
]
],
"require-dev": {
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-deprecation-rules": "^1.0"
}
}
4 changes: 4 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 1
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
16 changes: 7 additions & 9 deletions src/Command/MakeVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* NetBrothers VersionBundle
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 19.03.21
*/

namespace NetBrothers\VersionBundle\Command;
Expand Down Expand Up @@ -135,7 +134,6 @@ protected function initCommand(
$this->entityManager = $entityManager;
$con = $this->entityManager->getConnection();
$con->getConfiguration()->setSchemaAssetsFilter(null);

$schemaManager = $con->createSchemaManager();
$compareService = new CompareService($schemaManager, $excludeColumnNames);
$this->jobService = new JobService($schemaManager, $compareService, $ignoreTables);
Expand Down Expand Up @@ -209,9 +207,9 @@ private function printReport(SymfonyStyle $io): int
$errors = [];
$warnings = [];
foreach ($this->jobService->getReport() as $message) {
if (preg_match("/^ERROR/", $message)) {
if (preg_match('/^ERROR/', $message)) {
$errors[] = $message;
} elseif (preg_match("/^WARNING/", $message)) {
} elseif (preg_match('/^WARNING/', $message)) {
$warnings[] = $message;
} else {
$io->writeln($message);
Expand Down Expand Up @@ -277,7 +275,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->setJobs($tableName);
} catch (\Exception $exception) {
$io->error($exception->getMessage());
return 1;
return Command::FAILURE;
}
$this->sql = [];
if (null !== $tableName) {
Expand All @@ -302,14 +300,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($this->jobService->hasError()) {
$this->printReport($io);
$io->newLine(2);
$io->note("Operation canceled");
return 1;
$io->note('Operation canceled');
return Command::FAILURE;
}
$this->executeService->execute($this->sql);
if ($this->jobService->hasWarning()) {
$this->printReport($io);
}
$io->success("Operation success");
return 0;
$io->success('Operation success');
return Command::SUCCESS;
}
}
29 changes: 18 additions & 11 deletions src/Services/CompareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,33 @@ private function getColumn(Table $table, string $columnName): ?Column
*/
private function compareOneColumn(Column $column, Column $compareColumn): bool
{
$type = $column->getType()->getName();
$compareColumnType = $compareColumn->getType()->getName();
$valid = true;
if ($type !== $compareColumnType) {
$this->errors[] = "different types";

// compare types
$columnType = $column->getType();
$compareColumnType = $compareColumn->getType();
if (! $columnType instanceof $compareColumnType) {
$this->errors[] = 'different types';
$valid = false;
}

// compare default values
$defaultValue = $column->getDefault();
$compareValue = $compareColumn->getDefault();
if ($defaultValue !== $compareValue) {
$this->errors[] = "different default values ($defaultValue != $compareValue)";
$this->errors[] = sprintf(
'different default values (%s != %s)',
$defaultValue,
$compareValue
);
$valid = false;
}
$value = $column->getNotnull();
$compare = $compareColumn->getNotnull();
if ($value !== $compare) {
$this->errors[] = "different NULL-definition";

// compare NULL definition
if ($column->getNotnull() !== $compareColumn->getNotnull()) {
$this->errors[] = "different NULL definitions";
$valid = false;
}
return $valid;
}

}
}
19 changes: 9 additions & 10 deletions src/Services/JobService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* NetBrothers VersionBundle
*
Expand All @@ -9,7 +10,6 @@

namespace NetBrothers\VersionBundle\Services;


use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
Expand Down Expand Up @@ -96,7 +96,11 @@ private function initJobs(): void
* @param AbstractSchemaManager $schemaManager
* @param array $ignoreTables
*/
public function __construct(AbstractSchemaManager $schemaManager, CompareService $compareService, array $ignoreTables = [])
public function __construct(
AbstractSchemaManager $schemaManager,
CompareService $compareService,
array $ignoreTables = []
)
{
$this->schemaManager = $schemaManager;
$this->ignoreTables = $ignoreTables;
Expand Down Expand Up @@ -168,7 +172,6 @@ private function originTableExists(Table $versionTable): bool
$tName = $versionTable->getName();
$orgName = preg_replace("/" . Definitions::VERSION_TABLE_NAME_POSTFIX ."$/", '', $tName);
return $this->schemaManager->tablesExist([$orgName]);

}

/**
Expand Down Expand Up @@ -238,16 +241,12 @@ public function getVersionTableFromOrigin(Table $table): ?Table
*/
public function getOneTable(string $tableName): ?Table
{
if (!$this->schemaManager->tablesExist([$tableName])) {
if (! $this->schemaManager->tablesExist([$tableName])) {
return null;
}
if (is_null($this->schema)) {
$this->schema = $this->schemaManager->createSchema();
$this->schema = $this->schemaManager->introspectSchema();
}
return $this->schema->getTable($tableName);
}




}
}
82 changes: 28 additions & 54 deletions src/Services/Sql/ExecuteService.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
<?php

/**
* NetBrothers VersionBundle
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 19.03.21
*
*/

namespace NetBrothers\VersionBundle\Services\Sql;


use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Exception as DriverException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\EntityManagerInterface;

/** executing sql on connection
*
* Class ExecuteService
/**
* Execute SQL on connection.
*
* @package NetBrothers\VersionBundle\Services\Sql
*/
class ExecuteService
{
private const SUPPORTED_PLATFORMS = [
MariaDBPlatform::class,
MySQLPlatform::class,
];

/** @var EntityManagerInterface */
private $entityManager;

/** @var Connection */
private $connection;

/** @var string */
private $errMsg;
private Connection $connection;

/**
* ExecuteService constructor.
Expand All @@ -38,53 +35,30 @@ class ExecuteService
*/
public function __construct(EntityManagerInterface $entityManager)
{
if ($entityManager->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
throw new \Exception(__CLASS__ . ' can only be executed safely on \'mysql\'.');
$platform = $entityManager->getConnection()->getDatabasePlatform();
if (! in_array(get_class($platform), self::SUPPORTED_PLATFORMS)) {
echo sprintf(
'WARNING: Unsupported database platform: %s',
get_class($platform)
) . PHP_EOL;
}
$this->entityManager = $entityManager;
$this->connection = $entityManager->getConnection();
}

/**
* @param array $sql
* @return bool
* @throws DriverException
* @throws \Exception
* Executes the assembled queries.
*
* @param array $sql SQL queries priorly generated by the `GenerateService`.
* @return void
* @throws Exception
*/
public function execute(array $sql = []): bool
public function execute(array $sql = []): void
{
if (!is_array($sql)) {
throw new \Exception(__CLASS__ . ': only sql-statements packed in array can be executed.');
if (empty($sql)) {
return;
}
if (0 < count($sql)) {
try {
foreach ($sql as $query) {
if (true !== $this->_execute($query)) {
return false;
}
}
} catch (\Exception $e) {
throw new \Exception("Cannot commit", 500, $e);
}
foreach ($sql as $query) {
$this->connection->prepare($query)->executeQuery();
}
return true;
}

/**
* @param string $query
* @return bool
* @throws \Doctrine\DBAL\Exception
*/
private function _execute(string $query): bool
{
try {
$statement = $this->connection->prepare($query);
$result = $statement->executeQuery();
return true;
} catch ( DriverException $e) {
$this->errMsg = "Cannot execute SQL: $query";
return false;
}
}

}

0 comments on commit cfa4584

Please sign in to comment.