|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema; |
|
21
|
|
|
|
|
22
|
|
|
use \Doctrine\DBAL\Platforms\AbstractPlatform; |
|
23
|
|
|
use function array_merge; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Schema Diff. |
|
27
|
|
|
* |
|
28
|
|
|
* @link www.doctrine-project.org |
|
29
|
|
|
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. |
|
30
|
|
|
* @license http://ez.no/licenses/new_bsd New BSD License |
|
31
|
|
|
* @since 2.0 |
|
32
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class SchemaDiff |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Doctrine\DBAL\Schema\Schema |
|
38
|
|
|
*/ |
|
39
|
|
|
public $fromSchema; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* All added namespaces. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string[] |
|
45
|
|
|
*/ |
|
46
|
|
|
public $newNamespaces = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* All removed namespaces. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public $removedNamespaces = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* All added tables. |
|
57
|
|
|
* |
|
58
|
|
|
* @var \Doctrine\DBAL\Schema\Table[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public $newTables = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* All changed tables. |
|
64
|
|
|
* |
|
65
|
|
|
* @var \Doctrine\DBAL\Schema\TableDiff[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public $changedTables = []; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* All removed tables. |
|
71
|
|
|
* |
|
72
|
|
|
* @var \Doctrine\DBAL\Schema\Table[] |
|
73
|
|
|
*/ |
|
74
|
|
|
public $removedTables = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public $newSequences = []; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
83
|
|
|
*/ |
|
84
|
|
|
public $changedSequences = []; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
88
|
|
|
*/ |
|
89
|
|
|
public $removedSequences = []; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[] |
|
93
|
|
|
*/ |
|
94
|
|
|
public $orphanedForeignKeys = []; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var \Doctrine\DBAL\Schema\View[] |
|
98
|
|
|
*/ |
|
99
|
|
|
public $newViews = []; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var \Doctrine\DBAL\Schema\View[] |
|
103
|
|
|
*/ |
|
104
|
|
|
public $changedViews = []; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var \Doctrine\DBAL\Schema\View[] |
|
108
|
|
|
*/ |
|
109
|
|
|
public $removedViews = []; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Constructs an SchemaDiff object. |
|
113
|
|
|
* |
|
114
|
|
|
* @param \Doctrine\DBAL\Schema\Table[] $newTables |
|
115
|
|
|
* @param \Doctrine\DBAL\Schema\TableDiff[] $changedTables |
|
116
|
|
|
* @param \Doctrine\DBAL\Schema\Table[] $removedTables |
|
117
|
|
|
* @param \Doctrine\DBAL\Schema\Schema|null $fromSchema |
|
118
|
|
|
*/ |
|
119
|
629 |
|
public function __construct($newTables = [], $changedTables = [], $removedTables = [], Schema $fromSchema = null) |
|
120
|
|
|
{ |
|
121
|
629 |
|
$this->newTables = $newTables; |
|
122
|
629 |
|
$this->changedTables = $changedTables; |
|
123
|
629 |
|
$this->removedTables = $removedTables; |
|
124
|
629 |
|
$this->fromSchema = $fromSchema; |
|
125
|
629 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* The to save sql mode ensures that the following things don't happen: |
|
129
|
|
|
* |
|
130
|
|
|
* 1. Tables are deleted |
|
131
|
|
|
* 2. Sequences are deleted |
|
132
|
|
|
* 3. Foreign Keys which reference tables that would otherwise be deleted. |
|
133
|
|
|
* |
|
134
|
|
|
* This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all. |
|
135
|
|
|
* |
|
136
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
20 |
|
public function toSaveSql(AbstractPlatform $platform) |
|
141
|
|
|
{ |
|
142
|
20 |
|
return $this->_toSql($platform, true); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
29 |
|
public function toSql(AbstractPlatform $platform) |
|
151
|
|
|
{ |
|
152
|
29 |
|
return $this->_toSql($platform, false); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
157
|
|
|
* @param bool $saveMode |
|
158
|
|
|
* |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
49 |
|
protected function _toSql(AbstractPlatform $platform, $saveMode = false) |
|
162
|
|
|
{ |
|
163
|
49 |
|
$sql = []; |
|
164
|
|
|
|
|
165
|
49 |
|
if ($platform->supportsSchemas()) { |
|
166
|
29 |
|
foreach ($this->newNamespaces as $newNamespace) { |
|
167
|
29 |
|
$sql[] = $platform->getCreateSchemaSQL($newNamespace); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
49 |
|
if ($platform->supportsForeignKeyConstraints() && $saveMode == false) { |
|
|
|
|
|
|
172
|
9 |
|
foreach ($this->orphanedForeignKeys as $orphanedForeignKey) { |
|
173
|
|
|
$sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable()); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
49 |
|
if ($platform->supportsSequences() == true) { |
|
|
|
|
|
|
178
|
29 |
|
foreach ($this->changedSequences as $sequence) { |
|
179
|
20 |
|
$sql[] = $platform->getAlterSequenceSQL($sequence); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
29 |
|
if ($saveMode === false) { |
|
183
|
9 |
|
foreach ($this->removedSequences as $sequence) { |
|
184
|
|
|
$sql[] = $platform->getDropSequenceSQL($sequence); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
29 |
|
foreach ($this->newSequences as $sequence) { |
|
189
|
20 |
|
$sql[] = $platform->getCreateSequenceSQL($sequence); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
49 |
|
if ($platform->supportsViews() == true) { |
|
|
|
|
|
|
194
|
49 |
|
if ($saveMode === false) { |
|
195
|
29 |
|
foreach ($this->removedViews as $view) { |
|
196
|
|
|
$sql[] = $platform->getDropViewSQL($view->getName()); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
49 |
|
foreach ($this->changedViews as $view) { |
|
200
|
|
|
$sql[] = $platform->getDropViewSQL($view->getName()); |
|
201
|
|
|
$sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql()); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
49 |
|
foreach ($this->newViews as $view) { |
|
205
|
20 |
|
$sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql()); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
49 |
|
$foreignKeySql = []; |
|
210
|
49 |
|
foreach ($this->newTables as $table) { |
|
211
|
40 |
|
$sql = array_merge( |
|
212
|
40 |
|
$sql, |
|
213
|
40 |
|
$platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES) |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
40 |
|
if ($platform->supportsForeignKeyConstraints()) { |
|
217
|
20 |
|
foreach ($table->getForeignKeys() as $foreignKey) { |
|
218
|
40 |
|
$foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
49 |
|
$sql = array_merge($sql, $foreignKeySql); |
|
223
|
|
|
|
|
224
|
49 |
|
if ($saveMode === false) { |
|
225
|
29 |
|
foreach ($this->removedTables as $table) { |
|
226
|
|
|
$sql[] = $platform->getDropTableSQL($table); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
49 |
|
foreach ($this->changedTables as $tableDiff) { |
|
231
|
20 |
|
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff)); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
49 |
|
return $sql; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.