1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Schema\Synchronizer; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
9
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
11
|
|
|
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector; |
12
|
|
|
use function count; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Schema Synchronizer for Default DBAL Connection. |
16
|
|
|
*/ |
17
|
|
|
final class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer |
18
|
|
|
{ |
19
|
|
|
/** @var AbstractPlatform */ |
20
|
|
|
private $platform; |
21
|
|
|
|
22
|
88 |
|
public function __construct(Connection $conn) |
23
|
|
|
{ |
24
|
88 |
|
parent::__construct($conn); |
25
|
88 |
|
$this->platform = $conn->getDatabasePlatform(); |
26
|
88 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
66 |
|
public function getCreateSchema(Schema $createSchema) : array |
32
|
|
|
{ |
33
|
66 |
|
return $createSchema->toSql($this->platform); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
22 |
|
public function getUpdateSchema(Schema $toSchema, bool $noDrops = false) : array |
40
|
|
|
{ |
41
|
22 |
|
$comparator = new Comparator(); |
42
|
22 |
|
$sm = $this->conn->getSchemaManager(); |
43
|
|
|
|
44
|
22 |
|
$fromSchema = $sm->createSchema(); |
45
|
22 |
|
$schemaDiff = $comparator->compare($fromSchema, $toSchema); |
46
|
|
|
|
47
|
22 |
|
if ($noDrops) { |
48
|
|
|
return $schemaDiff->toSaveSql($this->platform); |
49
|
|
|
} |
50
|
|
|
|
51
|
22 |
|
return $schemaDiff->toSql($this->platform); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
22 |
|
public function getDropSchema(Schema $dropSchema) : array |
58
|
|
|
{ |
59
|
22 |
|
$visitor = new DropSchemaSqlCollector($this->platform); |
60
|
22 |
|
$sm = $this->conn->getSchemaManager(); |
61
|
|
|
|
62
|
22 |
|
$fullSchema = $sm->createSchema(); |
63
|
|
|
|
64
|
22 |
|
foreach ($fullSchema->getTables() as $table) { |
65
|
22 |
|
if ($dropSchema->hasTable($table->getName())) { |
66
|
22 |
|
$visitor->acceptTable($table); |
67
|
|
|
} |
68
|
|
|
|
69
|
22 |
|
foreach ($table->getForeignKeys() as $foreignKey) { |
70
|
|
|
if (! $dropSchema->hasTable($table->getName())) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! $dropSchema->hasTable($foreignKey->getForeignTableName())) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$visitor->acceptForeignKey($table, $foreignKey); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
22 |
|
if (! $this->platform->supportsSequences()) { |
83
|
22 |
|
return $visitor->getQueries(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($dropSchema->getSequences() as $sequence) { |
87
|
|
|
$visitor->acceptSequence($sequence); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ($dropSchema->getTables() as $table) { |
91
|
|
|
$primaryKey = $table->getPrimaryKey(); |
92
|
|
|
|
93
|
|
|
if ($primaryKey === null) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$columns = $primaryKey->getColumns(); |
98
|
|
|
|
99
|
|
|
if (count($columns) > 1) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$checkSequence = $table->getName() . '_' . $columns[0] . '_seq'; |
104
|
|
|
if (! $fullSchema->hasSequence($checkSequence)) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$visitor->acceptSequence($fullSchema->getSequence($checkSequence)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $visitor->getQueries(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
22 |
|
public function getDropAllSchema() : array |
118
|
|
|
{ |
119
|
22 |
|
$sm = $this->conn->getSchemaManager(); |
120
|
22 |
|
$visitor = new DropSchemaSqlCollector($this->platform); |
121
|
|
|
|
122
|
22 |
|
$schema = $sm->createSchema(); |
123
|
22 |
|
$schema->visit($visitor); |
124
|
|
|
|
125
|
22 |
|
return $visitor->getQueries(); |
126
|
|
|
} |
127
|
|
|
|
128
|
44 |
|
public function createSchema(Schema $createSchema) : void |
129
|
|
|
{ |
130
|
44 |
|
$this->processSql($this->getCreateSchema($createSchema)); |
131
|
44 |
|
} |
132
|
|
|
|
133
|
|
|
public function updateSchema(Schema $toSchema, bool $noDrops = false) : void |
134
|
|
|
{ |
135
|
|
|
$this->processSql($this->getUpdateSchema($toSchema, $noDrops)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function dropSchema(Schema $dropSchema) : void |
139
|
|
|
{ |
140
|
|
|
$this->processSqlSafely($this->getDropSchema($dropSchema)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function dropAllSchema() : void |
144
|
|
|
{ |
145
|
|
|
$this->processSql($this->getDropAllSchema()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|