Skip to content

Commit b995f9d

Browse files
committed
Fix up decimal migration_diff
1 parent 52b112b commit b995f9d

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed

src/Command/BakeMigrationDiffCommand.php

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

311+
// For decimal columns, ensure precision (scale) is included for proper conversion
312+
if (
313+
isset($column['type']) &&
314+
($column['type'] === 'decimal' || $column['type'] === 'float') &&
315+
!isset($changedAttributes['precision']) &&
316+
isset($column['precision'])
317+
) {
318+
$changedAttributes['precision'] = $column['precision'];
319+
}
320+
311321
$this->templateData[$table]['columns']['changed'][$columnName] = $changedAttributes;
312322
}
313323
}

tests/TestCase/Command/BakeMigrationDiffCommandTest.php

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

243+
/**
244+
* Tests that baking a diff with decimal column changes includes precision
245+
* This is a regression test for the fix that ensures precision is included for decimal/float columns
246+
*
247+
* @return void
248+
*/
249+
public function testBakingDiffDecimalChange(): void
250+
{
251+
$this->skipIf(!env('DB_URL_COMPARE'));
252+
253+
$this->runDiffBakingTest('DecimalChange');
254+
}
255+
243256
/**
244257
* Tests that baking a diff with --plugin option only includes tables with Table classes
245258
*/
Binary file not shown.
Binary file not shown.
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+
}

tests/test_app/App/Command/CustomBakeMigrationDiffCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Cake\Console\ConsoleOptionParser;
99
use Cake\Core\Plugin;
1010
use Migrations\Command\BakeMigrationDiffCommand;
11+
use RuntimeException;
1112
use function Cake\Core\env;
1213

1314
class CustomBakeMigrationDiffCommand extends BakeMigrationDiffCommand
@@ -63,6 +64,11 @@ protected function getDumpSchema(Arguments $args): array
6364
$diffConfigFolder = Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Diff' . DS . $comparison . DS;
6465
$diffDumpPath = $diffConfigFolder . 'schema-dump-test_comparisons_' . env('DB') . '.lock';
6566

66-
return unserialize(trim(file_get_contents($diffDumpPath)));
67+
$contents = file_get_contents($diffDumpPath);
68+
if ($contents === false) {
69+
throw new RuntimeException("Unable to read dump file: {$diffDumpPath}");
70+
}
71+
72+
return unserialize(trim($contents));
6773
}
6874
}

0 commit comments

Comments
 (0)