Skip to content

Commit 49ab8cd

Browse files
author
Bizley
authored
Merge pull request #62 from bizley/after-column
After and first for MySQL
2 parents 3466471 + 0650242 commit 49ab8cd

File tree

10 files changed

+121
-8
lines changed

10 files changed

+121
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
vendor
33
docker
4+
runtime
45
composer.lock

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ Add the package to your composer.json:
1515

1616
{
1717
"require": {
18-
"bizley/migration": "^3.5"
18+
"bizley/migration": "^3.6"
1919
}
2020
}
2121

22-
and run `composer update` or alternatively run `composer require bizley/migration:^3.5`
22+
and run `composer update` or alternatively run `composer require bizley/migration:^3.6`
2323

2424
## Installation for PHP < 7.1
2525

2626
Add the package to your composer.json:
2727

2828
{
2929
"require": {
30-
"bizley/migration": "^2.8"
30+
"bizley/migration": "^2.9"
3131
}
3232
}
3333

34-
and run `composer update` or alternatively run `composer require bizley/migration:^2.8`
34+
and run `composer update` or alternatively run `composer require bizley/migration:^2.9`
3535

3636
## Configuration
3737

@@ -114,14 +114,19 @@ Starting with yii2-migration v2.0 it is possible to generate updating migration
114114
[1] Remember that with different database types general column schemas may be generated with different length.
115115

116116
> ### MySQL examples:
117-
> Column `varchar(45)`
118-
> generalSchema=0: `$this->string(45)`
117+
> Column `varchar(255)`
118+
> generalSchema=0: `$this->string(255)`
119119
> generalSchema=1: `$this->string()`
120120
121121
> Column `int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY`
122122
> generalSchema=0: `$this->integer(11)->notNull()->append('AUTO_INCREMENT PRIMARY KEY')`
123123
> generalSchema=1: `$this->primaryKey()`
124124
125+
> Since 3.6/2.9 when column size is different from DBMS' default it's kept:
126+
> Column `varchar(45)`
127+
> generalSchema=0: `$this->string(45)`
128+
> generalSchema=1: `$this->string(45)`
129+
125130
[2] Here you can place migrations containing actions that can not be covered by extractor i.e. when there is a migration
126131
setting the RBAC hierarchy with authManager component. Such actions should be kept in separated migration and placed on
127132
this list to prevent them from being run during the extraction process.

src/Updater.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,28 @@ protected function compareStructures(): bool
348348
echo "SHOWING DIFFERENCES:\n";
349349
}
350350

