Skip to content

Commit

Permalink
Merge pull request #55 from trompette/remove-doctrine-dbal-deprecations
Browse files Browse the repository at this point in the history
Remove Doctrine DBAL deprecations
  • Loading branch information
trompette authored Mar 25, 2024
2 parents 216a222 + 9399ced commit e79d31f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 31 deletions.
10 changes: 4 additions & 6 deletions sources/DBAL/SchemaMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ public function __construct(Connection $connection)

public function migrateSchema(): void
{
$platform = $this->connection->getDatabasePlatform();
$fromSchema = $this->connection->getSchemaManager()->createSchema();
$schemaManager = $this->connection->createSchemaManager();

$this->configureSchema($toSchema = clone $fromSchema, $this->connection);
$schema = $schemaManager->introspectSchema();
$this->configureSchema($schema, $this->connection);

foreach ($toSchema->getMigrateFromSql($fromSchema, $platform) as $statement) {
$this->connection->executeStatement($statement);
}
$schemaManager->migrateSchema($schema);
}
}
2 changes: 1 addition & 1 deletion tests/Bundle/FeatureTogglesBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
$container
->register('my_doctrine_dbal_connection', Connection::class)
->setFactory([DriverManager::class, 'getConnection'])
->setArguments([['url' => 'sqlite:///:memory:']])
->setArguments([['driver' => 'pdo_sqlite', 'memory' => true]])
->setPublic(true)
;

Expand Down
36 changes: 14 additions & 22 deletions tests/DBAL/ConfigurationRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\TestCase;
use Trompette\FeatureToggles\DBAL\SchemaMigrator;
Expand All @@ -20,7 +16,7 @@ abstract class ConfigurationRepositoryTest extends TestCase

protected function setUp(): void
{
$this->connection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
$this->connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$this->createRepository();
}

Expand All @@ -31,33 +27,29 @@ public function testSchemaIsConfiguredForUnderlyingConnectionOnly(): void
$this->repository->configureSchema($schema = new Schema(), $this->connection);
static::assertCount(1, $schema->getTables());

$anotherConnection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
$anotherConnection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$this->repository->configureSchema($schema = new Schema(), $anotherConnection);
static::assertEmpty($schema->getTables());
}

public function testAlteredSchemaCanBeMigrated(): void
{
$schemaManager = $this->connection->getSchemaManager();
$schemaManager = $this->connection->createSchemaManager();
$comparator = $schemaManager->createComparator();

$this->repository->migrateSchema();
$schema = $schemaManager->createSchema();
static::assertCount(1, $tables = $schema->getTables());
$expectedSchema = $schemaManager->introspectSchema();
$expectedTables = $expectedSchema->getTables();
static::assertNotEmpty($expectedTables);

$table = $schema->getTable((string) array_key_first($tables));
$schemaManager->alterTable($this->createTableDiff($table));
static::assertNotEquals($schema, $schemaManager->createSchema());
$fromTable = reset($expectedTables);
$toTable = clone $fromTable;
$toTable->addColumn(uniqid('c_'), Types::INTEGER, ['default' => 0]);
$tableDiff = $comparator->compareTables($fromTable, $toTable);
$schemaManager->alterTable($tableDiff);
static::assertNotEquals($expectedSchema, $schemaManager->introspectSchema());

$this->repository->migrateSchema();
static::assertEquals($schema, $schemaManager->createSchema());
}

private function createTableDiff(Table $table): TableDiff
{
$tableDiff = new TableDiff($table->getName());
$tableDiff->addedColumns[] = new Column(uniqid('c_'), Type::getType(Types::INTEGER), ['default' => 0]);
$tableDiff->fromTable = $table;

return $tableDiff;
static::assertEquals($expectedSchema, $schemaManager->introspectSchema());
}
}
2 changes: 1 addition & 1 deletion tests/ORM/SchemaConfigurationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SchemaConfigurationListenerTest extends TestCase
public function testSchemaIsConfiguredAfterGeneration(): void
{
$schema = new Schema();
$connection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->getConnection()->willReturn($connection);
Expand Down
2 changes: 1 addition & 1 deletion tests/ToggleRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private function configureToggleRouter(FeatureDefinition $definition = null, arr
*/
private function configureAllStrategies(): array
{
$DBALConnection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
$DBALConnection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

$onOffConfigurationRepository = new OnOffStrategyConfigurationRepository($DBALConnection);
$onOffConfigurationRepository->migrateSchema();
Expand Down

0 comments on commit e79d31f

Please sign in to comment.