Skip to content

Commit ca22731

Browse files
committed
Add docs + refactor
1 parent e54defe commit ca22731

File tree

4 files changed

+17
-105
lines changed

4 files changed

+17
-105
lines changed

src/db/ColumnSchema.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ class ColumnSchema extends \yii\db\ColumnSchema
2727
public $xDbType;
2828

2929
/**
30-
* TODO
3130
* Used only for MySQL/MariaDB
3231
* @var array|null
3332
* [
34-
* index => int
35-
* after => ?string
36-
* before => ?string
33+
* index => int # position: starts from 1
34+
* after => ?string # after column
35+
* before => ?string # before column
3736
* ]
3837
* If `before` is null then column is last
38+
* If `after` is null then column is first
39+
* If both are null then table has only 1 column
3940
*/
4041
public ?array $fromPosition = null;
4142
public ?array $toPosition = null;
4243

43-
public bool $isPositionReallyChanged = false;
44+
/**
45+
* From `$this->fromPosition` and `$this->toPosition` we can check if the position is changed or not. This is done in `BaseMigrationBuilder::setColumnsPositions()`
46+
*/
47+
public bool $isPositionChanged = false;
4448
}

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function buildSecondary(?ManyToManyRelation $relation = null):MigrationMo
171171
{
172172
$this->migration = Yii::createObject(MigrationModel::class, [$this->model, false, $relation, []]);
173173
$this->newColumns = $relation->columnSchema ?? $this->model->attributesToColumnSchema();
174-
$this->setPositions();
174+
$this->setColumnsPositions();
175175
$wantNames = array_keys($this->newColumns);
176176
$haveNames = $this->tableSchema->columnNames;
177177
$columnsForCreate = array_map(
@@ -538,21 +538,18 @@ public function modifyDesiredInContextOfDesiredFromDb(ColumnSchema $desired, Col
538538
$desired->dbType = $desiredFromDb->dbType;
539539
}
540540

541-
/**
542-
* TODO docs
543-
*/
544-
abstract public function handleColumnsPositionsChanges(array $haveNames, array $wantNames);
545-
546-
547541
/**
548542
* Only for MySQL and MariaDB
549543
* Given a column, compute its previous column name present in OpenAPI schema
550544
* @param ColumnSchema $column
551545
* @param bool $forDrop
546+
* @param bool $forAlter
552547
* @return ?string
553548
* `null` if column is added at last
554549
* 'FIRST' if column is added at first position
555550
* 'AFTER <columnName>' if column is added in between e.g. if 'email' is added after 'username' then 'AFTER username'
556551
*/
557552
abstract public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $forAlter = false): ?string;
553+
554+
abstract public function setColumnsPositions();
558555
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
8585
// $positionCurrent = $this->findPosition($desired, true);
8686
// $positionDesired = $this->findPosition($desired);
8787
// if ($positionCurrent !== $positionDesired) {
88-
if ($desired->isPositionReallyChanged) {
88+
if ($desired->isPositionChanged) {
8989
$changedAttributes[] = 'position';
9090
}
9191

@@ -181,62 +181,11 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
181181
}
182182
}
183183

