|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
//@codingStandardsIgnoreFile |
|
4
|
|
|
// This class is created based on Doctrine\DBAL\Schema\SchemaDiff, |
|
5
|
|
|
// in order to disable automatic rename of autogenerated indices |
|
6
|
|
|
// as since v2.5.1 schema update force index renaming which is very heavy operation |
|
7
|
|
|
// for existing database and require a lot of effort from developers |
|
8
|
|
|
// to prepare proper migrations as well. |
|
9
|
|
|
// For details see _toSql and disableIndicesRenaming methods. |
|
10
|
|
|
// {@see \RDV\Bundle\MigrationBundle\EventListener\ConsoleCommandListener::onConsoleCommand} |
|
11
|
|
|
// @todo because of this change we have to watch changes in Doctrine\DBAL\Schema\SchemaDiff |
|
12
|
|
|
|
|
13
|
|
|
namespace RDV\Bundle\MigrationBundle\Migration\Schema; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
16
|
|
|
|
|
17
|
|
|
class SchemaDiff |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Doctrine\DBAL\Schema\Schema |
|
21
|
|
|
*/ |
|
22
|
|
|
public $fromSchema; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* All added namespaces. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string[] |
|
28
|
|
|
*/ |
|
29
|
|
|
public $newNamespaces = array(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* All removed namespaces. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public $removedNamespaces = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* All added tables. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \Doctrine\DBAL\Schema\Table[] |
|
42
|
|
|
*/ |
|
43
|
|
|
public $newTables = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* All changed tables. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \Doctrine\DBAL\Schema\TableDiff[] |
|
49
|
|
|
*/ |
|
50
|
|
|
public $changedTables = array(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* All removed tables. |
|
54
|
|
|
* |
|
55
|
|
|
* @var \Doctrine\DBAL\Schema\Table[] |
|
56
|
|
|
*/ |
|
57
|
|
|
public $removedTables = array(); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
61
|
|
|
*/ |
|
62
|
|
|
public $newSequences = array(); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public $changedSequences = array(); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var \Doctrine\DBAL\Schema\Sequence[] |
|
71
|
|
|
*/ |
|
72
|
|
|
public $removedSequences = array(); |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public $orphanedForeignKeys = array(); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Constructs an SchemaDiff object. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Doctrine\DBAL\Schema\Table[] $newTables |
|
83
|
|
|
* @param \Doctrine\DBAL\Schema\TableDiff[] $changedTables |
|
84
|
|
|
* @param \Doctrine\DBAL\Schema\Table[] $removedTables |
|
85
|
|
|
* @param \Doctrine\DBAL\Schema\Schema|null $fromSchema |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct($newTables = array(), $changedTables = array(), $removedTables = array(), $fromSchema = null) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->newTables = $newTables; |
|
90
|
|
|
$this->changedTables = $changedTables; |
|
91
|
|
|
$this->removedTables = $removedTables; |
|
92
|
|
|
$this->fromSchema = $fromSchema; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The to save sql mode ensures that the following things don't happen: |
|
97
|
|
|
* |
|
98
|
|
|
* 1. Tables are deleted |
|
99
|
|
|
* 2. Sequences are deleted |
|
100
|
|
|
* 3. Foreign Keys which reference tables that would otherwise be deleted. |
|
101
|
|
|
* |
|
102
|
|
|
* This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function toSaveSql(AbstractPlatform $platform) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->_toSql($platform, true); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function toSql(AbstractPlatform $platform) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->_toSql($platform, false); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
125
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
128
|
|
|
* @param boolean $saveMode |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function _toSql(AbstractPlatform $platform, $saveMode = false) |
|
133
|
|
|
{ |
|
134
|
|
|
// Disable renaming of indices |
|
135
|
|
|
$this->disableIndicesRenaming(); |
|
136
|
|
|
|
|
137
|
|
|
$sql = array(); |
|
138
|
|
|
|
|
139
|
|
|
if ($platform->supportsSchemas()) { |
|
140
|
|
|
foreach ($this->newNamespaces as $newNamespace) { |
|
141
|
|
|
$sql[] = $platform->getCreateSchemaSQL($newNamespace); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ($platform->supportsForeignKeyConstraints() && $saveMode == false) { |
|
|
|
|
|
|
146
|
|
|
foreach ($this->orphanedForeignKeys as $orphanedForeignKey) { |
|
147
|
|
|
$sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName()); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($platform->supportsSequences() == true) { |
|
|
|
|
|
|
152
|
|
|
foreach ($this->changedSequences as $sequence) { |
|
153
|
|
|
$sql[] = $platform->getAlterSequenceSQL($sequence); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if ($saveMode === false) { |
|
157
|
|
|
foreach ($this->removedSequences as $sequence) { |
|
158
|
|
|
$sql[] = $platform->getDropSequenceSQL($sequence); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ($this->newSequences as $sequence) { |
|
163
|
|
|
$sql[] = $platform->getCreateSequenceSQL($sequence); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$foreignKeySql = array(); |
|
168
|
|
|
foreach ($this->newTables as $table) { |
|
169
|
|
|
$sql = array_merge( |
|
170
|
|
|
$sql, |
|
171
|
|
|
$platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
if ($platform->supportsForeignKeyConstraints()) { |
|
175
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) { |
|
176
|
|
|
$foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
$sql = array_merge($sql, $foreignKeySql); |
|
181
|
|
|
|
|
182
|
|
|
if ($saveMode === false) { |
|
183
|
|
|
foreach ($this->removedTables as $table) { |
|
184
|
|
|
$sql[] = $platform->getDropTableSQL($table); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
foreach ($this->changedTables as $tableDiff) { |
|
189
|
|
|
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $sql; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
protected function disableIndicesRenaming() |
|
196
|
|
|
{ |
|
197
|
|
|
foreach ($this->changedTables as $table) { |
|
198
|
|
|
$table->renamedIndexes = []; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.