Skip to content

Commit a4dc629

Browse files
author
Bizley
authored
Fixes introduced in v3 (#86)
1 parent ec30d17 commit a4dc629

File tree

6 files changed

+92
-69
lines changed

6 files changed

+92
-69
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
vendor
33
docker
44
runtime
5-
composer.lock
5+
composer.lock
6+
.phpunit.result.cache

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,7 @@
99

1010
Generates migration file based on the existing database table and previous migrations.
1111

12-
## Installation for PHP >= 7.1 and Yii >= 2.0.15.1
13-
14-
Add the package to your composer.json:
15-
16-
{
17-
"require": {
18-
"bizley/migration": "^3.6"
19-
}
20-
}
21-
22-
and run `composer update` or alternatively run `composer require bizley/migration:^3.6`
23-
24-
## Installation for PHP < 7.1
12+
## Installation
2513

2614
Add the package to your composer.json:
2715

@@ -33,6 +21,13 @@ Add the package to your composer.json:
3321

3422
and run `composer update` or alternatively run `composer require bizley/migration:^2.9`
3523

24+
## Other versions
25+
26+
| version constraint | PHP requirements | Yii requirements
27+
|:------------------:|:----------------:|:----------------:
28+
| ^4.0 | >= 7.2 | >= 2.0.19
29+
| ^3.6 | >= 7.1 | >= 2.0.15.1
30+
3631
## Configuration
3732

3833
Add the following in your configuration file (preferably console configuration file):
@@ -167,4 +162,4 @@ in columns definition in form of an instance of `yii\db\ColumnSchemaBuilder` (li
167162
## Tests
168163

169164
Tests for MySQL, PostgreSQL, and SQLite. Database configuration is stored in `tests/config.php` (you can override it by
170-
creating `config.local.php` file there).
165+
creating `config.local.php` file there).

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"name": "bizley/migration",
33
"description": "Migration generator for Yii 2.",
44
"type": "yii2-extension",
5-
"keywords": ["yii2", "migration", "migrate", "generator", "console"],
5+
"keywords": ["yii2", "migration", "migrate", "generator", "console", "updater"],
66
"license": "Apache-2.0",
77
"authors": [
88
{
9-
"name": "Pawel Bizley Brzozowski",
9+
"name": "Paweł Bizley Brzozowski",
1010
"email": "[email protected]"
1111
}
1212
],

src/Updater.php

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ protected function extract($migration)
198198
require_once $file;
199199
}
200200

201-
$subject = new $migration();
202-
$subject->db = $this->db;
201+
$subject = new $migration(['db' => clone $this->db]);
203202
$subject->up();
204203

205204
return $subject->changes;
@@ -387,21 +386,25 @@ protected function compareStructures()
387386

388387
$previousColumn = $name;
389388

