From 378cf4b897e9ab9b0468011ddf6efbcf817dbd72 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 7 Nov 2022 12:07:45 +0100 Subject: [PATCH 1/4] Don't throw NoMappingFound from OrmSchemaProvider --- .../Migrations/Provider/OrmSchemaProvider.php | 9 --------- .../Tests/Provider/OrmSchemaProviderTest.php | 11 +++++++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php b/lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php index 1f69bb8a9..92c0faab8 100644 --- a/lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php +++ b/lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php @@ -5,12 +5,10 @@ namespace Doctrine\Migrations\Provider; use Doctrine\DBAL\Schema\Schema; -use Doctrine\Migrations\Provider\Exception\NoMappingFound; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Tools\SchemaTool; -use function count; use function usort; /** @@ -29,18 +27,11 @@ public function __construct(EntityManagerInterface $em) $this->entityManager = $em; } - /** - * @throws NoMappingFound - */ public function createSchema(): Schema { /** @var array> $metadata */ $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata(); - if (count($metadata) === 0) { - throw NoMappingFound::new(); - } - usort($metadata, static function (ClassMetadata $a, ClassMetadata $b): int { return $a->getTableName() <=> $b->getTableName(); }); diff --git a/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php b/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php index 248f2a02d..a399b39f2 100644 --- a/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php +++ b/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php @@ -12,7 +12,6 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\Driver\XmlDriver; use Doctrine\ORM\ORMSetup; -use UnexpectedValueException; /** * Tests the OrmSchemaProvider using a real entity manager. @@ -37,10 +36,14 @@ public function testCreateSchemaFetchesMetadataFromEntityManager(): void } } - public function testEntityManagerWithoutMetadataCausesError(): void + /** + * It should be OK to use migrations to manage tables not managed by + * the ORM. + * + * @doesNotPerformAssertions + */ + public function testEntityManagerWithoutMetadata(): void { - $this->expectException(UnexpectedValueException::class); - $this->config->setMetadataDriverImpl(new XmlDriver([])); $this->ormProvider->createSchema(); From 2fee1b42ac7884c2fe78b9e75b138269586810b7 Mon Sep 17 00:00:00 2001 From: Einar Date: Mon, 12 Dec 2022 20:35:52 +0100 Subject: [PATCH 2/4] Use correct default values See TableMetadataStorageConfiguration --- docs/en/reference/configuration.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/en/reference/configuration.rst b/docs/en/reference/configuration.rst index 06af26283..d451c1ac3 100644 --- a/docs/en/reference/configuration.rst +++ b/docs/en/reference/configuration.rst @@ -27,7 +27,7 @@ Now, in the root of your project place a file named ``migrations.php``, ``migrat 'table_storage' => [ 'table_name' => 'doctrine_migration_versions', 'version_column_name' => 'version', - 'version_column_length' => 1024, + 'version_column_length' => 191, 'executed_at_column_name' => 'executed_at', 'execution_time_column_name' => 'execution_time', ], @@ -50,7 +50,7 @@ Now, in the root of your project place a file named ``migrations.php``, ``migrat table_storage: table_name: doctrine_migration_versions version_column_name: version - version_column_length: 1024 + version_column_length: 191 executed_at_column_name: executed_at execution_time_column_name: execution_time @@ -81,7 +81,7 @@ Now, in the root of your project place a file named ``migrations.php``, ``migrat @@ -104,7 +104,7 @@ Now, in the root of your project place a file named ``migrations.php``, ``migrat "table_storage": { "table_name": "doctrine_migration_versions", "version_column_name": "version", - "version_column_length": 1024, + "version_column_length": 191, "executed_at_column_name": "executed_at", "execution_time_column_name": "execution_time" }, @@ -164,7 +164,7 @@ Here the possible options for ``table_storage``: +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+ | version_column_name | no | version | The name of the column which stores the version name. | +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+ -| version_column_length | no | 1024 | The length of the column which stores the version name. | +| version_column_length | no | 191 | The length of the column which stores the version name. | +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+ | executed_at_column_name | no | executed_at | The name of the column which stores the date that a migration was executed. | +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+ From 315f255aa78c1179197759a6afa9608437ea2ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 9 Jan 2023 22:54:53 +0100 Subject: [PATCH 3/4] Address deprecations from doctrine/orm --- composer.json | 2 +- .../Tests/Configuration/EntityManager/_files/em-loader.php | 2 +- .../Tests/Configuration/EntityManager/_files/migrations-em.php | 2 +- .../Migrations/Tests/Provider/OrmSchemaProviderTest.php | 2 +- .../Migrations/Tests/Tools/Console/config/cli-config.php | 2 +- .../Tests/Tools/Console/legacy-config-orm/cli-config.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index ba9328d68..bd547f968 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "require-dev": { "ext-pdo_sqlite": "*", "doctrine/coding-standard": "^9", - "doctrine/orm": "^2.12", + "doctrine/orm": "^2.13", "doctrine/persistence": "^2 || ^3", "doctrine/sql-formatter": "^1.0", "phpstan/phpstan": "^1.5", diff --git a/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/em-loader.php b/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/em-loader.php index ad0d048ec..2f20bb724 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/em-loader.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/em-loader.php @@ -19,6 +19,6 @@ public function getEntityManager(?string $name = null): EntityManagerInterface $conf->setProxyNamespace('Foo'); $conf->setMetadataDriverImpl(new PHPDriver('')); - return EntityManager::create($conn, $conf); + return new EntityManager($conn, $conf); } }; diff --git a/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/migrations-em.php b/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/migrations-em.php index 3bd4912db..df565ec2f 100644 --- a/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/migrations-em.php +++ b/tests/Doctrine/Migrations/Tests/Configuration/EntityManager/_files/migrations-em.php @@ -14,4 +14,4 @@ $conf->setProxyNamespace('Foo'); $conf->setMetadataDriverImpl(new PHPDriver('')); -return EntityManager::create($conn, $conf); +return new EntityManager($conn, $conf); diff --git a/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php b/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php index a399b39f2..081646dd5 100644 --- a/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php +++ b/tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php @@ -55,7 +55,7 @@ protected function setUp(): void $this->config->setClassMetadataFactoryName(ClassMetadataFactory::class); $this->conn = $this->getSqliteConnection(); - $this->entityManager = EntityManager::create($this->conn, $this->config); + $this->entityManager = new EntityManager($this->conn, $this->config); $this->ormProvider = new OrmSchemaProvider($this->entityManager); } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/config/cli-config.php b/tests/Doctrine/Migrations/Tests/Tools/Console/config/cli-config.php index dd99d7806..d73e28cd3 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/config/cli-config.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/config/cli-config.php @@ -17,7 +17,7 @@ $conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]); -$em = EntityManager::create($conn, $conf); +$em = new EntityManager($conn, $conf); $config = new ConfigurationArray([ 'custom_template' => 'foo', diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/legacy-config-orm/cli-config.php b/tests/Doctrine/Migrations/Tests/Tools/Console/legacy-config-orm/cli-config.php index ff8927b53..c9c8a6083 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/legacy-config-orm/cli-config.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/legacy-config-orm/cli-config.php @@ -16,7 +16,7 @@ $conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]); -$em = EntityManager::create($conn, $conf); +$em = new EntityManager($conn, $conf); return new HelperSet([ 'em' => new EntityManagerHelper($em), From 4da00c540672d687b8efa7b5f0c2b50107845379 Mon Sep 17 00:00:00 2001 From: Agustin Gomes Date: Mon, 9 Jan 2023 22:08:19 +0100 Subject: [PATCH 4/4] Apply workaround to maintain expected behavior This test case addition simulates what happens when the `--all-or-nothing` option is indicated, but no explicit value is passed. We'll set a default option, which is retrieved in case the option has not been provided, allowing us to override the value in case the option is different than the default value. --- .../Tools/Console/Command/MigrateCommand.php | 3 ++- ...onsoleInputMigratorConfigurationFactory.php | 18 ++++++++++++++++-- .../Console/Command/MigrateCommandTest.php | 13 +++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php index 13394258c..24a48df83 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php @@ -75,7 +75,8 @@ protected function configure(): void 'all-or-nothing', null, InputOption::VALUE_OPTIONAL, - 'Wrap the entire migration in a transaction.' + 'Wrap the entire migration in a transaction.', + 'notprovided' ) ->setHelp(<<%command.name% command executes a migration to a specified version or the latest available version: diff --git a/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php b/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php index 04ee777b3..853b28913 100644 --- a/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php +++ b/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php @@ -21,12 +21,26 @@ public function getMigratorConfiguration(InputInterface $input): MigratorConfigu { $timeAllQueries = $input->hasOption('query-time') ? (bool) $input->getOption('query-time') : false; $dryRun = $input->hasOption('dry-run') ? (bool) $input->getOption('dry-run') : false; - $allOrNothing = $input->hasOption('all-or-nothing') ? $input->getOption('all-or-nothing') : null; - $allOrNothing = (bool) ($allOrNothing ?? $this->configuration->isAllOrNothing()); + $allOrNothing = $this->determineAllOrNothingValueFrom($input) ?? $this->configuration->isAllOrNothing(); return (new MigratorConfiguration()) ->setDryRun($dryRun) ->setTimeAllQueries($timeAllQueries) ->setAllOrNothing($allOrNothing); } + + private function determineAllOrNothingValueFrom(InputInterface $input): ?bool + { + $allOrNothingOption = null; + + if ($input->hasOption('all-or-nothing')) { + $allOrNothingOption = $input->getOption('all-or-nothing'); + } + + if ($allOrNothingOption === 'notprovided') { + return null; + } + + return (bool) ($allOrNothingOption ?? true); + } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php index 71aa4344d..3838137ae 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php @@ -362,7 +362,7 @@ public function testExecuteMigrateDown(): void } /** - * @psalm-param array $input + * @psalm-param array $input * * @dataProvider allOrNothing */ @@ -390,13 +390,22 @@ public function testExecuteMigrateAllOrNothing(bool $default, array $input, bool } /** - * @psalm-return Generator, bool}> + * @psalm-return Generator, bool}> */ public function allOrNothing(): Generator { yield [false, ['--all-or-nothing' => false], false]; + yield [false, ['--all-or-nothing' => 0], false]; + yield [false, ['--all-or-nothing' => '0'], false]; + yield [false, ['--all-or-nothing' => true], true]; + yield [false, ['--all-or-nothing' => 1], true]; + yield [false, ['--all-or-nothing' => '1'], true]; + yield [false, ['--all-or-nothing' => null], true]; + yield [true, ['--all-or-nothing' => false], false]; + yield [true, ['--all-or-nothing' => 0], false]; + yield [true, ['--all-or-nothing' => '0'], false]; yield [true, [], true]; yield [false, [], false];