Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DropSchemaObjectsSQLBuilder issues #5604

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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