390-
foreach ([
391-
'type',
392-
'isNotNull',
393-
'length',
394-
'isUnique',
395-
'isUnsigned',
396-
'default',
397-
'append',
398-
'comment'
399-
] as $property) {
400-
if (!$this->generalSchema
389+
foreach (
390+
[
391+
'type',
392+
'isNotNull',
393+
'length',
394+
'isUnique',
395+
'isUnsigned',
396+
'default',
397+
'append',
398+
'comment'
399+
] as $property
400+
) {
401+
if (
402+
!$this->generalSchema
401403
&& $property === 'append'
402404
&& $column->append === null
403405
&& !$this->table->primaryKey->isComposite()
404-
&& $column->isColumnInPK($this->table->primaryKey)) {
406+
&& $column->isColumnInPK($this->table->primaryKey)
407+
) {
405408
$column->append = $column->prepareSchemaAppend(true, $column->autoIncrement);
406409
}
407410

@@ -491,12 +494,14 @@ protected function compareStructures()
491494
? $this->oldTable->foreignKeys[$name]->columns
492495
: [];
493496

494-
if (count(
495-
array_merge(
496-
array_diff($tableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns)),
497-
array_diff($oldTableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns))
497+
if (
498+
count(
499+
array_merge(
500+
array_diff($tableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns)),
501+
array_diff($oldTableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns))
502+
)
498503
)
499-
)) {
504+
) {
500505
if ($this->showOnly) {
501506
echo " - different foreign key '$name' columns (";
502507
echo 'DB: (' . implode(', ', $tableFKColumns) . ') <> ';
@@ -526,12 +531,14 @@ protected function compareStructures()
526531
? $this->oldTable->foreignKeys[$name]->refColumns
527532
: [];
528533

529-
if (count(
530-
array_merge(
531-
array_diff($tableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns)),
532-
array_diff($oldTableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns))
534+
if (
535+
count(
536+
array_merge(
537+
array_diff($tableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns)),
538+
array_diff($oldTableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns))
539+
)
533540
)
534-
)) {
541+
) {
535542
if ($this->showOnly) {
536543
echo " - different foreign key '$name' referral columns (";
537544
echo 'DB: (' . implode(', ', $tableFKRefColumns) . ') <> ';
@@ -645,12 +652,14 @@ protected function compareStructures()
645652
? $this->oldTable->indexes[$name]->columns
646653
: [];
647654

648-
if (count(
649-
array_merge(
650-
array_diff($tableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns)),
651-
array_diff($oldTableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns))
655+
if (
656+
count(
657+
array_merge(
658+
array_diff($tableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns)),
659+
array_diff($oldTableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns))
660+
)
652661
)
653-
)) {
662+
) {
654663
if ($this->showOnly) {
655664
echo " - different index '$name' columns (";
656665
echo 'DB: (' . implode(', ', $tableIndexColumns) . ') <> ';

src/controllers/MigrationController.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* Generates migration file based on the existing database table and previous migrations.
2727
*
2828
* @author Paweł Bizley Brzozowski
29-
* @version 2.9.4
29+
* @version 2.9.5
3030
* @license Apache 2.0
3131
* https://github.com/bizley/yii2-migration
3232
*/
3333
class MigrationController extends Controller
3434
{
35-
protected $version = '2.9.4';
35+
protected $version = '2.9.5';
3636

3737
/**
3838
* @var string Default command action.
@@ -252,6 +252,7 @@ public function init()
252252
}
253253

254254
protected $workingPath;
255+
protected $workingNamespace;
255256

256257
/**
257258
* This method is invoked right before an action is to be executed (after all possible filters).
@@ -280,8 +281,10 @@ public function beforeAction($action)
280281
$this->workingPath = $this->preparePathDirectory(
281282
'@' . FileHelper::normalizePath($namespace, '/')
282283
);
284+
$this->workingNamespace = $namespace;
283285
}
284286
}
287+
unset($namespace);
285288
} elseif ($this->migrationPath !== null) {
286289
if (!is_array($this->migrationPath)) {
287290
$this->migrationPath = [$this->migrationPath];
@@ -455,8 +458,10 @@ public function actionCreate($table)
455458
$tables = $arrangedTables['order'];
456459
$suppressForeignKeys = $arrangedTables['suppressForeignKeys'];
457460

458-
if (count($suppressForeignKeys)
459-
&& TableStructure::identifySchema(get_class($this->db->schema)) === TableStructure::SCHEMA_SQLITE) {
461+
if (
462+
count($suppressForeignKeys)
463+
&& TableStructure::identifySchema(get_class($this->db->schema)) === TableStructure::SCHEMA_SQLITE
464+
) {
460465
$this->stdout(
461466
"WARNING!\n > Creating provided tables in batch requires manual migration!\n",
462467
Console::FG_RED
@@ -523,7 +528,7 @@ public function actionCreate($table)
523528
$this->createMigrationHistoryTable();
524529
}
525530

526-
$this->addMigrationHistory($className, $this->migrationNamespace);
531+
$this->addMigrationHistory($className, $this->workingNamespace);
527532
}
528533

529534
$this->stdout("\n");
@@ -544,11 +549,19 @@ public function actionCreate($table)
544549
);
545550
$file = $this->workingPath . DIRECTORY_SEPARATOR . $className . '.php';
546551

547-
if ($this->generateFile($file, $this->view->renderFile(Yii::getAlias($this->templateFileForeignKey), [
548-
'fks' => $postponedForeignKeys,
549-
'className' => $className,
550-
'namespace' => $this->migrationNamespace
551-
])) === false) {
552+
if (
553+
$this->generateFile(
554+
$file,
555+
$this->view->renderFile(
556+
Yii::getAlias($this->templateFileForeignKey),
557+
[
558+
'fks' => $postponedForeignKeys,
559+
'className' => $className,
560+
'namespace' => $this->migrationNamespace
561+
]
562+
)
563+
) === false
564+
) {
552565
$this->stdout(
553566
"ERROR!\n > Migration file for foreign keys can not be generated!\n\n",
554567
Console::FG_RED
@@ -561,7 +574,7 @@ public function actionCreate($table)
561574
$this->stdout(" > Saved as '{$file}'\n");
562575

563576
if ($this->fixHistory) {
564-
$this->addMigrationHistory($className, $this->migrationNamespace);
577+
$this->addMigrationHistory($className, $this->workingNamespace);
565578
}
566579
}
567580

@@ -681,7 +694,7 @@ public function actionUpdate($table)
681694
$this->createMigrationHistoryTable();
682695
}
683696

684-
$this->addMigrationHistory($className, $this->migrationNamespace);
697+
$this->addMigrationHistory($className, $this->workingNamespace);
685698
}
686699
}
687700

src/table/TableColumn.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ class TableColumn extends Object
120120
* Sets length of the column.
121121
* @param string|int $value
122122
*/
123-
public function setLength($value) {}
123+
public function setLength($value)
124+
{
125+
}
124126

125127
/**
126128
* Returns length of the column.
@@ -131,7 +133,9 @@ public function getLength()
131133
return null;
132134
}
133135

134-
protected function buildSpecificDefinition($table) {}
136+
protected function buildSpecificDefinition($table)
137+
{
138+
}
135139

136140
protected $definition = [];
137141
protected $isUnsignedPossible = true;
@@ -230,7 +234,8 @@ public function isColumnAppendPK()
230234
}
231235

232236
if ($this->schema === TableStructure::SCHEMA_MSSQL) {
233-
if (stripos($this->append, 'IDENTITY') !== false
237+
if (
238+
stripos($this->append, 'IDENTITY') !== false
234239
&& stripos($this->append, 'PRIMARY KEY') !== false
235240
) {
236241
return true;
@@ -290,34 +295,34 @@ public function escapeQuotes($value)
290295
public function removePKAppend()
291296
{
292297
if (!$this->isColumnAppendPK()) {
293-
return null;
298+
return $this->append;
294299
}
295300

296-
$uppercaseAppend = preg_replace('/\s+/', ' ', mb_strtoupper($this->append, 'UTF-8'));
301+
$append = preg_replace('/\s+/', ' ', $this->append);
297302

298303
switch ($this->schema) {
299304
case TableStructure::SCHEMA_MSSQL:
300-
$formattedAppend = str_replace(['PRIMARY KEY', 'IDENTITY'], '', $uppercaseAppend);
305+
$filteredAppend = str_ireplace(['PRIMARY KEY', 'IDENTITY'], '', $append);
301306
break;
302307

303308
case TableStructure::SCHEMA_OCI:
304309
case TableStructure::SCHEMA_PGSQL:
305-
$formattedAppend = str_replace('PRIMARY KEY', '', $uppercaseAppend);
310+
$filteredAppend = str_ireplace('PRIMARY KEY', '', $append);
306311
break;
307312

308313
case TableStructure::SCHEMA_SQLITE:
309-
$formattedAppend = str_replace(['PRIMARY KEY', 'AUTOINCREMENT'], '', $uppercaseAppend);
314+
$filteredAppend = str_ireplace(['PRIMARY KEY', 'AUTOINCREMENT'], '', $append);
310315
break;
311316

312317
case TableStructure::SCHEMA_CUBRID:
313318
case TableStructure::SCHEMA_MYSQL:
314319
default:
315-
$formattedAppend = str_replace(['PRIMARY KEY', 'AUTO_INCREMENT'], '', $uppercaseAppend);
320+
$filteredAppend = str_ireplace(['PRIMARY KEY', 'AUTO_INCREMENT'], '', $append);
316321
}
317322

318-
$formattedAppend = trim($formattedAppend);
323+
$filteredAppend = trim($filteredAppend);
319324

320-
return !empty($formattedAppend) ? $formattedAppend : null;
325+
return !empty($filteredAppend) ? $filteredAppend : null;
321326
}
322327

323328
/**

0 commit comments

Comments
 (0)