diff --git a/composer.json b/composer.json index 6842921a0..75b9d3a72 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": { "php": "^7.4 || ^8.0", "composer-runtime-api": "^2", - "doctrine/dbal": "^3.4.3", + "doctrine/dbal": "^3.5.1", "doctrine/deprecations": "^0.5.3 || ^1", "doctrine/event-manager": "^1.0", "friendsofphp/proxy-manager-lts": "^1.0", diff --git a/lib/Doctrine/Migrations/Generator/DiffGenerator.php b/lib/Doctrine/Migrations/Generator/DiffGenerator.php index 98dfd0b5d..e7a83b330 100644 --- a/lib/Doctrine/Migrations/Generator/DiffGenerator.php +++ b/lib/Doctrine/Migrations/Generator/DiffGenerator.php @@ -91,7 +91,7 @@ static function ($assetName) use ($filterExpression) { $comparator = $this->schemaManager->createComparator(); - $upSql = $comparator->compareSchemas($fromSchema, $toSchema)->toSql($this->platform); + $upSql = $this->platform->getAlterSchemaSQL($comparator->compareSchemas($fromSchema, $toSchema)); $up = $this->migrationSqlGenerator->generate( $upSql, @@ -100,7 +100,7 @@ static function ($assetName) use ($filterExpression) { $checkDbPlatform ); - $downSql = $comparator->compareSchemas($toSchema, $fromSchema)->toSql($this->platform); + $downSql = $this->platform->getAlterSchemaSQL($comparator->compareSchemas($toSchema, $fromSchema)); $down = $this->migrationSqlGenerator->generate( $downSql, @@ -127,7 +127,7 @@ private function createEmptySchema(): Schema private function createFromSchema(): Schema { - return $this->schemaManager->createSchema(); + return $this->schemaManager->introspectSchema(); } private function createToSchema(): Schema diff --git a/lib/Doctrine/Migrations/Metadata/Storage/TableMetadataStorage.php b/lib/Doctrine/Migrations/Metadata/Storage/TableMetadataStorage.php index bff0185f3..3afaff833 100644 --- a/lib/Doctrine/Migrations/Metadata/Storage/TableMetadataStorage.php +++ b/lib/Doctrine/Migrations/Metadata/Storage/TableMetadataStorage.php @@ -206,10 +206,10 @@ private function needsUpdate(Table $expectedTable): ?TableDiff return null; } - $currentTable = $this->schemaManager->listTableDetails($this->configuration->getTableName()); - $diff = $this->schemaManager->createComparator()->diffTable($currentTable, $expectedTable); + $currentTable = $this->schemaManager->introspectTable($this->configuration->getTableName()); + $diff = $this->schemaManager->createComparator()->compareTables($currentTable, $expectedTable); - return $diff instanceof TableDiff ? $diff : null; + return $diff->isEmpty() ? null : $diff; } private function isInitialized(): bool diff --git a/lib/Doctrine/Migrations/Provider/DBALSchemaDiffProvider.php b/lib/Doctrine/Migrations/Provider/DBALSchemaDiffProvider.php index 5b41c8b46..6934b97fa 100644 --- a/lib/Doctrine/Migrations/Provider/DBALSchemaDiffProvider.php +++ b/lib/Doctrine/Migrations/Provider/DBALSchemaDiffProvider.php @@ -36,7 +36,7 @@ public function __construct(AbstractSchemaManager $schemaManager, AbstractPlatfo public function createFromSchema(): Schema { - return $this->schemaManager->createSchema(); + return $this->schemaManager->introspectSchema(); } public function createToSchema(Schema $fromSchema): Schema @@ -47,9 +47,8 @@ public function createToSchema(Schema $fromSchema): Schema /** @return string[] */ public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array { - return $this->schemaManager->createComparator()->compareSchemas( - $fromSchema, - $toSchema - )->toSql($this->platform); + return $this->platform->getAlterSchemaSQL( + $this->schemaManager->createComparator()->compareSchemas($fromSchema, $toSchema) + ); } } diff --git a/lib/Doctrine/Migrations/SchemaDumper.php b/lib/Doctrine/Migrations/SchemaDumper.php index 0d8111757..ba844cec8 100644 --- a/lib/Doctrine/Migrations/SchemaDumper.php +++ b/lib/Doctrine/Migrations/SchemaDumper.php @@ -78,7 +78,7 @@ public function dump( bool $formatted = false, int $lineLength = 120 ): string { - $schema = $this->schemaManager->createSchema(); + $schema = $this->schemaManager->introspectSchema(); $up = []; $down = []; @@ -150,7 +150,8 @@ private function shouldSkipTable(Table $table, array $excludedTablesRegexes): bo * * @internal * - * @param mixed[] $matches + * @param mixed[] $matches + * @param int-mask-of $flags */ private static function pregMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 8e576b046..cf09a77f0 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -53,6 +53,12 @@ parameters: message: '~^Call to function method_exists\(\) with Doctrine\\Migrations\\Metadata\\Storage\\MetadataStorage and ''getSql'' will always evaluate to true\.$~' path: lib/Doctrine/Migrations/Version/DbalExecutor.php + # TODO: deprecate using the connection event manager and expose + # our own event manager instead. + - + message: '~^Call to deprecated method getEventManager\(\) of class Doctrine\\DBAL\\Connection\.$~' + path: lib/Doctrine/Migrations/DependencyFactory.php + symfony: console_application_loader: tests/Doctrine/Migrations/Tests/doctrine-migrations-phpstan-app.php includes: diff --git a/tests/Doctrine/Migrations/Tests/Generator/DiffGeneratorTest.php b/tests/Doctrine/Migrations/Tests/Generator/DiffGeneratorTest.php index cf9b71f21..68cd40475 100644 --- a/tests/Doctrine/Migrations/Tests/Generator/DiffGeneratorTest.php +++ b/tests/Doctrine/Migrations/Tests/Generator/DiffGeneratorTest.php @@ -82,7 +82,7 @@ static function ($name): bool { ->method('createSchema'); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($fromSchema); $this->schemaProvider->expects(self::once()) @@ -94,10 +94,15 @@ static function ($name): bool { ->will(self::onConsecutiveCalls('schema.table_name2', 'schema.table_name3')); $schemaDiff = $this->createStub(SchemaDiff::class); - $schemaDiff->method('toSql')->willReturn(self::onConsecutiveCalls( - ['UPDATE table SET value = 2'], - ['UPDATE table SET value = 1'] - )); + + $this->platform->method('getAlterSchemaSQL')->willReturnCallback(static function (): array { + static $i = 0; + if ($i++ === 0) { + return ['UPDATE table SET value = 2']; + } + + return ['UPDATE table SET value = 1']; + }); // regular mocks cannot be used here, because the method is static $comparator = new class extends Comparator { @@ -158,7 +163,7 @@ public function testGenerateFromEmptySchema(): void ->willReturn($emptySchema); $this->schemaManager->expects(self::never()) - ->method('createSchema'); + ->method('introspectSchema'); $this->schemaProvider->expects(self::once()) ->method('createSchema') @@ -168,10 +173,14 @@ public function testGenerateFromEmptySchema(): void ->method('dropTable'); $schemaDiff = $this->createStub(SchemaDiff::class); - $schemaDiff->method('toSql')->willReturn(self::onConsecutiveCalls( - ['CREATE TABLE table_name'], - ['DROP TABLE table_name'] - )); + $this->platform->method('getAlterSchemaSQL')->willReturnCallback(static function (): array { + static $i = 0; + if ($i++ === 0) { + return ['CREATE TABLE table_name']; + } + + return ['DROP TABLE table_name']; + }); // regular mocks cannot be used here, because the method is static $comparator = new class extends Comparator { diff --git a/tests/Doctrine/Migrations/Tests/Metadata/Storage/TableMetadataStorageTest.php b/tests/Doctrine/Migrations/Tests/Metadata/Storage/TableMetadataStorageTest.php index e8c643cb6..ed24d0919 100644 --- a/tests/Doctrine/Migrations/Tests/Metadata/Storage/TableMetadataStorageTest.php +++ b/tests/Doctrine/Migrations/Tests/Metadata/Storage/TableMetadataStorageTest.php @@ -115,7 +115,7 @@ public function testTableStructureUpdate(): void $storage->ensureInitialized(); - $table = $this->schemaManager->listTableDetails($config->getTableName()); + $table = $this->schemaManager->introspectTable($config->getTableName()); self::assertInstanceOf(StringType::class, $table->getColumn('b')->getType()); self::assertInstanceOf(DateTimeType::class, $table->getColumn('c')->getType()); @@ -156,7 +156,7 @@ public function testTableStructure(): void $storage->ensureInitialized(); - $table = $this->schemaManager->listTableDetails($config->getTableName()); + $table = $this->schemaManager->introspectTable($config->getTableName()); self::assertInstanceOf(StringType::class, $table->getColumn('b')->getType()); self::assertInstanceOf(DateTimeType::class, $table->getColumn('c')->getType()); diff --git a/tests/Doctrine/Migrations/Tests/SchemaDumperTest.php b/tests/Doctrine/Migrations/Tests/SchemaDumperTest.php index 227ee7694..7a4720213 100644 --- a/tests/Doctrine/Migrations/Tests/SchemaDumperTest.php +++ b/tests/Doctrine/Migrations/Tests/SchemaDumperTest.php @@ -42,7 +42,7 @@ public function testDumpNoTablesException(): void $schema = $this->createMock(Schema::class); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($schema); $schema->expects(self::once()) @@ -62,7 +62,7 @@ public function testDump(): void $schema = $this->createMock(Schema::class); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($schema); $schema->expects(self::once()) @@ -106,7 +106,7 @@ public function testExcludedTableIsNotInTheDump(): void $schema = $this->createMock(Schema::class); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($schema); $schema->expects(self::once()) @@ -145,7 +145,7 @@ public function testRegexErrorsAreConvertedToExceptions(): void $schema = $this->createMock(Schema::class); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($schema); $schema->expects(self::once()) @@ -168,7 +168,7 @@ public function testExcludedTableViaParamIsNotInTheDump(): void $schema = $this->createMock(Schema::class); $this->schemaManager->expects(self::once()) - ->method('createSchema') + ->method('introspectSchema') ->willReturn($schema); $schema->expects(self::once()) diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php index 6bf723ae8..71aa4344d 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php @@ -5,7 +5,6 @@ namespace Doctrine\Migrations\Tests\Tools\Console\Command; use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Types\Types; use Doctrine\Migrations\AbstractMigration; use Doctrine\Migrations\Configuration\Configuration; @@ -312,7 +311,7 @@ public function testExecuteMigrateUpdatesMigrationsTableWhenNeeded(): void $this->migrateCommandTester->execute([], ['interactive' => false]); $refreshedTable = $this->connection->createSchemaManager() - ->listTableDetails($this->metadataConfiguration->getTableName()); + ->introspectTable($this->metadataConfiguration->getTableName()); self::assertFalse($refreshedTable->hasColumn('extra')); } @@ -326,7 +325,7 @@ public function testExecuteMigrateDoesNotUpdateMigrationsTableWhenSyaingNo(): vo $this->migrateCommandTester->execute([]); $refreshedTable = $this->connection->createSchemaManager() - ->listTableDetails($this->metadataConfiguration->getTableName()); + ->introspectTable($this->metadataConfiguration->getTableName()); self::assertTrue($refreshedTable->hasColumn('extra')); } @@ -493,13 +492,13 @@ private function alterMetadataTable(): void { $schemaManager = $this->connection->createSchemaManager(); $originalTable = $schemaManager - ->listTableDetails($this->metadataConfiguration->getTableName()); + ->introspectTable($this->metadataConfiguration->getTableName()); $modifiedTable = clone $originalTable; $modifiedTable->addColumn('extra', Types::STRING, ['notnull' => false]); - $diff = $schemaManager->createComparator()->diffTable($originalTable, $modifiedTable); - if (! ($diff instanceof TableDiff)) { + $diff = $schemaManager->createComparator()->compareTables($originalTable, $modifiedTable); + if ($diff->isEmpty()) { return; }