Skip to content

Commit

Permalink
Merge pull request #766 from cakephp/bake-migration-diff
Browse files Browse the repository at this point in the history
Update Bake migration diff to generate with builtin base classes
  • Loading branch information
markstory authored Nov 7, 2024
2 parents 6768a06 + a1dbe87 commit 4a6b14e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/Command/BakeMigrationDiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Schema\CollectionInterface;
use Cake\Database\Schema\TableSchema;
Expand Down Expand Up @@ -199,6 +200,7 @@ public function templateData(Arguments $arguments): array
'data' => $this->templateData,
'dumpSchema' => $this->dumpSchema,
'currentSchema' => $this->currentSchema,
'backend' => Configure::read('Migrations.backend', 'builtin'),
];
}

Expand Down Expand Up @@ -491,6 +493,7 @@ protected function bakeSnapshot(string $name, Arguments $args, ConsoleIo $io): ?

$newArgs = array_merge($newArgs, $this->parseOptions($args));

// TODO(mark) This nested command call always uses phinx backend.
$exitCode = $this->executeCommand(BakeMigrationSnapshotCommand::class, $newArgs, $io);

if ($exitCode === 1) {
Expand Down Expand Up @@ -519,6 +522,7 @@ protected function getDumpSchema(Arguments $args): array
$inputArgs['--plugin'] = $args->getOption('plugin');
}

// TODO(mark) This has to change for the built-in backend
$className = Dump::class;
$definition = (new $className())->getDefinition();

Expand Down
10 changes: 10 additions & 0 deletions templates/bake/config/diff.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
<?php
declare(strict_types=1);
{% if backend == "builtin" %}
use Migrations\BaseMigration;
class {{ name }} extends BaseMigration
{% else %}
use Migrations\AbstractMigration;
class {{ name }} extends AbstractMigration
{% endif %}
{
{% if not autoId %}
public bool $autoId = false;
Expand All @@ -32,7 +38,11 @@ class {{ name }} extends AbstractMigration
* Up Method.
*
* More information on this method is available here:
{% if backend == "builtin" %}
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
{% else %}
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
{% endif %}
* @return void
*/
public function up(): void
Expand Down
13 changes: 12 additions & 1 deletion tests/TestCase/Command/BakeMigrationDiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
namespace Migrations\Test\TestCase\Command;

use Cake\Cache\Cache;
use Cake\Console\BaseCommand;
use Cake\Core\Configure;
use Cake\Core\Plugin;
Expand Down Expand Up @@ -55,6 +56,15 @@ public function tearDown(): void
unlink($file);
}
}
if (env('DB_URL_COMPARE')) {
// Clean up the comparison database each time. Table order is important.
$connection = ConnectionManager::get('test_comparisons');
$tables = ['articles', 'categories', 'comments', 'users', 'phinxlog'];
foreach ($tables as $table) {
$connection->execute("DROP TABLE IF EXISTS $table");
}
Cache::clear('_cake_model_');
}
}

/**
Expand Down Expand Up @@ -206,7 +216,8 @@ protected function runDiffBakingTest(string $scenario): void
$destinationDumpPath,
];

$this->getMigrations("MigrationsDiff$scenario")->migrate();
$migrations = $this->getMigrations("MigrationsDiff$scenario");
$migrations->migrate();

unlink($destination);
copy($diffDumpPath, $destinationDumpPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffAddRemoveMysql extends AbstractMigration
class TheDiffAddRemoveMysql extends BaseMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/comparisons/Diff/default/the_diff_default_mysql.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffDefaultMysql extends AbstractMigration
class TheDiffDefaultMysql extends BaseMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/comparisons/Diff/simple/the_diff_simple_mysql.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffSimpleMysql extends AbstractMigration
class TheDiffSimpleMysql extends BaseMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffWithAutoIdCompatibleSignedPrimaryKeysMysql extends AbstractMigration
class TheDiffWithAutoIdCompatibleSignedPrimaryKeysMysql extends BaseMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffWithAutoIdIncompatibleSignedPrimaryKeysMysql extends AbstractMigration
class TheDiffWithAutoIdIncompatibleSignedPrimaryKeysMysql extends BaseMigration
{
public bool $autoId = false;

/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TheDiffWithAutoIdIncompatibleUnsignedPrimaryKeysMysql extends AbstractMigration
class TheDiffWithAutoIdIncompatibleUnsignedPrimaryKeysMysql extends BaseMigration
{
public bool $autoId = false;

/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* https://book.cakephp.org/migrations/4/en/migrations.html#the-up-method
* @return void
*/
public function up(): void
Expand Down

0 comments on commit 4a6b14e

Please sign in to comment.