Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions tests/Functional/Schema/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally changed the name so it conflicts with previous test

'quoted schema' => ['"other_schema".some_table'],
'reserved schema' => ['case.some_table'],
] as $testScenario => $testArguments
) {
Expand Down
60 changes: 57 additions & 3 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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);
}
}
Loading