diff --git a/tests/Functional/Schema/SchemaManagerTest.php b/tests/Functional/Schema/SchemaManagerTest.php index 273084e4117..7f5405a3a1e 100644 --- a/tests/Functional/Schema/SchemaManagerTest.php +++ b/tests/Functional/Schema/SchemaManagerTest.php @@ -7,7 +7,6 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Comparator; -use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -37,22 +36,19 @@ public function testEmptyDiffRegardlessOfForeignTableQuotes( self::markTestSkipped('Platform does not support schemas.'); } - $this->dropTableIfExists('other_schema.other_table'); - $this->dropTableIfExists('other_schema."user"'); - $this->dropSchemaIfExists('other_schema'); + $this->dropAndCreateSchema('other_schema'); $tableForeign = new Table($foreignTableName); $tableForeign->addColumn('id', 'integer'); $tableForeign->setPrimaryKey(['id']); + $this->dropAndCreateTable($tableForeign); $tableTo = new Table('other_schema.other_table'); $tableTo->addColumn('id', 'integer'); $tableTo->addColumn('user_id', 'integer'); $tableTo->setPrimaryKey(['id']); - $tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id'], []); - - $schemaTo = new Schema([$tableForeign, $tableTo]); - $this->schemaManager->createSchemaObjects($schemaTo); + $tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id']); + $this->dropAndCreateTable($tableTo); $schemaFrom = $this->schemaManager->introspectSchema(); $tableFrom = $schemaFrom->getTable('other_schema.other_table'); @@ -88,12 +84,8 @@ public function testDropIndexInAnotherSchema(callable $comparatorFactory, string self::markTestSkipped('Platform does not support schemas.'); } - $this->dropTableIfExists('test_drop_index_schema.some_table'); - $this->dropSchemaIfExists('test_drop_index_schema'); - $this->connection->executeStatement('CREATE SCHEMA test_drop_index_schema'); - $this->dropTableIfExists('"case".some_table'); - $this->dropSchemaIfExists('"case"'); - $this->connection->executeStatement('CREATE SCHEMA "case"'); + $this->dropAndCreateSchema('other_schema'); + $this->dropAndCreateSchema('case'); $tableFrom = new Table($tableName); $tableFrom->addColumn('id', Types::INTEGER); @@ -119,8 +111,8 @@ public static function dataDropIndexInAnotherSchema(): iterable foreach ( [ 'default schema' => ['some_table'], - 'unquoted schema' => ['test_drop_index_schema.some_table'], - 'quoted schema' => ['"test_drop_index_schema".some_table'], + 'unquoted schema' => ['other_schema.some_table'], + 'quoted schema' => ['"other_schema".some_table'], 'reserved schema' => ['case.some_table'], ] as $testScenario => $testArguments ) { diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 827f3f30f05..283a3f16a49 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -5,9 +5,13 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException; +use Doctrine\DBAL\Schema\Identifier; +use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; use PHPUnit\Framework\TestCase; +use function count; + abstract class FunctionalTestCase extends TestCase { /** @@ -104,13 +108,63 @@ public function dropAndCreateTable(Table $table): void * * @throws Exception */ - public function dropSchemaIfExists(string $name): void + public function dropSchemaIfExists(string $schemaName): void { - $schemaManager = $this->connection->createSchemaManager(); + $platform = $this->connection->getDatabasePlatform(); + if (! $platform->supportsSchemas()) { + throw Exception::notSupported(__METHOD__); + } + + $schemaName = (new Identifier($schemaName))->getName(); + $schemaManager = $this->connection->createSchemaManager(); + $databaseSchema = $schemaManager->introspectSchema(); + + $sequencesToDrop = []; + foreach ($databaseSchema->getSequences() as $sequence) { + if ($sequence->getNamespaceName() !== $schemaName) { + continue; + } + + $sequencesToDrop[] = $sequence; + } + + $tablesToDrop = []; + foreach ($databaseSchema->getTables() as $table) { + if ($table->getNamespaceName() !== $schemaName) { + continue; + } + + $tablesToDrop[] = $table; + } + + if (count($sequencesToDrop) > 0 || count($tablesToDrop) > 0) { + $schemaManager->dropSchemaObjects(new Schema($tablesToDrop, $sequencesToDrop)); + } try { - $schemaManager->dropSchema($name); + $quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform); + $schemaManager->dropSchema($quotedSchemaName); } catch (DatabaseObjectNotFoundException $e) { } } + + /** + * Drops and creates a new schema. + * + * @throws Exception + */ + public function dropAndCreateSchema(string $schemaName): void + { + $platform = $this->connection->getDatabasePlatform(); + if (! $platform->supportsSchemas()) { + throw Exception::notSupported(__METHOD__); + } + + $schemaManager = $this->connection->createSchemaManager(); + $quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform); + $schemaToCreate = new Schema([], [], null, [$quotedSchemaName]); + + $this->dropSchemaIfExists($quotedSchemaName); + $schemaManager->createSchemaObjects($schemaToCreate); + } }