Skip to content

Commit

Permalink
Address deprecations from doctrine/dbal (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Nov 7, 2022
1 parent c1a230c commit 1f58518
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/Migrations/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -127,7 +127,7 @@ private function createEmptySchema(): Schema

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

private function createToSchema(): Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/Doctrine/Migrations/Provider/DBALSchemaDiffProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
);
}
}
5 changes: 3 additions & 2 deletions lib/Doctrine/Migrations/SchemaDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function dump(
bool $formatted = false,
int $lineLength = 120
): string {
$schema = $this->schemaManager->createSchema();
$schema = $this->schemaManager->introspectSchema();

$up = [];
$down = [];
Expand Down Expand Up @@ -150,7 +150,8 @@ private function shouldSkipTable(Table $table, array $excludedTablesRegexes): bo
*
* @internal
*
* @param mixed[] $matches
* @param mixed[] $matches
* @param int-mask-of<PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL> $flags
*/
private static function pregMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
{
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 19 additions & 10 deletions tests/Doctrine/Migrations/Tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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')
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 5 additions & 5 deletions tests/Doctrine/Migrations/Tests/SchemaDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}
Expand All @@ -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'));
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 1f58518

Please sign in to comment.