Skip to content

Commit

Permalink
Merge pull request #5604 from morozov/issues/5598-drop-sequence
Browse files Browse the repository at this point in the history
Fix DropSchemaObjectsSQLBuilder issues
  • Loading branch information
morozov committed Aug 21, 2022
2 parents cc94e25 + 645ea4e commit 22de295
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
23 changes: 1 addition & 22 deletions src/SQL/Builder/DropSchemaObjectsSQLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public function __construct(AbstractPlatform $platform)
public function buildSQL(Schema $schema): array
{
return array_merge(
$this->buildTableStatements($schema->getTables()),
$this->buildSequenceStatements($schema->getSequences()),
$this->buildNamespaceStatements($schema->getNamespaces()),
$this->buildTableStatements($schema->getTables()),
);
}

Expand Down Expand Up @@ -60,24 +59,4 @@ private function buildSequenceStatements(array $sequences): array

return $statements;
}

/**
* @param list<string> $namespaces
*
* @return list<string>
*
* @throws Exception
*/
private function buildNamespaceStatements(array $namespaces): array
{
$statements = [];

if ($this->platform->supportsSchemas()) {
foreach ($namespaces as $namespace) {
$statements[] = $this->platform->getDropSchemaSQL($namespace);
}
}

return $statements;
}
}
35 changes: 35 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\View;
Expand Down Expand Up @@ -304,6 +305,40 @@ protected function assertVarBinaryColumnIsValid(Table $table, string $columnName
self::assertInstanceOf(BlobType::class, $table->getColumn($columnName)->getType());
}

/**
* Although this test would pass in isolation on any platform, we keep it here for the following reasons:
*
* 1. The DBAL currently doesn't properly drop tables in the namespaces that need to be quoted
* (@see testListTableDetailsWhenCurrentSchemaNameQuoted()).
* 2. The schema returned by {@see AbstractSchemaManager::createSchema()} doesn't contain views, so
* {@see AbstractSchemaManager::dropSchemaObjects()} cannot drop the tables that have dependent views
* (@see testListTablesExcludesViews()).
* 3. In the case of inheritance, PHPUnit runs the tests declared immediately in the test class
* and then runs the tests declared in the parent.
*
* This test needs to be executed before the ones it conflicts with, so it has to be declared in the same class.
*/
public function testDropWithAutoincrement(): void
{
$this->dropTableIfExists('test_autoincrement');

$schema = new Schema();
$table = $schema->createTable('test_autoincrement');
$table->addColumn('id', 'integer', [
'notnull' => true,
'autoincrement' => true,
]);
$table->setPrimaryKey(['id']);

$schemaManager = $this->connection->createSchemaManager();
$schemaManager->createSchemaObjects($schema);

$schema = $schemaManager->createSchema();
$schemaManager->dropSchemaObjects($schema);

self::assertFalse($schemaManager->tablesExist(['test_autoincrement']));
}

public function testListTableDetailsWhenCurrentSchemaNameQuoted(): void
{
$this->connection->executeStatement('CREATE SCHEMA "001_test"');
Expand Down

0 comments on commit 22de295

Please sign in to comment.