184-
/**
185-
* TODO
186-
* Check if order/position of column is changed
187-
* @return void
188-
*/
189-
public function checkOrder()
190-
{
191-
}
192-
193184
/**
194185
* {@inheritDoc}
195186
*/
196187
public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $forAlter = false): ?string
197188
{
198-
// if ($column instanceof \cebe\yii2openapi\db\ColumnSchema) {
199-
// if (!$column->isPositionReallyChanged) {
200-
// return null;
201-
// }
202-
// }
203-
// if (!$forDrop) {
204-
// $columnNames = array_keys($this->newColumns);
205-
// $key = array_search($column->name, $columnNames);
206-
// if (is_int($key)) {
207-
// if ($key > 0) {
208-
// $prevColName = $columnNames[$key - 1];
209-
//
210-
// // if the perv col is last then no need for `'AFTER <column-name>'` because last is the default position
211-
// if (array_search($prevColName, $columnNames) === (count($columnNames) - 1)) {
212-
// return null;
213-
// }
214-
//
215-
// return self::POS_AFTER . ' ' . $prevColName;
216-
// } elseif ($key === 0) {
217-
// return self::POS_FIRST;
218-
// }
219-
// }
220-
//
221-
// } else {
222-
// $columnNames = array_keys($this->tableSchema->columns);
223-
// $key = array_search($column->name, $columnNames);
224-
// if (is_int($key)) {
225-
// if ($key > 0) {
226-
// $prevColName = $columnNames[$key - 1];
227-
//
228-
// if (array_search($prevColName, $columnNames) === count($columnNames) - 1) {
229-
// return null;
230-
// }
231-
//
232-
// return self::POS_AFTER . ' ' . $prevColName;
233-
// } elseif ($key === 0) {
234-
// return self::POS_FIRST;
235-
// }
236-
// }
237-
// }
238-
// return null;
239-
240189
$columnNames = array_keys($forDrop ? $this->tableSchema->columns : $this->newColumns);
241190

242191
$key = array_search($column->name, $columnNames);
@@ -246,10 +195,6 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
246195
return null;
247196
}
248197

249-
// 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
250-
// return null;
251-
// }
252-
253198
if (array_key_exists($prevColName, $forDrop ? $this->tableSchema->columns : $this->newColumns)) {
254199
if ($forDrop && !$forAlter) {
255200
// if the previous column is the last one in the want names then no need for AFTER
@@ -275,33 +220,7 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
275220
return null;
276221
}
277222

278-
279-
// TODO
280-
public function handleColumnsPositionsChanges(array $haveNames, array $wantNames)
281-
{
282-
$indices = [];
283-
if ($haveNames !== $wantNames) {
284-
foreach ($wantNames as $key => $name) {
285-
if ($name !== $haveNames[$key]) {
286-
$indices[] = $key;
287-
}
288-
}
289-
}
290-
for ($i = 0; $i < count($indices) / 2; $i++) {
291-
$this->migration->addUpCode($this->recordBuilder->alterColumn(
292-
$this->model->getTableAlias(),
293-
$this->newColumns[$wantNames[$indices[$i]]],
294-
$this->findPosition($this->newColumns[$wantNames[$indices[$i]]])
295-
))->addDownCode($this->recordBuilder->alterColumn(
296-
$this->model->getTableAlias(),
297-
$this->tableSchema->columns[$wantNames[$indices[$i]]],
298-
$this->findPosition($this->tableSchema->columns[$wantNames[$indices[$i]]], true)
299-
));
300-
}
301-
// $this->migration->addUpCode($this->recordBuilder->dropTable($this->model->getTableAlias()));
302-
}
303-
304-
public function setPositions()
223+
public function setColumnsPositions()
305224
{
306225
$i = 0;
307226
$haveColumns = $this->tableSchema->columns;
@@ -362,7 +281,7 @@ public function setPositions()
362281
continue;
363282
}
364283

365-
$column->isPositionReallyChanged = true;
284+
$column->isPositionChanged = true;
366285
$takenIndices[] = [$column->fromPosition['index'], $column->toPosition['index']];
367286
}
368287
}

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,6 @@ public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSch
248248
}
249249
}
250250

251-
/**
252-
* {@inheritDoc}
253-
*/
254-
public function handleColumnsPositionsChanges(array $haveNames, array $wantNames)
255-
{
256-
}
257-
258-
259251
/**
260252
* {@inheritDoc}
261253
*/
@@ -264,7 +256,7 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
264256
return null;
265257
}
266258

267-
public function setPositions()
259+
public function setColumnsPositions()
268260
{
269261
}
270262
}

0 commit comments

Comments
 (0)