Skip to content

Commit bd18b3b

Browse files
committed
Fix up decimal migration_diff
1 parent efe451a commit bd18b3b

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

src/Command/BakeMigrationDiffCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ protected function getColumns(): void
311311
unset($changedAttributes['length']);
312312
}
313313

314+
// For decimal columns, ensure precision (scale) is included for proper conversion
315+
if (
316+
isset($column['type']) &&
317+
($column['type'] === 'decimal' || $column['type'] === 'float') &&
318+
!isset($changedAttributes['precision']) &&
319+
isset($column['precision'])
320+
) {
321+
$changedAttributes['precision'] = $column['precision'];
322+
}
323+
314324
$this->templateData[$table]['columns']['changed'][$columnName] = $changedAttributes;
315325
}
316326
}

tests/TestCase/Command/BakeMigrationDiffCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ public function testBakingDiffWithAutoIdIncompatibleUnsignedPrimaryKeys(): void
241241
$this->runDiffBakingTest('WithAutoIdIncompatibleUnsignedPrimaryKeys');
242242
}
243243

244+
/**
245+
* Tests that baking a diff with decimal column changes includes precision
246+
* This is a regression test for the fix that ensures precision is included for decimal/float columns
247+
*
248+
* @return void
249+
*/
250+
public function testBakingDiffDecimalChange()
251+
{
252+
$this->skipIf(!env('DB_URL_COMPARE'));
253+
254+
$this->runDiffBakingTest('DecimalChange');
255+
}
256+
244257
/**
245258
* Tests that baking a diff with --plugin option only includes tables with Table classes
246259
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\BaseMigration;
5+
6+
class TheDiffDecimalChangeMysql extends BaseMigration
7+
{
8+
/**
9+
* Up Method.
10+
*
11+
* More information on this method is available here:
12+
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
13+
*
14+
* @return void
15+
*/
16+
public function up(): void
17+
{
18+
$this->table('products')
19+
->changeColumn('price', 'decimal', [
20+
'default' => null,
21+
'limit' => null,
22+
'null' => false,
23+
'precision' => 10,
24+
'scale' => 2,
25+
])
26+
->update();
27+
}
28+
29+
/**
30+
* Down Method.
31+
*
32+
* More information on this method is available here:
33+
* https://book.cakephp.org/phinx/0/en/migrations.html#the-down-method
34+
*
35+
* @return void
36+
*/
37+
public function down(): void
38+
{
39+
$this->table('products')
40+
->changeColumn('price', 'decimal', [
41+
'default' => null,
42+
'null' => false,
43+
'precision' => 8,
44+
'scale' => 2,
45+
])
46+
->update();
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\BaseMigration;
5+
6+
class TheDiffDecimalChangePgsql extends BaseMigration
7+
{
8+
/**
9+
* Up Method.
10+
*
11+
* More information on this method is available here:
12+
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
13+
*
14+
* @return void
15+
*/
16+
public function up(): void
17+
{
18+
$this->table('products')
19+
->changeColumn('price', 'decimal', [
20+
'default' => null,
21+
'limit' => null,
22+
'null' => false,
23+
'precision' => 10,
24+
'scale' => 2,
25+
])
26+
->update();
27+
}
28+
29+
/**
30+
* Down Method.
31+
*
32+
* More information on this method is available here:
33+
* https://book.cakephp.org/phinx/0/en/migrations.html#the-down-method
34+
*
35+
* @return void
36+
*/
37+
public function down(): void
38+
{
39+
$this->table('products')
40+
->changeColumn('price', 'decimal', [
41+
'default' => null,
42+
'null' => false,
43+
'precision' => 8,
44+
'scale' => 2,
45+
])
46+
->update();
47+
}
48+
}

0 commit comments

Comments
 (0)