12
12
namespace Symfony \Bridge \Doctrine \SchemaListener ;
13
13
14
14
use Doctrine \DBAL \Connection ;
15
+ use Doctrine \DBAL \Exception \ConnectionException ;
16
+ use Doctrine \DBAL \Exception \DatabaseObjectExistsException ;
15
17
use Doctrine \DBAL \Exception \DatabaseObjectNotFoundException ;
16
- use Doctrine \DBAL \Exception \TableNotFoundException ;
17
18
use Doctrine \DBAL \Schema \Name \Identifier ;
18
19
use Doctrine \DBAL \Schema \Name \UnqualifiedName ;
19
20
use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
@@ -29,32 +30,47 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
29
30
{
30
31
return static function (\Closure $ exec ) use ($ connection ): bool {
31
32
$ 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 ' );
34
35
$ table ->addColumn ('id ' , Types::INTEGER )
35
36
->setAutoincrement (true )
36
37
->setNotnull (true );
38
+ $ table ->addColumn ('key ' , Types::STRING )
39
+ ->setLength (14 )
40
+ ->setNotNull (true )
41
+ ;
37
42
38
43
if (class_exists (PrimaryKeyConstraint::class)) {
39
44
$ table ->addPrimaryKeyConstraint (new PrimaryKeyConstraint (null , [new UnqualifiedName (Identifier::unquoted ('id ' ))], true ));
40
45
} else {
41
46
$ table ->setPrimaryKey (['id ' ]);
42
47
}
43
48
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 ]);
45
55
46
56
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 ) {
50
59
}
51
60
52
61
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 ();
54
67
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
+ }
58
74
}
59
75
};
60
76
}
0 commit comments