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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"brainbits/phpcs-standard": "^8.0.0",
"brainbits/phpstan-rules": "^4.0",
"dama/doctrine-test-bundle": "^8.2",
"doctrine/dbal": "^3.9",
"doctrine/dbal": "^4.2",
"ergebnis/phpstan-rules": "^2.8",
"gemorroj/archive7z": "^5.7",
"mikey179/vfsstream": "^1.6.12",
Expand Down
21 changes: 8 additions & 13 deletions src/Schema/Strategy/MysqlBasedSchemaStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function applySchema(SchemaBuilder $schemaBuilder, Connection $connection
{
$platform = $connection->getDatabasePlatform();

$existingTables = $connection->executeQuery($platform->getListTablesSQL())->fetchFirstColumn();
$existingTables = $connection->createSchemaManager()->listTableNames();

if (count($existingTables) > 0) {
if (count(self::$executedStatements) === 0) {
Expand Down Expand Up @@ -107,25 +107,20 @@ public function applyData(DataBuilder $dataBuilder, Connection $connection): voi
}
}

/** @param list<string> $tables */
private function dropTables(array $tables, Connection $connection): void
/** @param list<string> $tableNames */
private function dropTables(array $tableNames, Connection $connection): void
{
$platform = $connection->getDatabasePlatform();
$schemaManager = $connection->createSchemaManager();

try {
$connection->executeStatement('SET foreign_key_checks = 0');

foreach ($tables as $table) {
$listForeignKeysSql = $platform->getListTableForeignKeysSQL($table);
$foreignKeys = $connection->executeQuery($listForeignKeysSql)->fetchFirstColumn();

foreach ($foreignKeys as $foreignKey) {
$dropForeignKeySQL = $platform->getDropForeignKeySQL($foreignKey, $table);
$connection->executeStatement($dropForeignKeySQL);
foreach ($tableNames as $tableName) {
foreach ($schemaManager->listTableForeignKeys($tableName) as $foreignKey) {
$schemaManager->dropForeignKey($foreignKey->getName(), $tableName);
}

$dropTableSQL = $platform->getDropTableSQL($table);
$connection->executeStatement($dropTableSQL);
$schemaManager->dropTable($tableName);
}
} finally {
$connection->executeStatement('SET foreign_key_checks = 1');
Expand Down
21 changes: 8 additions & 13 deletions src/Schema/Strategy/MysqlDamaBasedSchemaStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function applySchema(SchemaBuilder $schemaBuilder, Connection $connection
{
$platform = $connection->getDatabasePlatform();

$existingTables = $connection->executeQuery($platform->getListTablesSQL())->fetchFirstColumn();
$existingTables = $connection->createSchemaManager()->listTableNames();

StaticDriver::rollBack();

Expand Down Expand Up @@ -101,25 +101,20 @@ public function applyData(DataBuilder $dataBuilder, Connection $connection): voi
}
}

/** @param list<string> $tables */
private function dropTables(array $tables, Connection $connection): void
/** @param list<string> $tableNames */
private function dropTables(array $tableNames, Connection $connection): void
{
$platform = $connection->getDatabasePlatform();
$schemaManager = $connection->createSchemaManager();

try {
$connection->executeStatement('SET foreign_key_checks = 0');

foreach ($tables as $table) {
$listForeignKeysSql = $platform->getListTableForeignKeysSQL($table);
$foreignKeys = $connection->executeQuery($listForeignKeysSql)->fetchFirstColumn();

foreach ($foreignKeys as $foreignKey) {
$dropForeignKeySQL = $platform->getDropForeignKeySQL($foreignKey, $table);
$connection->executeStatement($dropForeignKeySQL);
foreach ($tableNames as $tableName) {
foreach ($schemaManager->listTableForeignKeys($tableName) as $foreignKey) {
$schemaManager->dropForeignKey($foreignKey->getName(), $tableName);
}

$dropTableSQL = $platform->getDropTableSQL($table);
$connection->executeStatement($dropTableSQL);
$schemaManager->dropTable($tableName);
}
} finally {
$connection->executeStatement('SET foreign_key_checks = 1');
Expand Down
142 changes: 131 additions & 11 deletions tests/Schema/Strategy/MysqlBasedSchemaStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\MySQLSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function array_keys;
use function array_map;
use function array_values;
use function func_get_arg;
use function func_get_args;
use function Safe\preg_match;
use function str_starts_with;

Expand All @@ -26,11 +33,13 @@ final class MysqlBasedSchemaStrategyTest extends TestCase
use SnapshotTrait;

private MySQLPlatform $platform;
private MySQLSchemaManager&MockObject $schemaManager;
private SchemaBuilder $schemaBuilder;

protected function setUp(): void
{
$this->platform = new MySQLPlatform();
$this->schemaManager = $this->createMock(MySQLSchemaManager::class);

$this->schemaBuilder = $this->createSchemaBuilder();
$this->schemaBuilder->foo();
Expand All @@ -41,6 +50,22 @@ public function testApplySchema(): void
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
$queryLog = new ArrayObject();

$this->schemaManager->expects($this->any())
->method('listTableNames')
->willReturnCallback(
static function () use ($queryLog) {
$result = [];

$queryLog[] = [
'function' => 'listTableNames()',
'parameters' => func_get_args(),
'result' => $result,
];

return $result;
},
);

$connection = $this->createMock(Connection::class);
$connection->expects($this->any())
->method('quoteIdentifier')
Expand All @@ -51,11 +76,16 @@ public function testApplySchema(): void
$connection->expects($this->any())
->method('getDatabasePlatform')
->willReturn($this->platform);
$connection->expects($this->any())
->method('createSchemaManager')
->willReturn($this->schemaManager);
$connection->expects($this->any())
->method('executeStatement')
->willReturnCallback(
static function () use ($queryLog): void {
static function () use ($queryLog): int {
$queryLog[] = ['statement' => func_get_arg(0)];

return 0;
},
);
$connection->expects($this->any())
Expand All @@ -68,7 +98,7 @@ static function () use ($queryLog, $connection) {

$queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result];

return new Result(new ArrayResult($result), $connection);
return new Result(self::createArrayResult($result), $connection);
},
);

Expand All @@ -83,6 +113,57 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
$queryLog = new ArrayObject();

$this->schemaManager->expects($this->once())
->method('listTableNames')
->willReturnCallback(static function () use ($queryLog) {
$result = ['old_table_1', 'old_table_2'];

$queryLog[] = ['function' => 'listTableNames()', 'parameters' => func_get_args(), 'result' => $result];

return $result;
});

$this->schemaManager->expects($this->exactly(2))
->method('listTableForeignKeys')
->willReturnCallback(static function ($tableName) use ($queryLog) {
$result = [];

if ($tableName === 'old_table_1') {
$result = [
new ForeignKeyConstraint([], '', [], 'constraint_1'),
new ForeignKeyConstraint([], '', [], 'constraint_2'),
];
}

$queryLog[] = [
'function' => 'listTableForeignKeys()',
'parameters' => func_get_args(),
'result' => array_map(static fn ($fk) => $fk->getName(), $result),
];

return $result;
});

$this->schemaManager->expects($this->any())
->method('dropForeignKey')
->willReturnCallback(static function ($tableName) use ($queryLog) {
$result = [];

$queryLog[] = ['function' => 'dropForeignKey()', 'parameters' => func_get_args(), 'result' => $result];

return $result;
});

$this->schemaManager->expects($this->any())
->method('dropTable')
->willReturnCallback(static function ($tableName) use ($queryLog) {
$result = [];

$queryLog[] = ['function' => 'dropTable()', 'parameters' => func_get_args(), 'result' => $result];

return $result;
});

$connection = $this->createMock(Connection::class);
$connection->expects($this->any())
->method('quoteIdentifier')
Expand All @@ -93,11 +174,16 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void
$connection->expects($this->any())
->method('getDatabasePlatform')
->willReturn($this->platform);
$connection->expects($this->any())
->method('createSchemaManager')
->willReturn($this->schemaManager);
$connection->expects($this->any())
->method('executeStatement')
->willReturnCallback(
static function () use ($queryLog): void {
static function () use ($queryLog): int {
$queryLog[] = ['statement' => func_get_arg(0)];

return 0;
},
);
$connection->expects($this->any())
Expand All @@ -118,7 +204,7 @@ static function () use ($queryLog, $connection) {

$queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result];

return new Result(new ArrayResult($result), $connection);
return new Result(self::createArrayResult($result), $connection);
},
);

Expand All @@ -133,6 +219,22 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
$queryLog = new ArrayObject();

$this->schemaManager->expects($this->any())
->method('listTableNames')
->willReturnCallback(
static function () use ($queryLog) {
$result = [];

$queryLog[] = [
'function' => 'listTableNames()',
'parameters' => func_get_args(),
'result' => $result,
];

return $result;
},
);

$connection = $this->createMock(Connection::class);
$connection->expects($this->any())
->method('quoteIdentifier')
Expand All @@ -143,11 +245,16 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void
$connection->expects($this->any())
->method('getDatabasePlatform')
->willReturn(new MySQLPlatform());
$connection->expects($this->any())
->method('createSchemaManager')
->willReturn($this->schemaManager);
$connection->expects($this->any())
->method('executeStatement')
->willReturnCallback(
static function () use ($queryLog): void {
static function () use ($queryLog): int {
$queryLog[] = ['statement' => func_get_arg(0)];

return 0;
},
);
$connection->expects($this->any())
Expand All @@ -160,7 +267,7 @@ static function () use ($queryLog, $connection) {

$queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result];

return new Result(new ArrayResult($result), $connection);
return new Result(self::createArrayResult($result), $connection);
},
);

Expand Down Expand Up @@ -194,8 +301,10 @@ public function testDeleteData(): void
$connection->expects($this->any())
->method('executeStatement')
->willReturnCallback(
static function () use ($queryLog): void {
static function () use ($queryLog): int {
$queryLog[] = ['statement' => func_get_arg(0)];

return 0;
},
);
$connection->expects($this->any())
Expand All @@ -213,7 +322,7 @@ static function () use ($queryLog, $connection) {

$queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result];

return new Result(new ArrayResult($result), $connection);
return new Result(self::createArrayResult($result), $connection);
},
);

Expand Down Expand Up @@ -244,8 +353,10 @@ public function testResetSequences(): void
$connection->expects($this->any())
->method('executeStatement')
->willReturnCallback(
static function () use ($queryLog): void {
static function () use ($queryLog): int {
$queryLog[] = ['statement' => func_get_arg(0)];

return 0;
},
);
$connection->expects($this->any())
Expand All @@ -264,7 +375,7 @@ static function () use ($queryLog, $connection) {

$queryLog[] = ['query' => $query, 'parameters' => $parameters, 'result' => $result];

return new Result(new ArrayResult($result), $connection);
return new Result(self::createArrayResult($result), $connection);
},
);

Expand Down Expand Up @@ -298,7 +409,7 @@ public function foo(): void
{
$table = $this->schema->createTable('foo');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('bar', 'string');
$table->addColumn('bar', 'string', ['length' => 255]);
}
};
}
Expand All @@ -307,4 +418,13 @@ private function snapshotPath(): string
{
return __DIR__;
}

/** @param mixed[] $result */
private static function createArrayResult(array $result): ArrayResult
{
$columnNames = array_keys($result[0] ?? []);
$rows = array_map(array_values(...), $result);

return new ArrayResult($columnNames, $rows);
}
}
Loading
Loading