-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenameExtension.php
222 lines (202 loc) · 7.16 KB
/
RenameExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Effiana\MigrationBundle\Migration\Extension;
use Effiana\MigrationBundle\Migration\QueryBag;
use Effiana\MigrationBundle\Migration\Schema\Column;
use Effiana\MigrationBundle\Migration\SqlSchemaUpdateMigrationQuery;
use Effiana\MigrationBundle\Tools\DbIdentifierNameGenerator;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
/**
* Class RenameExtension
* @package Effiana\MigrationBundle\Migration\Extension
*/
class RenameExtension implements DatabasePlatformAwareInterface, NameGeneratorAwareInterface
{
/**
* @var AbstractPlatform
*/
protected $platform;
/**
* @var DbIdentifierNameGenerator
*/
protected $nameGenerator;
/**
* {@inheritdoc}
*/
public function setDatabasePlatform(AbstractPlatform $platform)
{
$this->platform = $platform;
}
/**
* {@inheritdoc}
*/
public function setNameGenerator(DbIdentifierNameGenerator $nameGenerator)
{
$this->nameGenerator = $nameGenerator;
}
/**
* Renames a table
*
* @param Schema $schema
* @param QueryBag $queries
* @param string $oldTableName
* @param string $newTableName
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Schema\SchemaException
*/
public function renameTable(Schema $schema, QueryBag $queries, $oldTableName, $newTableName)
{
$table = $schema->getTable($oldTableName);
$diff = new TableDiff($table->getName());
$diff->newName = $newTableName;
$renameQuery = new SqlSchemaUpdateMigrationQuery(
$this->platform->getAlterTableSQL($diff)
);
$queries->addQuery($renameQuery);
if ($this->platform->supportsSequences()) {
$primaryKey = $schema->getTable($oldTableName)->getPrimaryKeyColumns();
if (count($primaryKey) === 1) {
$primaryKey = reset($primaryKey);
$oldSequenceName = $this->platform->getIdentitySequenceName($oldTableName, $primaryKey);
if ($schema->hasSequence($oldSequenceName)) {
$newSequenceName = $this->platform->getIdentitySequenceName($newTableName, $primaryKey);
if ($this->platform instanceof PostgreSqlPlatform) {
$renameSequenceQuery = new SqlSchemaUpdateMigrationQuery(
"ALTER SEQUENCE $oldSequenceName RENAME TO $newSequenceName"
);
$queries->addQuery($renameSequenceQuery);
}
}
}
}
}
/**
* Renames a column
*
* @param Schema $schema
* @param QueryBag $queries
* @param Table $table
* @param string $oldColumnName
* @param string $newColumnName
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\DBAL\Schema\SchemaException
*/
public function renameColumn(Schema $schema, QueryBag $queries, Table $table, $oldColumnName, $newColumnName)
{
$column = new Column(['column' => $table->getColumn($oldColumnName)]);
$column->changeName($newColumnName);
$diff = new TableDiff($table->getName());
$diff->renamedColumns = [$oldColumnName => $column];
$renameQuery = new SqlSchemaUpdateMigrationQuery(
$this->platform->getAlterTableSQL($diff)
);
$queries->addQuery($renameQuery);
}
/**
* Create an index without check of table and columns existence.
* This method can be helpful when you need to create an index for renamed table or column
*
* @param Schema $schema
* @param QueryBag $queries
* @param string $tableName
* @param string[] $columnNames
* @param string|null $indexName
* @throws \Doctrine\DBAL\DBALException
*/
public function addIndex(
Schema $schema,
QueryBag $queries,
$tableName,
array $columnNames,
$indexName = null
) {
if (!$indexName) {
$indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames);
}
$index = new Index($indexName, $columnNames);
$diff = new TableDiff($tableName);
$diff->addedIndexes = [$indexName => $index];
$renameQuery = new SqlSchemaUpdateMigrationQuery(
$this->platform->getAlterTableSQL($diff)
);
$queries->addQuery($renameQuery);
}
/**
* Create an unique index without check of table and columns existence.
* This method can be helpful when you need to create an index for renamed table or column
*
* @param Schema $schema
* @param QueryBag $queries
* @param string $tableName
* @param string[] $columnNames
* @param string|null $indexName
* @throws \Doctrine\DBAL\DBALException
*/
public function addUniqueIndex(
Schema $schema,
QueryBag $queries,
$tableName,
array $columnNames,
$indexName = null
) {
if (!$indexName) {
$indexName = $this->nameGenerator->generateIndexName($tableName, $columnNames, true);
}
$index = new Index($indexName, $columnNames, true);
$diff = new TableDiff($tableName);
$diff->addedIndexes = [$indexName => $index];
$renameQuery = new SqlSchemaUpdateMigrationQuery(
$this->platform->getAlterTableSQL($diff)
);
$queries->addQuery($renameQuery);
}
/**
* Create a foreign key constraint without check of table and columns existence.
* This method can be helpful when you need to create a constraint for renamed table or column
*
* @param Schema $schema
* @param QueryBag $queries
* @param string $tableName
* @param string $foreignTable
* @param string[] $localColumnNames
* @param string[] $foreignColumnNames
* @param array $options
* @param string|null $constraintName
* @throws \Doctrine\DBAL\DBALException
*/
public function addForeignKeyConstraint(
Schema $schema,
QueryBag $queries,
$tableName,
$foreignTable,
array $localColumnNames,
array $foreignColumnNames,
array $options = [],
$constraintName = null
) {
if (!$constraintName) {
$constraintName = $this->nameGenerator->generateForeignKeyConstraintName(
$tableName,
$localColumnNames
);
}
$constraint = new ForeignKeyConstraint(
$localColumnNames,
$foreignTable,
$foreignColumnNames,
$constraintName,
$options
);
$diff = new TableDiff($tableName);
$diff->addedForeignKeys = [$constraintName => $constraint];
$renameQuery = new SqlSchemaUpdateMigrationQuery(
$this->platform->getAlterTableSQL($diff)
);
$queries->addQuery($renameQuery);
}
}