Skip to content

Commit

Permalink
Merge pull request #295 from cakephp/decimal-support
Browse files Browse the repository at this point in the history
Improve support for baking diff including the addition of a column with the `decimal` type
  • Loading branch information
lorenzo authored Jan 7, 2017
2 parents 7e252a3 + 739b12c commit 7d3dcad
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ public function column($tableSchema, $column)
];
}

/**
* Compute the final array of options to display in a `addColumn` or `changeColumn` instruction.
* The method also takes care of translating properties names between CakePHP database layer and phinx database
* layer.
*
* @param array $options Array of options to compute the final list from.
* @return array
*/
public function getColumnOption($options)
{
$wantedOptions = array_flip([
Expand Down Expand Up @@ -334,8 +342,15 @@ public function getColumnOption($options)
} else {
// due to Phinx using different naming for the precision and scale to CakePHP
$columnOptions['scale'] = $columnOptions['precision'];
$columnOptions['precision'] = $columnOptions['limit'];
unset($columnOptions['limit']);

if (isset($columnOptions['limit'])) {
$columnOptions['precision'] = $columnOptions['limit'];
unset($columnOptions['limit']);
}
if (isset($columnOptions['length'])) {
$columnOptions['precision'] = $columnOptions['length'];
unset($columnOptions['length']);
}
}

return $columnOptions;
Expand Down
8 changes: 8 additions & 0 deletions tests/comparisons/Diff/the_diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function up()
'length' => 11,
'null' => false,
])
->addColumn('average_note', 'decimal', [
'after' => 'category_id',
'default' => null,
'null' => true,
'precision' => 5,
'scale' => 5,
])
->addIndex(
[
'slug',
Expand Down Expand Up @@ -154,6 +161,7 @@ public function down()
'null' => false,
])
->removeColumn('category_id')
->removeColumn('average_note')
->addIndex(
[
'slug',
Expand Down
8 changes: 8 additions & 0 deletions tests/comparisons/Diff/the_diff_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function up()
'length' => 11,
'null' => false,
])
->addColumn('average_note', 'decimal', [
'after' => 'category_id',
'default' => null,
'null' => true,
'precision' => 5,
'scale' => 5,
])
->addIndex(
[
'slug',
Expand Down Expand Up @@ -154,6 +161,7 @@ public function down()
'null' => false,
])
->removeColumn('category_id')
->removeColumn('average_note')
->addIndex(
[
'slug',
Expand Down
8 changes: 8 additions & 0 deletions tests/comparisons/Diff/the_diff_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function up()
'length' => 10,
'null' => false,
])
->addColumn('average_note', 'decimal', [
'after' => 'category_id',
'default' => null,
'null' => true,
'precision' => 5,
'scale' => 5,
])
->addIndex(
[
'category_id',
Expand Down Expand Up @@ -154,6 +161,7 @@ public function down()
'null' => false,
])
->removeColumn('category_id')
->removeColumn('average_note')
->addIndex(
[
'slug',
Expand Down

0 comments on commit 7d3dcad

Please sign in to comment.