1212namespace Symfony \Bridge \Doctrine \SchemaListener ;
1313
1414use Doctrine \DBAL \Connection ;
15+ use Doctrine \DBAL \Exception \ConnectionException ;
16+ use Doctrine \DBAL \Exception \DatabaseObjectExistsException ;
1517use Doctrine \DBAL \Exception \DatabaseObjectNotFoundException ;
16- use Doctrine \DBAL \Exception \TableNotFoundException ;
1718use Doctrine \DBAL \Schema \Name \Identifier ;
1819use Doctrine \DBAL \Schema \Name \UnqualifiedName ;
1920use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
@@ -29,32 +30,47 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
2930 {
3031 return static function (\Closure $ exec ) use ($ connection ): bool {
3132 $ schemaManager = method_exists ($ connection , 'createSchemaManager ' ) ? $ connection ->createSchemaManager () : $ connection ->getSchemaManager ();
32- $ checkTable = ' schema_subscriber_check_ ' . bin2hex (random_bytes (7 ));
33- $ table = new Table ($ checkTable );
33+ $ key = bin2hex (random_bytes (7 ));
34+ $ table = new Table (' _schema_subscriber_check ' );
3435 $ table ->addColumn ('id ' , Types::INTEGER )
3536 ->setAutoincrement (true )
3637 ->setNotnull (true );
38+ $ table ->addColumn ('key ' , Types::STRING )
39+ ->setLength (14 )
40+ ->setNotNull (true )
41+ ;
3742
3843 if (class_exists (PrimaryKeyConstraint::class)) {
3944 $ table ->addPrimaryKeyConstraint (new PrimaryKeyConstraint (null , [new UnqualifiedName (Identifier::unquoted ('id ' ))], true ));
4045 } else {
4146 $ table ->setPrimaryKey (['id ' ]);
4247 }
4348
44- $ 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 ]);
4555
4656 try {
47- $ exec (\sprintf ('DROP TABLE %s ' , $ checkTable ));
48- } catch (\Exception ) {
49- // ignore
57+ $ exec ('DELETE FROM _schema_subscriber_check WHERE key == :key ' , ['key ' => $ key ], ['key ' => Types::STRING ]);
58+ } catch (DatabaseObjectNotFoundException |ConnectionException ) {
5059 }
5160
5261 try {
53- $ 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 ();
5467
55- return false ;
56- } catch (DatabaseObjectNotFoundException ) {
57- return true ;
68+ if (!$ count ) {
69+ try {
70+ $ schemaManager ->dropTable ('_schema_subscriber_check ' );
71+ } catch (DatabaseObjectNotFoundException ) {
72+ }
73+ }
5874 }
5975 };
6076 }
0 commit comments