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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate `UniqueEntity::getRequiredOptions()` and `UniqueEntity::getDefaultOption()`
* Use a single table named `_schema_subscriber_check` in schema listeners to detect same database connections

7.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace Symfony\Bridge\Doctrine\SchemaListener;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Schema\Name\Identifier;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
Expand All @@ -29,32 +30,47 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
{
return static function (\Closure $exec) use ($connection): bool {
$schemaManager = method_exists($connection, 'createSchemaManager') ? $connection->createSchemaManager() : $connection->getSchemaManager();
$checkTable = 'schema_subscriber_check_'.bin2hex(random_bytes(7));
$table = new Table($checkTable);
$key = bin2hex(random_bytes(7));
$table = new Table('_schema_subscriber_check');
$table->addColumn('id', Types::INTEGER)
->setAutoincrement(true)
->setNotnull(true);
$table->addColumn('key', Types::STRING)
->setLength(14)
->setNotNull(true)
;

if (class_exists(PrimaryKeyConstraint::class)) {
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
} else {
$table->setPrimaryKey(['id']);
}

$schemaManager->createTable($table);
try {
$schemaManager->createTable($table);
} catch (DatabaseObjectExistsException) {
}

$connection->executeStatement('INSERT INTO _schema_subscriber_check (key) VALUES (:key)', ['key' => $key], ['key' => Types::STRING]);

try {
$exec(\sprintf('DROP TABLE %s', $checkTable));
} catch (\Exception) {
// ignore
$exec('DELETE FROM _schema_subscriber_check WHERE key == :key', ['key' => $key], ['key' => Types::STRING]);
} catch (DatabaseObjectNotFoundException|ConnectionException) {
}

try {
$schemaManager->dropTable($checkTable);
$rowCount = $connection->executeStatement('DELETE FROM _schema_subscriber_check WHERE key == :key', ['key' => $key], ['key' => Types::STRING]);

return 0 === $rowCount;
} finally {
[$count] = $connection->executeQuery('SELECT count(id) FROM _schema_subscriber_check')->fetchOne();

return false;
} catch (DatabaseObjectNotFoundException) {
return true;
if (!$count) {
try {
$schemaManager->dropTable('_schema_subscriber_check');
} catch (DatabaseObjectNotFoundException) {
}
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SchemaListener;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\SchemaListener\AbstractSchemaListener;

#[RequiresPhpExtension('pdo_sqlite')]
class AbstractSchemaListenerTest extends TestCase
{
public function testSameDatabaseChecker()
{
$connectionParams = [
'dbname' => ':memory:',
'driver' => 'pdo_sqlite',
];
// Create two distinct in-memory SQLite databases
$connection1 = DriverManager::getConnection($connectionParams);
$connection2 = DriverManager::getConnection($connectionParams);

self::assertTrue($this->getIsSameDatabaseChecker($connection1)($connection1->executeStatement(...)));
self::assertFalse($this->getIsSameDatabaseChecker($connection1)($connection2->executeStatement(...)));

$remainingTables = $connection1->executeQuery('SELECT name FROM sqlite_schema WHERE name <> "sqlite_sequence"')->fetchFirstColumn();
self::assertSame([], $remainingTables, 'Temporary table was dropped');
}

private function getIsSameDatabaseChecker(Connection $connection): \Closure
{
return (new class extends AbstractSchemaListener {
public function postGenerateSchema($event): void
{
}

public function getIsSameDatabaseChecker(Connection $connection): \Closure
{
return parent::getIsSameDatabaseChecker($connection);
}
})->getIsSameDatabaseChecker($connection);
}
}
Loading