Skip to content

Commit

Permalink
DBAL Upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed May 9, 2024
1 parent 5c586c2 commit 2b66fd8
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"require": {
"php": " >=8.3",
"doctrine/dbal": "^2.13",
"doctrine/dbal": "^2.13 | ^3",
"symfony/config": "^5.3",
"symfony/console": "^5.3",
"symfony/dependency-injection": "^5.3",
Expand Down
22 changes: 5 additions & 17 deletions src/bundle/Command/DumpSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Ibexa\DoctrineSchema\Builder\SchemaBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -40,7 +38,7 @@ final class DumpSqlCommand extends Command
private const PLATFORM_MAP = [
'mysql8' => MySQL80Platform::class,
'mysql57' => MySQL57Platform::class,
'mysql' => MySqlPlatform::class,
'mysql' => MySQLPlatform::class,
'mariadb' => MariaDb1027Platform::class,
'postgres' => PostgreSQL100Platform::class,
];
Expand Down Expand Up @@ -90,11 +88,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$platform = $this->getPlatformForInput($input);

if ($input->getOption('compare')) {
$schemaManager = $this->getSchemaManager();
$fromSchema = $this->introspectSchema($schemaManager);
$schemaManager = $this->db->createSchemaManager();
$fromSchema = $schemaManager->introspectSchema();

$comparator = new Comparator();
$diff = $comparator->compare($fromSchema, $toSchema);
$diff = $comparator->compareSchemas($fromSchema, $toSchema);
$sqls = $diff->toSql($platform);
} else {
$sqls = $toSchema->toSql($platform);
Expand All @@ -118,16 +116,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function getSchemaManager(): AbstractSchemaManager
{
return $this->db->getSchemaManager();
}

private function introspectSchema(AbstractSchemaManager $schemaManager): Schema
{
return $schemaManager->createSchema();
}

private function getPlatformForInput(InputInterface $input): AbstractPlatform
{
$forcePlatform = $input->getOption('force-platform');
Expand Down
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ services:
Doctrine\Common\EventManager: ~

Ibexa\Bundle\DoctrineSchema\Command\DumpSqlCommand: ~

Ibexa\Bundle\DoctrineSchema\EventSubscriber\DoctrineOrmSchemaSubscriber:
arguments:
$em: '@doctrine.orm.ibexa_default_entity_manager'
4 changes: 2 additions & 2 deletions src/contracts/SchemaImporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface SchemaImporterInterface
* @return \Doctrine\DBAL\Schema\Schema imported schema
*
* @throws \Ibexa\Contracts\DoctrineSchema\Exception\InvalidConfigurationException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function importFromFile(string $schemaFilePath, ?Schema $targetSchema = null): Schema;

Expand All @@ -37,7 +37,7 @@ public function importFromFile(string $schemaFilePath, ?Schema $targetSchema = n
* @return \Doctrine\DBAL\Schema\Schema imported schema
*
* @throws \Ibexa\Contracts\DoctrineSchema\Exception\InvalidConfigurationException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function importFromSource(string $schemaDefinition, ?Schema $targetSchema = null): Schema;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Exporter/SchemaExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(SchemaTableExporter $tableYamlExporter)
*
* @return string representation of database schema in Yaml format
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function export(Schema $schema): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Exporter/Table/SchemaTableExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SchemaTableExporter
/**
* Export \Doctrine\DBAL\Schema\Table to array representation.
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function export(Table $table): array
{
Expand Down Expand Up @@ -90,7 +90,7 @@ private function exportIndices(array $tableMetadata, Table $table): array
*
* @return array modified $tableMetadata
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
private function exportColumns(array $tableMetadata, Table $table): array
{
Expand Down
32 changes: 23 additions & 9 deletions src/lib/Importer/SchemaImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,47 @@ private function addSchemaTableColumns(Table $table, array $columnList): void
'precision',
'type',
'nullable',
'unsigned',
'fixed',
'options',
'index',
'foreignKey',
]);

$column = $table->addColumn(
$columnName,
$columnConfiguration['type'],
$columnConfiguration['options'] ?? []
);

if (isset($columnConfiguration['length'])) {
$columnConfiguration['options']['length'] = $columnConfiguration['length'];
$column->setLength($columnConfiguration['length']);
}

if (isset($columnConfiguration['scale'])) {
$columnConfiguration['options']['scale'] = $columnConfiguration['scale'];
$column->setScale($columnConfiguration['scale']);
}

if (isset($columnConfiguration['precision'])) {
$columnConfiguration['options']['precision'] = $columnConfiguration['precision'];
$column->setPrecision($columnConfiguration['precision']);
}

$column = $table->addColumn(
$columnName,
$columnConfiguration['type'],
$columnConfiguration['options'] ?? []
);

if (isset($columnConfiguration['nullable'])) {
$column->setNotnull(!$columnConfiguration['nullable']);
}

if (isset($columnConfiguration['default'])) {
$column->setDefault($columnConfiguration['default']);
}

if (isset($columnConfiguration['unsigned'])) {
$column->setUnsigned($columnConfiguration['unsigned']);
}

if (isset($columnConfiguration['fixed'])) {
$column->setFixed($columnConfiguration['fixed']);
}

if (isset($columnConfiguration['index'])) {
$indexConfig = $this->normalizeIndexConfig($columnConfiguration['index'], $location);

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Database/Builder/MySqlTestDatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class MySqlTestDatabaseBuilder implements TestDatabaseBuilder
{
/**
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
* @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
*/
public function buildDatabase(): Connection
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class SqliteTestDatabaseBuilder implements TestDatabaseBuilder
{
/**
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function buildDatabase(): Connection
{
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Database/Builder/TestDatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface TestDatabaseBuilder
{
/**
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
* @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
*/
public function buildDatabase(): Connection;
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Ibexa\Tests\DoctrineSchema\Database\DbPlatform;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Ibexa\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform;
use Ibexa\Tests\DoctrineSchema\Database\TestDatabaseFactory;
Expand All @@ -29,13 +29,13 @@ public function setUp(): void
}

/**
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
* @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
*/
public function testForeignKeys(): void
{
$connection = $this->testDatabaseFactory->prepareAndConnect($this->sqliteDbPlatform);
$schema = $connection->getSchemaManager()->createSchema();
$schema = $connection->createSchemaManager()->introspectSchema();

$primaryTable = $schema->createTable('my_primary_table');
$primaryTable->addColumn('id', 'integer');
Expand All @@ -55,7 +55,7 @@ public function testForeignKeys(): void
$connection->insert($secondaryTable->getName(), ['id' => 1], [ParameterType::INTEGER]);

// insert broken record
$this->expectException(DBALException::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('FOREIGN KEY constraint failed');
$connection->insert($secondaryTable->getName(), ['id' => 2], [ParameterType::INTEGER]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Database/TestDatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()

/**
* @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function prepareAndConnect(AbstractPlatform $databasePlatform): Connection
{
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/Exporter/SchemaExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Ibexa\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform;
use Ibexa\DoctrineSchema\Exporter\SchemaExporter;
use Ibexa\DoctrineSchema\Exporter\Table\SchemaTableExporter;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function providerForTestExport(): array
{
$data = [];

$databasePlatforms = [new SqliteDbPlatform(), new MySqlPlatform()];
$databasePlatforms = [new SqliteDbPlatform(), new MySQLPlatform()];

// iterate over output files to avoid loading it for each platform
$directoryIterator = new \DirectoryIterator(__DIR__ . '/_fixtures/output');
Expand Down Expand Up @@ -84,7 +84,7 @@ public function providerForTestExport(): array
*
* @param string $inputSchemaSQL
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function testExport(
AbstractPlatform $databasePlatform,
Expand Down Expand Up @@ -128,7 +128,7 @@ public function testExport(

/**
* @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
private function getDatabaseConnection(AbstractPlatform $databasePlatform): Connection
{
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/Importer/SchemaImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SchemaImporterTest extends TestCase
*
* @phpstan-return iterable<array{non-empty-string, \Doctrine\DBAL\Schema\Schema}>
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function providerForTestImportFromFile(): iterable
{
Expand Down Expand Up @@ -112,6 +112,7 @@ public function providerForTestImportFromFile(): iterable
[
new Index('primary', ['id'], false, true),
],
[],
[
new ForeignKeyConstraint(
['main_id'],
Expand Down Expand Up @@ -226,6 +227,7 @@ public function providerForTestImportFromFile(): iterable
new Index('data3_idx', ['data3'], false, false),
new Index('data4_uidx', ['data4'], true, false),
],
[],
[
new ForeignKeyConstraint(
['id'],
Expand Down Expand Up @@ -281,7 +283,7 @@ public function providerForTestImportFromFile(): iterable
* @param string $yamlSchemaDefinitionFile custom Yaml schema definition fixture file name
*
* @throws \Ibexa\Contracts\DoctrineSchema\Exception\InvalidConfigurationException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Exception
*/
public function testImportFromFile(
string $yamlSchemaDefinitionFile,
Expand Down

0 comments on commit 2b66fd8

Please sign in to comment.