Skip to content

Commit 73c7ccd

Browse files
authored
Merge pull request #6786 from cristi-contiu/helper-drop-create-schema
Tests: Added drop and create schema helper methods
2 parents c99be81 + 665ff4b commit 73c7ccd

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

tests/Functional/Schema/SchemaManagerTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\DBAL\Exception;
88
use Doctrine\DBAL\Schema\AbstractSchemaManager;
99
use Doctrine\DBAL\Schema\Comparator;
10-
use Doctrine\DBAL\Schema\Schema;
1110
use Doctrine\DBAL\Schema\Table;
1211
use Doctrine\DBAL\Tests\FunctionalTestCase;
1312
use Doctrine\DBAL\Types\Types;
@@ -37,22 +36,19 @@ public function testEmptyDiffRegardlessOfForeignTableQuotes(
3736
self::markTestSkipped('Platform does not support schemas.');
3837
}
3938

40-
$this->dropTableIfExists('other_schema.other_table');
41-
$this->dropTableIfExists('other_schema."user"');
42-
$this->dropSchemaIfExists('other_schema');
39+
$this->dropAndCreateSchema('other_schema');
4340

4441
$tableForeign = new Table($foreignTableName);
4542
$tableForeign->addColumn('id', 'integer');
4643
$tableForeign->setPrimaryKey(['id']);
44+
$this->dropAndCreateTable($tableForeign);
4745

4846
$tableTo = new Table('other_schema.other_table');
4947
$tableTo->addColumn('id', 'integer');
5048
$tableTo->addColumn('user_id', 'integer');
5149
$tableTo->setPrimaryKey(['id']);
52-
$tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id'], []);
53-
54-
$schemaTo = new Schema([$tableForeign, $tableTo]);
55-
$this->schemaManager->createSchemaObjects($schemaTo);
50+
$tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id']);
51+
$this->dropAndCreateTable($tableTo);
5652

5753
$schemaFrom = $this->schemaManager->introspectSchema();
5854
$tableFrom = $schemaFrom->getTable('other_schema.other_table');
@@ -88,12 +84,8 @@ public function testDropIndexInAnotherSchema(callable $comparatorFactory, string
8884
self::markTestSkipped('Platform does not support schemas.');
8985
}
9086

91-
$this->dropTableIfExists('test_drop_index_schema.some_table');
92-
$this->dropSchemaIfExists('test_drop_index_schema');
93-
$this->connection->executeStatement('CREATE SCHEMA test_drop_index_schema');
94-
$this->dropTableIfExists('"case".some_table');
95-
$this->dropSchemaIfExists('"case"');
96-
$this->connection->executeStatement('CREATE SCHEMA "case"');
87+
$this->dropAndCreateSchema('other_schema');
88+
$this->dropAndCreateSchema('case');
9789

9890
$tableFrom = new Table($tableName);
9991
$tableFrom->addColumn('id', Types::INTEGER);
@@ -119,8 +111,8 @@ public static function dataDropIndexInAnotherSchema(): iterable
119111
foreach (
120112
[
121113
'default schema' => ['some_table'],
122-
'unquoted schema' => ['test_drop_index_schema.some_table'],
123-
'quoted schema' => ['"test_drop_index_schema".some_table'],
114+
'unquoted schema' => ['other_schema.some_table'],
115+
'quoted schema' => ['"other_schema".some_table'],
124116
'reserved schema' => ['case.some_table'],
125117
] as $testScenario => $testArguments
126118
) {

tests/FunctionalTestCase.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Exception;
77
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
8+
use Doctrine\DBAL\Schema\Identifier;
9+
use Doctrine\DBAL\Schema\Schema;
810
use Doctrine\DBAL\Schema\Table;
911
use PHPUnit\Framework\TestCase;
1012

13+
use function count;
14+
1115
abstract class FunctionalTestCase extends TestCase
1216
{
1317
/**
@@ -104,13 +108,63 @@ public function dropAndCreateTable(Table $table): void
104108
*
105109
* @throws Exception
106110
*/
107-
public function dropSchemaIfExists(string $name): void
111+
public function dropSchemaIfExists(string $schemaName): void
108112
{
109-
$schemaManager = $this->connection->createSchemaManager();
113+
$platform = $this->connection->getDatabasePlatform();
114+
if (! $platform->supportsSchemas()) {
115+
throw Exception::notSupported(__METHOD__);
116+
}
117+
118+
$schemaName = (new Identifier($schemaName))->getName();
119+
$schemaManager = $this->connection->createSchemaManager();
120+
$databaseSchema = $schemaManager->introspectSchema();
121+
122+
$sequencesToDrop = [];
123+
foreach ($databaseSchema->getSequences() as $sequence) {
124+
if ($sequence->getNamespaceName() !== $schemaName) {
125+
continue;
126+
}
127+
128+
$sequencesToDrop[] = $sequence;
129+
}
130+
131+
$tablesToDrop = [];
132+
foreach ($databaseSchema->getTables() as $table) {
133+
if ($table->getNamespaceName() !== $schemaName) {
134+
continue;
135+
}
136+
137+
$tablesToDrop[] = $table;
138+
}
139+
140+
if (count($sequencesToDrop) > 0 || count($tablesToDrop) > 0) {
141+
$schemaManager->dropSchemaObjects(new Schema($tablesToDrop, $sequencesToDrop));
142+
}
110143

111144
try {
112-
$schemaManager->dropSchema($name);
145+
$quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform);
146+
$schemaManager->dropSchema($quotedSchemaName);
113147
} catch (DatabaseObjectNotFoundException $e) {
114148
}
115149
}
150+
151+
/**
152+
* Drops and creates a new schema.
153+
*
154+
* @throws Exception
155+
*/
156+
public function dropAndCreateSchema(string $schemaName): void
157+
{
158+
$platform = $this->connection->getDatabasePlatform();
159+
if (! $platform->supportsSchemas()) {
160+
throw Exception::notSupported(__METHOD__);
161+
}
162+
163+
$schemaManager = $this->connection->createSchemaManager();
164+
$quotedSchemaName = (new Identifier($schemaName))->getQuotedName($platform);
165+
$schemaToCreate = new Schema([], [], null, [$quotedSchemaName]);
166+
167+
$this->dropSchemaIfExists($quotedSchemaName);
168+
$schemaManager->createSchemaObjects($schemaToCreate);
169+
}
116170
}

0 commit comments

Comments
 (0)