Skip to content

Commit

Permalink
When dropping schema, drop sequences before tables
Browse files Browse the repository at this point in the history
When dropping a schema, the DBAL explicitly drops all schema sequences,
even if they are owned by one of the tables being dropped.

As a workaround for the regression, we will drop sequences first,
which is how it is implemented in `DropSchemaSqlCollector`.

Co-authored-by: HypeMC <hypemc@gmail.com>
  • Loading branch information
morozov and HypeMC committed Aug 20, 2022
1 parent d6798d7 commit 645ea4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SQL/Builder/DropSchemaObjectsSQLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +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->buildTableStatements($schema->getTables()),
);
}

Expand Down
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 645ea4e

Please sign in to comment.