Skip to content

Commit 4f3efe9

Browse files
committed
Refactor
1 parent dae990b commit 4f3efe9

File tree

3 files changed

+76
-63
lines changed

3 files changed

+76
-63
lines changed

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -516,51 +516,6 @@ public function isDefaultValueChanged(
516516
return false;
517517
}
518518

519-
/**
520-
* TODO move this method to MysqlMigrationBuilder
521-
* Only for MySQL and MariaDB
522-
* Given a column, compute its previous column name present in OpenAPI schema
523-
* @return ?string
524-
* `null` if column is added at last
525-
* 'FIRST' if column is added at first position
526-
* 'AFTER <columnName>' if column is added in between e.g. if 'email' is added after 'username' then 'AFTER username'
527-
*/
528-
public function findPosition(ColumnSchema $column, bool $forDrop = false): ?string
529-
{
530-
$columnNames = array_keys($forDrop ? $this->tableSchema->columns : $this->newColumns);
531-
532-
$key = array_search($column->name, $columnNames);
533-
if ($key > 0) {
534-
$prevColName = $columnNames[$key-1];
535-
536-
if (!$forDrop && !isset($columnNames[$key+1])) { // if new col is added at last then no need to add 'AFTER' SQL part. This is checked as if next column is present or not
537-
return null;
538-
}
539-
540-
// in case of `down()` code of migration, putting 'after <colName>' in add column statement is erroneous because <colName> may not exist.
541-
// Example: From col a, b, c, d, if I drop c and d then their migration code will be generated like:
542-
// `up()` code
543-
// drop c
544-
// drop d
545-
// `down()` code
546-
// add d after c (c does not exist! Error!) (TODO check if c is present in newColumn)
547-
// add c after b (can fix this issue) TODO
548-
if ($forDrop) {
549-
// return null; // TODO this case can be fixed
550-
}
551-
552-
if (array_key_exists($prevColName, $this->newColumns)) {
553-
return self::POS_AFTER . ' ' . $prevColName;
554-
}
555-
return null;
556-
557-
// if no `$columnSchema` is found, previous column does not exist. This happens when 'after column' is not yet added in migration or added after currently undertaken column
558-
} elseif ($key === 0) {
559-
return self::POS_FIRST;
560-
}
561-
562-
return null;
563-
}
564519

565520
public function modifyDesiredFromDbInContextOfDesired(ColumnSchema $desired, ColumnSchema $desiredFromDb): void
566521
{
@@ -582,19 +537,14 @@ public function modifyDesiredInContextOfDesiredFromDb(ColumnSchema $desired, Col
582537
$desired->dbType = $desiredFromDb->dbType;
583538
}
584539

585-
// TODO
586-
public function handleColumnsPositionsChanges(array $haveNames, array $wantNames)
587-
{
588-
$indices = [];
589-
if ($haveNames !== $wantNames) {
590-
foreach ($wantNames as $key => $name) {
591-
if ($name !== $haveNames[$key]) {
592-
$indices[] = $key;
593-
}
594-
}
595-
}
596-
for ($i = 0; $i < count($indices)/2; $i++) {
597-
$this->migration->addUpCode($this->recordBuilder->alterColumn());
598-
}
599-
}
540+
/**
541+
* TODO docs
542+
*/
543+
abstract public function handleColumnsPositionsChanges(array $haveNames, array $wantNames);
544+
545+
546+
/**
547+
* TODO docs
548+
*/
549+
abstract public function findPosition(ColumnSchema $column, bool $forDrop = false): ?string;
600550
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use yii\db\ColumnSchema;
1515
use yii\db\IndexConstraint;
1616
use yii\db\Schema;
17-
use \Yii;
1817
use yii\helpers\ArrayHelper;
19-
use yii\helpers\VarDumper;
2018

2119
final class MysqlMigrationBuilder extends BaseMigrationBuilder
2220
{
@@ -169,4 +167,55 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
169167
public function checkOrder()
170168
{
171169
}
170+
171+
// TODO
172+
public function handleColumnsPositionsChanges(array $haveNames, array $wantNames)
173+
{
174+
$indices = [];
175+
if ($haveNames !== $wantNames) {
176+
foreach ($wantNames as $key => $name) {
177+
if ($name !== $haveNames[$key]) {
178+
$indices[] = $key;
179+
}
180+
}
181+
}
182+
for ($i = 0; $i < count($indices) / 2; $i++) {
183+
$this->migration->addUpCode($this->recordBuilder->alterColumn());
184+
}
185+
}
186+
187+
188+
/**
189+
* TODO move this method to MysqlMigrationBuilder
190+
* Only for MySQL and MariaDB
191+
* Given a column, compute its previous column name present in OpenAPI schema
192+
* @return ?string
193+
* `null` if column is added at last
194+
* 'FIRST' if column is added at first position
195+
* 'AFTER <columnName>' if column is added in between e.g. if 'email' is added after 'username' then 'AFTER username'
196+
*/
197+
public function findPosition(ColumnSchema $column, bool $forDrop = false): ?string
198+
{
199+
$columnNames = array_keys($forDrop ? $this->tableSchema->columns : $this->newColumns);
200+
201+
$key = array_search($column->name, $columnNames);
202+
if ($key > 0) {
203+
$prevColName = $columnNames[$key - 1];
204+
205+
if (!$forDrop && !isset($columnNames[$key + 1])) { // if new col is added at last then no need to add 'AFTER' SQL part. This is checked as if next column is present or not
206+
return null;
207+
}
208+
209+
if (array_key_exists($prevColName, $this->newColumns)) {
210+
return self::POS_AFTER . ' ' . $prevColName;
211+
}
212+
return null;
213+
214+
// if no `$columnSchema` is found, previous column does not exist. This happens when 'after column' is not yet added in migration or added after currently undertaken column
215+
} elseif ($key === 0) {
216+
return self::POS_FIRST;
217+
}
218+
219+
return null;
220+
}
172221
}

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use cebe\yii2openapi\lib\items\DbIndex;
1111
use yii\db\ColumnSchema;
12-
use yii\helpers\VarDumper;
1312
use yii\helpers\ArrayHelper;
1413

1514
final class PostgresMigrationBuilder extends BaseMigrationBuilder
@@ -248,4 +247,19 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
248247
$desired->size = $current->size;
249248
}
250249
}
250+
251+
/**
252+
* {@inheritDoc}
253+
*/
254+
public function handleColumnsPositionsChanges(array $haveNames, array $wantNames)
255+
{
256+
}
257+
258+
259+
/**
260+
* {@inheritDoc}
261+
*/
262+
public function findPosition(ColumnSchema $column, bool $forDrop = false): ?string
263+
{
264+
}
251265
}

0 commit comments

Comments
 (0)