diff --git a/src/Shell/Task/MigrationDiffTask.php b/src/Shell/Task/MigrationDiffTask.php index 11928dde..22a4fc1d 100644 --- a/src/Shell/Task/MigrationDiffTask.php +++ b/src/Shell/Task/MigrationDiffTask.php @@ -228,7 +228,12 @@ protected function getColumns() // brand new columns $addedColumns = array_diff($currentColumns, $oldColumns); foreach ($addedColumns as $columnName) { - $this->templateData[$table]['columns']['add'][$columnName] = $currentSchema->column($columnName); + $column = $currentSchema->column($columnName); + $key = array_search($columnName, $currentColumns); + if ($key > 0) { + $column['after'] = $currentColumns[$key - 1]; + } + $this->templateData[$table]['columns']['add'][$columnName] = $column; } // changes in columns meta-data @@ -255,9 +260,13 @@ protected function getColumns() } $removedColumns = array_diff($oldColumns, $currentColumns); if (!empty($removedColumns)) { - foreach ($removedColumns as $column) { - $this->templateData[$table]['columns']['remove'][$column] = - $this->dumpSchema[$table]->column($column); + foreach ($removedColumns as $columnName) { + $column = $this->dumpSchema[$table]->column($columnName); + $key = array_search($columnName, $oldColumns); + if ($key > 0) { + $column['after'] = $oldColumns[$key - 1]; + } + $this->templateData[$table]['columns']['remove'][$columnName] = $column; } } } diff --git a/src/View/Helper/MigrationHelper.php b/src/View/Helper/MigrationHelper.php index d7260ddd..a023301c 100644 --- a/src/View/Helper/MigrationHelper.php +++ b/src/View/Helper/MigrationHelper.php @@ -301,7 +301,8 @@ public function getColumnOption($options) 'null', 'comment', 'autoIncrement', - 'precision' + 'precision', + 'after' ]); $columnOptions = array_intersect_key($options, $wantedOptions); if (empty($columnOptions['comment'])) { diff --git a/tests/comparisons/Diff/addremove/the_diff_add_remove_mysql.php b/tests/comparisons/Diff/addremove/the_diff_add_remove_mysql.php index 8720ef2f..fe9bbca3 100644 --- a/tests/comparisons/Diff/addremove/the_diff_add_remove_mysql.php +++ b/tests/comparisons/Diff/addremove/the_diff_add_remove_mysql.php @@ -13,6 +13,7 @@ public function up() $this->table('articles') ->addColumn('the_text', 'text', [ + 'after' => 'title', 'default' => null, 'length' => null, 'null' => false, @@ -25,6 +26,7 @@ public function down() $this->table('articles') ->addColumn('excerpt', 'text', [ + 'after' => 'title', 'default' => null, 'length' => null, 'null' => false, diff --git a/tests/comparisons/Diff/addremove/the_diff_add_remove_pgsql.php b/tests/comparisons/Diff/addremove/the_diff_add_remove_pgsql.php index 1c870ce9..15515549 100644 --- a/tests/comparisons/Diff/addremove/the_diff_add_remove_pgsql.php +++ b/tests/comparisons/Diff/addremove/the_diff_add_remove_pgsql.php @@ -13,6 +13,7 @@ public function up() $this->table('articles') ->addColumn('the_text', 'text', [ + 'after' => 'title', 'default' => null, 'length' => null, 'null' => false, @@ -25,6 +26,7 @@ public function down() $this->table('articles') ->addColumn('excerpt', 'text', [ + 'after' => 'title', 'default' => null, 'length' => null, 'null' => false, diff --git a/tests/comparisons/Diff/simple/the_diff_simple_mysql.php b/tests/comparisons/Diff/simple/the_diff_simple_mysql.php index 93384fa3..b3f2137e 100644 --- a/tests/comparisons/Diff/simple/the_diff_simple_mysql.php +++ b/tests/comparisons/Diff/simple/the_diff_simple_mysql.php @@ -22,6 +22,7 @@ public function up() $this->table('articles') ->addColumn('user_id', 'integer', [ + 'after' => 'name', 'default' => null, 'length' => 11, 'null' => false, diff --git a/tests/comparisons/Diff/simple/the_diff_simple_pgsql.php b/tests/comparisons/Diff/simple/the_diff_simple_pgsql.php index 886d5dc9..8a189e21 100644 --- a/tests/comparisons/Diff/simple/the_diff_simple_pgsql.php +++ b/tests/comparisons/Diff/simple/the_diff_simple_pgsql.php @@ -22,6 +22,7 @@ public function up() $this->table('articles') ->addColumn('user_id', 'integer', [ + 'after' => 'name', 'default' => null, 'length' => 10, 'null' => false, diff --git a/tests/comparisons/Diff/the_diff.php b/tests/comparisons/Diff/the_diff.php index c3f22fc2..6d9eff07 100644 --- a/tests/comparisons/Diff/the_diff.php +++ b/tests/comparisons/Diff/the_diff.php @@ -58,6 +58,7 @@ public function up() $this->table('articles') ->addColumn('category_id', 'integer', [ + 'after' => 'user_id', 'default' => null, 'length' => 11, 'null' => false, @@ -131,6 +132,7 @@ public function down() $this->table('articles') ->addColumn('content', 'text', [ + 'after' => 'rating', 'default' => null, 'length' => null, 'null' => false, diff --git a/tests/comparisons/Diff/the_diff_mysql.php b/tests/comparisons/Diff/the_diff_mysql.php index 82807a9a..09ca6cc0 100644 --- a/tests/comparisons/Diff/the_diff_mysql.php +++ b/tests/comparisons/Diff/the_diff_mysql.php @@ -58,6 +58,7 @@ public function up() $this->table('articles') ->addColumn('category_id', 'integer', [ + 'after' => 'user_id', 'default' => null, 'length' => 11, 'null' => false, @@ -131,6 +132,7 @@ public function down() $this->table('articles') ->addColumn('content', 'text', [ + 'after' => 'rating', 'default' => null, 'length' => null, 'null' => false, diff --git a/tests/comparisons/Diff/the_diff_pgsql.php b/tests/comparisons/Diff/the_diff_pgsql.php index 8c38bd7a..c4deecee 100644 --- a/tests/comparisons/Diff/the_diff_pgsql.php +++ b/tests/comparisons/Diff/the_diff_pgsql.php @@ -58,6 +58,7 @@ public function up() $this->table('articles') ->addColumn('category_id', 'integer', [ + 'after' => 'user_id', 'default' => null, 'length' => 10, 'null' => false, @@ -131,6 +132,7 @@ public function down() $this->table('articles') ->addColumn('content', 'text', [ + 'after' => 'rating', 'default' => null, 'length' => null, 'null' => false,