351+
$previousColumn = null;
351352
foreach ($this->table->columns as $name => $column) {
352353
if (!isset($this->oldTable->columns[$name])) {
353354
if ($this->showOnly) {
354355
echo " - missing column '$name'\n";
355356
} else {
357+
if ($previousColumn) {
358+
$column->after = $previousColumn;
359+
} else {
360+
$column->isFirst = true;
361+
}
356362
$this->plan->addColumn[$name] = $column;
357363
}
358364

359365
$different = true;
366+
$previousColumn = $name;
360367

361368
continue;
362369
}
363370

371+
$previousColumn = $name;
372+
364373
foreach ([
365374
'type',
366375
'isNotNull',

src/controllers/MigrationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ public function actionList(): int
413413
if (!$tables) {
414414
$this->stdout(" > Your database does not contain any tables yet.\n");
415415
} else {
416-
$this->stdout(' > Your database contains ' . count($tables) . " tables:\n");
416+
$tablesCount = count($tables);
417+
$this->stdout(" > Your database contains {$tablesCount} table" . ($tablesCount > 1 ? 's' : '') . ":\n");
417418

418419
foreach ($tables as $table) {
419420
$this->stdout(" - $table\n");

src/dummy/Migration.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,17 @@ protected function extractColumn($columnData)
187187
$this->db->schema->typeMap
188188
);
189189

190-
foreach (['length', 'isNotNull', 'isUnique', 'check', 'default', 'append', 'isUnsigned'] as $property) {
190+
foreach ([
191+
'length',
192+
'isNotNull',
193+
'isUnique',
194+
'check',
195+
'default',
196+
'append',
197+
'isUnsigned',
198+
'after',
199+
'isFirst'
200+
] as $property) {
191201
$reflectionProperty = $reflectionClass->getProperty($property);
192202
$reflectionProperty->setAccessible(true);
193203

@@ -231,6 +241,7 @@ public function addChange($table, $method, $data)
231241
'table' => $table,
232242
'method' => $method,
233243
'data' => $data,
244+
'db' => $this->db,
234245
]);
235246
}
236247

src/table/TableChange.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use yii\base\BaseObject;
88
use yii\base\InvalidConfigException;
9+
use yii\db\Connection;
910

1011
/**
1112
* Class TableChange
@@ -36,6 +37,12 @@ class TableChange extends BaseObject
3637
*/
3738
public $schema;
3839

40+
/**
41+
* @var Connection
42+
* @since 3.6.0
43+
*/
44+
public $db;
45+
3946
/**
4047
* Returns change value.
4148
* @return array|string|TableColumn|TablePrimaryKey|TableForeignKey|TableIndex
@@ -52,6 +59,7 @@ public function getValue()
5259
'schema' => $this->schema,
5360
'name' => $column,
5461
'type' => $schema['type'],
62+
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$schema['type']],
5563
'length' => $schema['length'] ?? null,
5664
'isNotNull' => $schema['isNotNull'] ?? null,
5765
'isUnique' => $schema['isUnique'] ?? null,
@@ -61,6 +69,8 @@ public function getValue()
6169
'append' => $schema['append'] ?? null,
6270
'isUnsigned' => $schema['isUnsigned'] ?? null,
6371
'comment' => !empty($schema['comment']) ? $schema['comment'] : null,
72+
'after' => $schema['after'] ?? null,
73+
'isFirst' => $schema['isFirst'] === true,
6474
]);
6575
}
6676
return $columns;
@@ -77,6 +87,7 @@ public function getValue()
7787
'schema' => $this->schema,
7888
'name' => $this->data[0],
7989
'type' => $this->data[1]['type'],
90+
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$this->data[1]['type']],
8091
'length' => $this->data[1]['length'] ?? null,
8192
'isNotNull' => $this->data[1]['isNotNull'] ?? null,
8293
'isUnique' => $this->data[1]['isUnique'] ?? null,
@@ -86,6 +97,8 @@ public function getValue()
8697
'append' => $this->data[1]['append'] ?? null,
8798
'isUnsigned' => $this->data[1]['isUnsigned'] ?? null,
8899
'comment' => !empty($this->data[1]['comment']) ? $this->data[1]['comment'] : null,
100+
'after' => $this->data[1]['after'] ?? null,
101+
'isFirst' => $this->data[1]['isFirst'] === true,
89102
]);
90103

91104
case 'addPrimaryKey':

src/table/TableColumn.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ class TableColumn extends BaseObject
111111
*/
112112
public $schema;
113113

114+
/**
115+
* @var string
116+
* @since 3.6.0
117+
*/
118+
public $after;
119+
120+
/**
121+
* @var bool
122+
* @since 3.6.0
123+
*/
124+
public $isFirst = false;
125+
114126
/**
115127
* Sets length of the column.
116128
* @param string|int $value
@@ -176,6 +188,12 @@ protected function buildGeneralDefinition(TableStructure $table): void
176188
if ($this->comment) {
177189
$this->definition[] = "comment('" . $this->escapeQuotes((string)$this->comment) . "')";
178190
}
191+
192+
if ($this->after) {
193+
$this->definition[] = "after('" . $this->escapeQuotes($this->after) . "')";
194+
} elseif ($this->isFirst) {
195+
$this->definition[] = 'first()';
196+
}
179197
}
180198

181199
/**

tests/cases/UpdaterColumnsTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ public function testAddColumn(): void
156156
$this->assertTrue($updater->isUpdateRequired());
157157
$this->assertArrayHasKey('col_new', $updater->plan->addColumn);
158158
$this->assertEquals(Schema::TYPE_INTEGER, $updater->plan->addColumn['col_new']->type);
159+
$this->assertEquals('col_timestamp', $updater->plan->addColumn['col_new']->after);
160+
$this->assertEquals(false, $updater->plan->addColumn['col_new']->isFirst);
159161
}
160162
}

tests/mysql/UpdaterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,45 @@ public function testAddPrimaryKey(): void
3737
$this->assertEmpty($updater->plan->addPrimaryKey->name);
3838
$this->assertEquals(['col'], $updater->plan->addPrimaryKey->columns);
3939
}
40+
41+
/**
42+
* @runInSeparateProcess
43+
* @preserveGlobalState disabled
44+
* @throws Exception
45+
* @throws ErrorException
46+
* @throws NotSupportedException
47+
*/
48+
public function testAddColumnAfter(): void
49+
{
50+
$this->dbUp('test_columns');
51+
52+
Yii::$app->db->createCommand()->addColumn('test_columns', 'after_date', $this->integer()->after('col_date'))->execute();
53+
54+
$updater = $this->getUpdater('test_columns');
55+
$this->assertTrue($updater->isUpdateRequired());
56+
$this->assertNotEmpty($updater->plan->addColumn);
57+
$this->assertArrayHasKey('after_date', $updater->plan->addColumn);
58+
$this->assertEquals('col_date', $updater->plan->addColumn['after_date']->after);
59+
}
60+
61+
/**
62+
* @runInSeparateProcess
63+
* @preserveGlobalState disabled
64+
* @throws Exception
65+
* @throws ErrorException
66+
* @throws NotSupportedException
67+
*/
68+
public function testAddColumnFirst(): void
69+
{
70+
$this->dbUp('test_columns');
71+
72+
Yii::$app->db->createCommand()->addColumn('test_columns', 'first_col', $this->integer()->first())->execute();
73+
74+
$updater = $this->getUpdater('test_columns');
75+
$this->assertTrue($updater->isUpdateRequired());
76+
$this->assertNotEmpty($updater->plan->addColumn);
77+
$this->assertArrayHasKey('first_col', $updater->plan->addColumn);
78+
$this->assertTrue($updater->plan->addColumn['first_col']->isFirst);
79+
$this->assertNull($updater->plan->addColumn['first_col']->after);
80+
}
4081
}

tests/table/TableColumnTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,16 @@ public function testDefinitionPKAppendSQLITE(): void
9595
$column->renderDefinition($this->getTable())
9696
);
9797
}
98+
99+
public function testDefinitionAfter(): void
100+
{
101+
$column = new TableColumn(['after' => 'columnAfter']);
102+
$this->assertEquals('$this->after(\'columnAfter\')', $column->renderDefinition($this->getTable()));
103+
}
104+
105+
public function testDefinitionFirst(): void
106+
{
107+
$column = new TableColumn(['isFirst' => true]);
108+
$this->assertEquals('$this->first()', $column->renderDefinition($this->getTable()));
109+
}
98110
}

0 commit comments

Comments
 (0)