|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Internal\DependencyOrderCalculator; |
|
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
7
|
|
|
use function array_merge; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Schema Diff. |
|
11
|
|
|
*/ |
|
12
|
|
|
class SchemaDiff |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Schema|null */ |
|
15
|
|
|
public $fromSchema; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* All added namespaces. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string[] |
|
21
|
|
|
*/ |
|
22
|
|
|
public $newNamespaces = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* All removed namespaces. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string[] |
|
28
|
|
|
*/ |
|
29
|
|
|
public $removedNamespaces = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* All added tables. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Table[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public $newTables = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* All changed tables. |
|
40
|
|
|
* |
|
41
|
|
|
* @var TableDiff[] |
|
42
|
|
|
*/ |
|
43
|
|
|
public $changedTables = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* All removed tables. |
|
47
|
|
|
* |
|
48
|
|
|
* @var Table[] |
|
49
|
|
|
*/ |
|
50
|
|
|
public $removedTables = []; |
|
51
|
|
|
|
|
52
|
|
|
/** @var Sequence[] */ |
|
53
|
|
|
public $newSequences = []; |
|
54
|
|
|
|
|
55
|
|
|
/** @var Sequence[] */ |
|
56
|
|
|
public $changedSequences = []; |
|
57
|
|
|
|
|
58
|
|
|
/** @var Sequence[] */ |
|
59
|
|
|
public $removedSequences = []; |
|
60
|
|
|
|
|
61
|
|
|
/** @var ForeignKeyConstraint[] */ |
|
62
|
|
|
public $orphanedForeignKeys = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructs an SchemaDiff object. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Table[] $newTables |
|
68
|
|
|
* @param TableDiff[] $changedTables |
|
69
|
|
|
* @param Table[] $removedTables |
|
70
|
|
|
*/ |
|
71
|
847 |
|
public function __construct($newTables = [], $changedTables = [], $removedTables = [], ?Schema $fromSchema = null) |
|
72
|
|
|
{ |
|
73
|
847 |
|
$this->newTables = $newTables; |
|
74
|
847 |
|
$this->changedTables = $changedTables; |
|
75
|
847 |
|
$this->removedTables = $removedTables; |
|
76
|
847 |
|
$this->fromSchema = $fromSchema; |
|
77
|
847 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The to save sql mode ensures that the following things don't happen: |
|
81
|
|
|
* |
|
82
|
|
|
* 1. Tables are deleted |
|
83
|
|
|
* 2. Sequences are deleted |
|
84
|
|
|
* 3. Foreign Keys which reference tables that would otherwise be deleted. |
|
85
|
|
|
* |
|
86
|
|
|
* This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string[] |
|
89
|
|
|
*/ |
|
90
|
52 |
|
public function toSaveSql(AbstractPlatform $platform) |
|
91
|
|
|
{ |
|
92
|
52 |
|
return $this->_toSql($platform, true); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string[] |
|
97
|
|
|
*/ |
|
98
|
793 |
|
public function toSql(AbstractPlatform $platform) |
|
99
|
|
|
{ |
|
100
|
793 |
|
return $this->_toSql($platform, false); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param bool $saveMode |
|
105
|
|
|
* |
|
106
|
|
|
* @return string[] |
|
107
|
|
|
*/ |
|
108
|
795 |
|
protected function _toSql(AbstractPlatform $platform, $saveMode = false) |
|
109
|
|
|
{ |
|
110
|
795 |
|
$sql = []; |
|
111
|
|
|
|
|
112
|
795 |
|
if ($platform->supportsSchemas()) { |
|
113
|
331 |
|
foreach ($this->newNamespaces as $newNamespace) { |
|
114
|
331 |
|
$sql[] = $platform->getCreateSchemaSQL($newNamespace); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
795 |
|
if ($platform->supportsForeignKeyConstraints() && $saveMode === false) { |
|
119
|
793 |
|
foreach ($this->orphanedForeignKeys as $orphanedForeignKey) { |
|
120
|
77 |
|
$sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
795 |
|
if ($platform->supportsSequences() === true) { |
|
|
|
|
|
|
125
|
333 |
|
foreach ($this->changedSequences as $sequence) { |
|
126
|
79 |
|
$sql[] = $platform->getAlterSequenceSQL($sequence); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
333 |
|
if ($saveMode === false) { |
|
130
|
331 |
|
foreach ($this->removedSequences as $sequence) { |
|
131
|
77 |
|
$sql[] = $platform->getDropSequenceSQL($sequence); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
333 |
|
foreach ($this->newSequences as $sequence) { |
|
136
|
79 |
|
$sql[] = $platform->getCreateSequenceSQL($sequence); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
795 |
|
$foreignKeySql = []; |
|
141
|
795 |
|
$createFlags = AbstractPlatform::CREATE_INDEXES; |
|
142
|
|
|
|
|
143
|
795 |
|
if (! $platform->supportsCreateDropForeignKeyConstraints()) { |
|
144
|
56 |
|
$createFlags |= AbstractPlatform::CREATE_FOREIGNKEYS; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
795 |
|
foreach ($this->getNewTablesSortedByDependencies() as $table) { |
|
148
|
758 |
|
$sql = array_merge($sql, $platform->getCreateTableSQL($table, $createFlags)); |
|
149
|
|
|
|
|
150
|
758 |
|
if (! $platform->supportsCreateDropForeignKeyConstraints()) { |
|
151
|
56 |
|
continue; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
729 |
|
foreach ($table->getForeignKeys() as $foreignKey) { |
|
155
|
729 |
|
$foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
795 |
|
$sql = array_merge($sql, $foreignKeySql); |
|
159
|
|
|
|
|
160
|
795 |
|
if ($saveMode === false) { |
|
161
|
793 |
|
foreach ($this->removedTables as $table) { |
|
162
|
77 |
|
$sql[] = $platform->getDropTableSQL($table); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
795 |
|
foreach ($this->changedTables as $tableDiff) { |
|
167
|
487 |
|
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
795 |
|
return $sql; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Sorts tables by dependencies so that they are created in the right order. |
|
175
|
|
|
* |
|
176
|
|
|
* This is necessary when one table depends on another while creating foreign key |
|
177
|
|
|
* constraints directly during CREATE TABLE. |
|
178
|
|
|
* |
|
179
|
|
|
* @return array<Table> |
|
180
|
|
|
*/ |
|
181
|
795 |
|
private function getNewTablesSortedByDependencies() |
|
182
|
|
|
{ |
|
183
|
795 |
|
$calculator = new DependencyOrderCalculator(); |
|
184
|
795 |
|
$newTables = []; |
|
185
|
|
|
|
|
186
|
795 |
|
foreach ($this->newTables as $table) { |
|
187
|
758 |
|
$newTables[$table->getName()] = true; |
|
188
|
758 |
|
$calculator->addNode($table->getName(), $table); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
795 |
|
foreach ($this->newTables as $table) { |
|
192
|
758 |
|
foreach ($table->getForeignKeys() as $foreignKey) { |
|
193
|
756 |
|
$foreignTableName = $foreignKey->getForeignTableName(); |
|
194
|
|
|
|
|
195
|
756 |
|
if (! isset($newTables[$foreignTableName])) { |
|
196
|
79 |
|
continue; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
752 |
|
$calculator->addDependency($foreignTableName, $table->getName(), 1); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
795 |
|
return $calculator->sort(); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|