Skip to content

Commit ec4ed73

Browse files
committed
Refactor SchemaListener isSameDatabaseChecker to use always the same table name
1 parent 18d7668 commit ec4ed73

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
7.3
1011
---

src/Symfony/Bridge/Doctrine/SchemaListener/AbstractSchemaListener.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Bridge\Doctrine\SchemaListener;
1313

1414
use Doctrine\DBAL\Connection;
15-
use Doctrine\DBAL\Exception\TableNotFoundException;
15+
use Doctrine\DBAL\Exception\ConnectionException;
16+
use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
17+
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
1618
use Doctrine\DBAL\Schema\Name\Identifier;
1719
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1820
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
@@ -28,32 +30,47 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
2830
{
2931
return static function (\Closure $exec) use ($connection): bool {
3032
$schemaManager = method_exists($connection, 'createSchemaManager') ? $connection->createSchemaManager() : $connection->getSchemaManager();
31-
$checkTable = 'schema_subscriber_check_'.bin2hex(random_bytes(7));
32-
$table = new Table($checkTable);
33+
$key = bin2hex(random_bytes(7));
34+
$table = new Table('_schema_subscriber_check');
3335
$table->addColumn('id', Types::INTEGER)
3436
->setAutoincrement(true)
3537
->setNotnull(true);
38+
$table->addColumn('key', Types::STRING)
39+
->setLength(14)
40+
->setNotNull(true)
41+
;
3642

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

43-
$schemaManager->createTable($table);
49+
try {
50+
$schemaManager->createTable($table);
51+
} catch (DatabaseObjectExistsException) {
52+
}
53+
54+
$connection->executeStatement('INSERT INTO _schema_subscriber_check (key) VALUES (:key)', ['key' => $key], ['key' => Types::STRING]);
4455

4556
try {
46-
$exec(\sprintf('DROP TABLE %s', $checkTable));
47-
} catch (\Exception) {
48-
// ignore
57+
$exec('DELETE FROM _schema_subscriber_check WHERE key == :key', ['key' => $key], ['key' => Types::STRING]);
58+
} catch (DatabaseObjectNotFoundException|ConnectionException) {
4959
}
5060

5161
try {
52-
$schemaManager->dropTable($checkTable);
62+
$rowCount = $connection->executeStatement('DELETE FROM _schema_subscriber_check WHERE key == :key', ['key' => $key], ['key' => Types::STRING]);
63+
64+
return 0 === $rowCount;
65+
} finally {
66+
[$count] = $connection->executeQuery('SELECT count(id) FROM _schema_subscriber_check')->fetchOne();
5367

54-
return false;
55-
} catch (TableNotFoundException) {
56-
return true;
68+
if (! $count) {
69+
try {
70+
$schemaManager->dropTable('_schema_subscriber_check');
71+
} catch (DatabaseObjectNotFoundException) {
72+
}
73+
}
5774
}
5875
};
5976
}

0 commit comments

Comments
 (0)