Skip to content

Commit

Permalink
WIP, dbal3
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Sep 19, 2021
1 parent 903b982 commit f5a2714
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"require": {
"php": "^7.2 || ^8.0",
"composer/package-versions-deprecated": "^1.8",
"doctrine/dbal": "^2.11",
"doctrine/dbal": "^2.11 || ^3.0",
"doctrine/deprecations": "^0.5.3",
"doctrine/event-manager": "^1.0",
"friendsofphp/proxy-manager-lts": "^1.0",
Expand All @@ -37,7 +37,7 @@
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/coding-standard": "^8.0",
"doctrine/orm": "^2.6",
"doctrine/orm": "^2.6@dev",
"doctrine/persistence": "^1.3 || ^2.0",
"doctrine/sql-formatter": "^1.0",
"ergebnis/composer-normalize": "^2.9",
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Migrations\Query\Query;
use Psr\Log\LoggerInterface;

use function method_exists;
use function sprintf;

/**
Expand All @@ -42,7 +43,9 @@ abstract class AbstractMigration
public function __construct(Connection $connection, LoggerInterface $logger)
{
$this->connection = $connection;
$this->sm = $this->connection->getSchemaManager();
$this->sm = method_exists($this->connection, 'createSchemaManager')
? $this->connection->createSchemaManager()
: $this->connection->getSchemaManager();
$this->platform = $this->connection->getDatabasePlatform();
$this->logger = $logger;
}
Expand Down
17 changes: 13 additions & 4 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Migrations;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\ConnectionLoader;
use Doctrine\Migrations\Configuration\EntityManager\EntityManagerLoader;
Expand Down Expand Up @@ -51,6 +52,7 @@

use function array_key_exists;
use function call_user_func;
use function method_exists;
use function preg_quote;
use function sprintf;

Expand Down Expand Up @@ -231,19 +233,26 @@ public function getSchemaDumper(): SchemaDumper

return new SchemaDumper(
$this->getConnection()->getDatabasePlatform(),
$this->getConnection()->getSchemaManager(),
$this->getSchemaManager($this->getConnection()),
$this->getMigrationGenerator(),
$this->getMigrationSqlGenerator(),
$excludedTables
);
});
}

private function getSchemaManager(Connection $connection): AbstractSchemaManager
{
return method_exists($connection, 'createSchemaManager')
? $connection->createSchemaManager()
: $connection->getSchemaManager();
}

private function getEmptySchemaProvider(): SchemaProvider
{
return $this->getDependency(EmptySchemaProvider::class, function (): SchemaProvider {
return new EmptySchemaProvider(
$this->getConnection()->getSchemaManager()
$this->getSchemaManager($this->getConnection())
);
});
}
Expand Down Expand Up @@ -275,7 +284,7 @@ public function getDiffGenerator(): DiffGenerator
return $this->getDependency(DiffGenerator::class, function (): DiffGenerator {
return new DiffGenerator(
$this->getConnection()->getConfiguration(),
$this->getConnection()->getSchemaManager(),
$this->getSchemaManager($this->getConnection()),
$this->getSchemaProvider(),
$this->getConnection()->getDatabasePlatform(),
$this->getMigrationGenerator(),
Expand All @@ -290,7 +299,7 @@ public function getSchemaDiffProvider(): SchemaDiffProvider
return $this->getDependency(SchemaDiffProvider::class, function (): LazySchemaDiffProvider {
return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
new DBALSchemaDiffProvider(
$this->getConnection()->getSchemaManager(),
$this->getSchemaManager($this->getConnection()),
$this->getConnection()->getDatabasePlatform()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory();
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input);

$question = sprintf(
$databaseName = $this->getDependencyFactory()->getConnection()->getDatabase();
$question = sprintf(
'WARNING! You are about to execute a migration in database "%s" that could result in schema changes and data loss. Are you sure you wish to continue?',
$this->getDependencyFactory()->getConnection()->getDatabase() ?? '<unnamed>'
$databaseName === '' || $databaseName === null ? '<unnamed>' : $databaseName
);
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory();
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input);

$question = sprintf(
$databaseName = $this->getDependencyFactory()->getConnection()->getDatabase();
$question = sprintf(
'WARNING! You are about to execute a migration in database "%s" that could result in schema changes and data loss. Are you sure you wish to continue?',
$this->getDependencyFactory()->getConnection()->getDatabase() ?? '<unnamed>'
$databaseName === '' || $databaseName === null ? '<unnamed>' : $databaseName
);
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) {
$this->io->error('Migration cancelled!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Migrations\Tests\Tools\Console;

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\ConsoleRunner;
use Doctrine\ORM\EntityManager;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\Console\Helper\HelperSet;

use function chdir;
use function class_exists;
use function getcwd;
use function realpath;
use function sprintf;
Expand All @@ -28,6 +30,11 @@ class ConsoleRunnerTest extends TestCase

public function testCreateDependencyFactoryFromLegacyDbalHelper(): void
{
// @phpstan-ignore-next-line
if (! class_exists(ConnectionHelper::class)) {
self::markTestSkipped('DBAL 3.0 does not provide anymore the ConnectionHelper');
}

$dir = getcwd();
if ($dir === false) {
$dir = '.';
Expand Down Expand Up @@ -65,6 +72,11 @@ public function testCreateDependencyFactoryFromLegacyOrmHelper(): void

public function testCreateDependencyFactoryFromWrongLegacyHelper(): void
{
// @phpstan-ignore-next-line
if (! class_exists(ConnectionHelper::class)) {
self::markTestSkipped('DBAL 3.0 does not provide anymore the ConnectionHelper');
}

$this->expectException(RuntimeException::class);

$dir = getcwd();
Expand Down

0 comments on commit f5a2714

Please sign in to comment.