|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Migration\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
7
|
|
|
use Doctrine\DBAL\Schema\Index; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
10
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
|
11
|
|
|
use RDV\Bundle\MigrationBundle\Migration\QueryBag; |
|
12
|
|
|
use RDV\Bundle\MigrationBundle\Migration\Schema\Column; |
|
13
|
|
|
use RDV\Bundle\MigrationBundle\Migration\SqlMigrationQuery; |
|
14
|
|
|
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator; |
|
15
|
|
|
|
|
16
|
|
|
class RenameExtension implements DatabasePlatformAwareInterface, NameGeneratorAwareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AbstractPlatform |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $platform; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var DbIdentifierNameGenerator |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $nameGenerator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setDatabasePlatform(AbstractPlatform $platform) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->platform = $platform; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setNameGenerator(DbIdentifierNameGenerator $nameGenerator) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->nameGenerator = $nameGenerator; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Renames a table |
|
46
|
|
|
* |
|
47
|
|
|
* @param Schema $schema |
|
48
|
|
|
* @param QueryBag $queries |
|
49
|
|
|
* @param string $oldTableName |
|
50
|
|
|
* @param string $newTableName |
|
51
|
|
|
*/ |
|
52
|
|
|
public function renameTable(Schema $schema, QueryBag $queries, $oldTableName, $newTableName) |
|
53
|
|
|
{ |
|
54
|
|
|
$table = $schema->getTable($oldTableName); |
|
55
|
|
|
$diff = new TableDiff($table->getName()); |
|
56
|
|
|
$diff->newName = $newTableName; |
|
57
|
|
|
|
|
58
|
|
|
$renameQuery = new SqlMigrationQuery( |
|
59
|
|
|
$this->platform->getAlterTableSQL($diff) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$queries->addQuery($renameQuery); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Renames a column |
|
67
|
|
|
* |
|
68
|
|
|
* @param Schema $schema |
|
69
|
|
|
* @param QueryBag $queries |
|
70
|
|
|
* @param Table $table |
|
71
|
|
|
* @param string $oldColumnName |
|
72
|
|
|
* @param string $newColumnName |
|
73
|
|
|
*/ |
|
74
|
|
|
public function renameColumn(Schema $schema, QueryBag $queries, Table $table, $oldColumnName, $newColumnName) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$column = new Column(['column' => $table->getColumn($oldColumnName)]); |
|
77
|
|
|
$column->changeName($newColumnName); |
|
78
|
|
|
$diff = new TableDiff($table->getName()); |
|
79
|
|
|
$diff->renamedColumns = [$oldColumnName => $column]; |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$renameQuery = new SqlMigrationQuery( |
|
82
|
|
|
$this->platform->getAlterTableSQL($diff) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$queries->addQuery($renameQuery); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create an index without check of table and columns existence. |
|
90
|
|
|
* This method can be helpful when you need to create an index for renamed table or column |
|
91
|
|
|
* |
|
92
|
|
|
* @param Schema $schema |
|
93
|
|
|
* @param QueryBag $queries |
|
94
|
|
|
* @param string $tableName |
|
95
|
|
|
* @param string[] $columnNames |
|
96
|
|
|
* @param string|null $indexName |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function addIndex( |
|
|
|
|
|
|
99
|
|
|
Schema $schema, |
|
|
|
|
|
|
100
|
|
|
QueryBag $queries, |
|
101
|
|
|
$tableName, |
|
102
|
|
|
array $columnNames, |
|
103
|
|
|
$indexName = null |
|
104
|
|
|
) { |
|
105
|
|
|
if (!$indexName) { |
|
|
|
|
|
|
106
|
|
|
$indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames); |
|
107
|
|
|
} |
|
108
|
|
|
$index = new Index($indexName, $columnNames); |
|
109
|
|
|
$diff = new TableDiff($tableName); |
|
110
|
|
|
$diff->addedIndexes = [$indexName => $index]; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$renameQuery = new SqlMigrationQuery( |
|
113
|
|
|
$this->platform->getAlterTableSQL($diff) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$queries->addQuery($renameQuery); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Create an unique index without check of table and columns existence. |
|
121
|
|
|
* This method can be helpful when you need to create an index for renamed table or column |
|
122
|
|
|
* |
|
123
|
|
|
* @param Schema $schema |
|
124
|
|
|
* @param QueryBag $queries |
|
125
|
|
|
* @param string $tableName |
|
126
|
|
|
* @param string[] $columnNames |
|
127
|
|
|
* @param string|null $indexName |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function addUniqueIndex( |
|
|
|
|
|
|
130
|
|
|
Schema $schema, |
|
|
|
|
|
|
131
|
|
|
QueryBag $queries, |
|
132
|
|
|
$tableName, |
|
133
|
|
|
array $columnNames, |
|
134
|
|
|
$indexName = null |
|
135
|
|
|
) { |
|
136
|
|
|
if (!$indexName) { |
|
|
|
|
|
|
137
|
|
|
$indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames, true); |
|
138
|
|
|
} |
|
139
|
|
|
$index = new Index($indexName, $columnNames, true); |
|
140
|
|
|
$diff = new TableDiff($tableName); |
|
141
|
|
|
$diff->addedIndexes = [$indexName => $index]; |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$renameQuery = new SqlMigrationQuery( |
|
144
|
|
|
$this->platform->getAlterTableSQL($diff) |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
$queries->addQuery($renameQuery); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Create a foreign key constraint without check of table and columns existence. |
|
152
|
|
|
* This method can be helpful when you need to create a constraint for renamed table or column |
|
153
|
|
|
* |
|
154
|
|
|
* @param Schema $schema |
|
155
|
|
|
* @param QueryBag $queries |
|
156
|
|
|
* @param string $tableName |
|
157
|
|
|
* @param string $foreignTable |
|
158
|
|
|
* @param string[] $localColumnNames |
|
159
|
|
|
* @param string[] $foreignColumnNames |
|
160
|
|
|
* @param array $options |
|
161
|
|
|
* @param string|null $constraintName |
|
162
|
|
|
*/ |
|
163
|
|
|
public function addForeignKeyConstraint( |
|
164
|
|
|
Schema $schema, |
|
|
|
|
|
|
165
|
|
|
QueryBag $queries, |
|
166
|
|
|
$tableName, |
|
167
|
|
|
$foreignTable, |
|
168
|
|
|
array $localColumnNames, |
|
169
|
|
|
array $foreignColumnNames, |
|
170
|
|
|
array $options = [], |
|
171
|
|
|
$constraintName = null |
|
172
|
|
|
) { |
|
173
|
|
|
if (!$constraintName) { |
|
|
|
|
|
|
174
|
|
|
$constraintName = $this->nameGenerator->generateForeignKeyConstraintName( |
|
175
|
|
|
$tableName, |
|
176
|
|
|
$localColumnNames |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
$constraint = new ForeignKeyConstraint( |
|
180
|
|
|
$localColumnNames, |
|
181
|
|
|
$foreignTable, |
|
182
|
|
|
$foreignColumnNames, |
|
183
|
|
|
$constraintName, |
|
184
|
|
|
$options |
|
185
|
|
|
); |
|
186
|
|
|
$diff = new TableDiff($tableName); |
|
187
|
|
|
$diff->addedForeignKeys = [$constraintName => $constraint]; |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
$renameQuery = new SqlMigrationQuery( |
|
190
|
|
|
$this->platform->getAlterTableSQL($diff) |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
$queries->addQuery($renameQuery); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.