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