Skip to content

Commit 1382e08

Browse files
author
Bizley
authored
Merge pull request #63 from bizley/after-column-2x
After column for 2.x
2 parents da08287 + ff82790 commit 1382e08

File tree

10 files changed

+121
-9
lines changed

10 files changed

+121
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.idea
2-
/vendor
2+
vendor
3+
docker
4+
runtime
35
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
@@ -329,19 +329,28 @@ protected function compareStructures()
329329
echo "SHOWING DIFFERENCES:\n";
330330
}
331331

332+
$previousColumn = null;
332333
foreach ($this->table->columns as $name => $column) {
333334
if (!isset($this->oldTable->columns[$name])) {
334335
if ($this->showOnly) {
335336
echo " - missing column '$name'\n";
336337
} else {
338+
if ($previousColumn) {
339+
$column->after = $previousColumn;
340+
} else {
341+
$column->isFirst = true;
342+
}
337343
$this->plan->addColumn[$name] = $column;
338344
}
339345

340346
$different = true;
347+
$previousColumn = $name;
341348

342349
continue;
343350
}
344351

352+
$previousColumn = $name;
353+
345354
foreach ([
346355
'type',
347356
'isNotNull',

src/controllers/MigrationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ public function actionList()
397397
if (!$tables) {
398398
$this->stdout(" > Your database does not contain any tables yet.\n");
399399
} else {
400-
$this->stdout(' > Your database contains ' . count($tables) . " tables:\n");
400+
$tablesCount = count($tables);
401+
$this->stdout(" > Your database contains {$tablesCount} table" . ($tablesCount > 1 ? 's' : '') . ":\n");
401402
foreach ($tables as $table) {
402403
$this->stdout(" - $table\n");
403404
}

src/dummy/Migration.php

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

179-
foreach (['length', 'isNotNull', 'isUnique', 'check', 'default', 'append', 'isUnsigned'] as $property) {
179+
foreach ([
180+
'length',
181+
'isNotNull',
182+
'isUnique',
183+
'check',
184+
'default',
185+
'append',
186+
'isUnsigned',
187+
'after',
188+
'isFirst'
189+
] as $property) {
180190
$reflectionProperty = $reflectionClass->getProperty($property);
181191
$reflectionProperty->setAccessible(true);
182192

@@ -220,6 +230,7 @@ public function addChange($table, $method, $data)
220230
'table' => $table,
221231
'method' => $method,
222232
'data' => $data,
233+
'db' => $this->db,
223234
]);
224235
}
225236

src/table/TableChange.php

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

55
use yii\base\InvalidConfigException;
66
use yii\base\Object;
7+
use yii\db\Connection;
78

89
/**
910
* Class TableChange
@@ -34,6 +35,12 @@ class TableChange extends Object
3435
*/
3536
public $schema;
3637

38+
/**
39+
* @var Connection
40+
* @since 2.9.0
41+
*/
42+
public $db;
43+
3744
/**
3845
* Returns change value.
3946
* @return array|string|TableColumn|TablePrimaryKey|TableForeignKey|TableIndex
@@ -49,6 +56,7 @@ public function getValue()
4956
'schema' => $this->schema,
5057
'name' => $column,
5158
'type' => $schema['type'],
59+
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$schema['type']],
5260
'length' => isset($schema['length']) ? $schema['length'] : null,
5361
'isNotNull' => isset($schema['isNotNull']) ? $schema['isNotNull'] : null,
5462
'isUnique' => isset($schema['isUnique']) ? $schema['isUnique'] : null,
@@ -58,6 +66,8 @@ public function getValue()
5866
'append' => isset($schema['append']) ? $schema['append'] : null,
5967
'isUnsigned' => isset($schema['isUnsigned']) ? $schema['isUnsigned'] : null,
6068
'comment' => !empty($schema['comment']) ? $schema['comment'] : null,
69+
'after' => !empty($schema['after']) ? $schema['after'] : null,
70+
'isFirst' => isset($schema['isFirst']) && $schema['isFirst'] === true,
6171
]);
6272
}
6373
return $columns;
@@ -74,6 +84,7 @@ public function getValue()
7484
'schema' => $this->schema,
7585
'name' => $this->data[0],
7686
'type' => $this->data[1]['type'],
87+
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$this->data[1]['type']],
7788
'length' => isset($this->data[1]['length']) ? $this->data[1]['length'] : null,
7889
'isNotNull' => isset($this->data[1]['isNotNull']) ? $this->data[1]['isNotNull'] : null,
7990
'isUnique' => isset($this->data[1]['isUnique']) ? $this->data[1]['isUnique'] : null,
@@ -83,6 +94,8 @@ public function getValue()
8394
'append' => isset($this->data[1]['append']) ? $this->data[1]['append'] : null,
8495
'isUnsigned' => isset($this->data[1]['isUnsigned']) ? $this->data[1]['isUnsigned'] : null,
8596
'comment' => !empty($this->data[1]['comment']) ? $this->data[1]['comment'] : null,
97+
'after' => !empty($this->data[1]['after']) ? $this->data[1]['after'] : null,
98+
'isFirst' => isset($this->data[1]['isFirst']) && $this->data[1]['isFirst'] === true,
8699
]);
87100

88101
case 'addPrimaryKey':

src/table/TableColumn.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ class TableColumn extends Object
9898
*/
9999
public $schema;
100100

101+
/**
102+
* @var string
103+
* @since 2.9.0
104+
*/
105+
public $after;
106+
/**
107+
* @var bool
108+
* @since 2.9.0
109+
*/
110+
public $isFirst = false;
111+
101112
/**
102113
* Sets length of the column.
103114
* @param string|int $value
@@ -159,6 +170,12 @@ protected function buildGeneralDefinition($table)
159170
if ($this->comment) {
160171
$this->definition[] = "comment('" . $this->escapeQuotes((string) $this->comment) . "')";
161172
}
173+
174+
if ($this->after) {
175+
$this->definition[] = "after('" . $this->escapeQuotes($this->after) . "')";
176+
} elseif ($this->isFirst) {
177+
$this->definition[] = 'first()';
178+
}
162179
}
163180

164181
/**

tests/cases/UpdaterColumnsTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,7 @@ public function testAddColumn()
154154
$this->assertTrue($updater->isUpdateRequired());
155155
$this->assertArrayHasKey('col_new', $updater->plan->addColumn);
156156
$this->assertEquals(Schema::TYPE_INTEGER, $updater->plan->addColumn['col_new']->type);
157+
$this->assertEquals('col_timestamp', $updater->plan->addColumn['col_new']->after);
158+
$this->assertEquals(false, $updater->plan->addColumn['col_new']->isFirst);
157159
}
158160
}

tests/mysql/UpdaterTest.php

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

tests/table/TableColumnTest.php

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

0 commit comments

Comments
 (0)