-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAbstractSchemaListener.php
43 lines (35 loc) · 1.28 KB
/
AbstractSchemaListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Doctrine\SchemaListener;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
abstract class AbstractSchemaListener
{
abstract public function postGenerateSchema(GenerateSchemaEventArgs $event): void;
protected function getIsSameDatabaseChecker(Connection $connection): \Closure
{
return static function (\Closure $exec) use ($connection): bool {
$checkTable = 'schema_subscriber_check_'.bin2hex(random_bytes(7));
$connection->executeStatement(sprintf('CREATE TABLE %s (id INTEGER PRIMARY KEY NOT NULL)', $checkTable));
try {
$exec(sprintf('DROP TABLE %s', $checkTable));
} catch (\Exception) {
// ignore
}
try {
$connection->executeStatement(sprintf('DROP TABLE %s', $checkTable));
return false;
} catch (TableNotFoundException) {
return true;
}
};
}
}