Skip to content

Commit

Permalink
Merge pull request #767 from cakephp/bake-migration-snap
Browse files Browse the repository at this point in the history
4.5 - Update bake migration snapshot for builtin backend
  • Loading branch information
markstory authored Nov 15, 2024
2 parents 4a6b14e + ffe8a41 commit 7644abd
Show file tree
Hide file tree
Showing 28 changed files with 81 additions and 51 deletions.
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<code><![CDATA[ConfigurationTrait]]></code>
</DeprecatedTrait>
</file>
<file src="src/Command/SeedCommand.php">
<DeprecatedMethod>
<code><![CDATA[getMultipleOption]]></code>
</DeprecatedMethod>
</file>
<file src="src/Config/ConfigInterface.php">
<MissingTemplateParam>
<code><![CDATA[ArrayAccess]]></code>
Expand Down
11 changes: 6 additions & 5 deletions src/Command/BakeMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ public function templateData(Arguments $arguments): array
$pluginPath = $this->plugin . '.';
}

$arguments = $arguments->getArguments();
unset($arguments[0]);
/** @var array<int, string> $args */
$args = $arguments->getArguments();
unset($args[0]);
$columnParser = new ColumnParser();
$fields = $columnParser->parseFields($arguments);
$indexes = $columnParser->parseIndexes($arguments);
$primaryKey = $columnParser->parsePrimaryKey($arguments);
$fields = $columnParser->parseFields($args);
$indexes = $columnParser->parseIndexes($args);
$primaryKey = $columnParser->parsePrimaryKey($args);

$action = $this->detectAction($className);

Expand Down
1 change: 1 addition & 0 deletions src/Command/BakeMigrationSnapshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function templateData(Arguments $arguments): array
'action' => 'create_table',
'name' => $this->_name,
'autoId' => $autoId,
'backend' => Configure::read('Migrations.backend', 'builtin'),
];
}

Expand Down
22 changes: 20 additions & 2 deletions src/Command/SnapshotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\Configure;

/**
* Trait needed for all "snapshot" type of bake operations.
Expand All @@ -40,6 +41,15 @@ protected function createFile(string $path, string $contents, Arguments $args, C
return $createFile;
}

/**
* @internal
* @return bool Whether or not the builtin backend is active.
*/
protected function useBuiltinBackend(): bool
{
return Configure::read('Migrations.backend', 'builtin') === 'builtin';
}

/**
* Will mark a snapshot created, the snapshot being identified by its
* full file path.
Expand All @@ -62,7 +72,11 @@ protected function markSnapshotApplied(string $path, Arguments $args, ConsoleIo
$newArgs = array_merge($newArgs, $this->parseOptions($args));

$io->out('Marking the migration ' . $fileName . ' as migrated...');
$this->executeCommand(MigrationsMarkMigratedCommand::class, $newArgs, $io);
if ($this->useBuiltinBackend()) {
$this->executeCommand(MarkMigratedCommand::class, $newArgs, $io);
} else {
$this->executeCommand(MigrationsMarkMigratedCommand::class, $newArgs, $io);
}
}

/**
Expand All @@ -78,7 +92,11 @@ protected function refreshDump(Arguments $args, ConsoleIo $io): void
$newArgs = $this->parseOptions($args);

$io->out('Creating a dump of the new database state...');
$this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io);
if ($this->useBuiltinBackend()) {
$this->executeCommand(DumpCommand::class, $newArgs, $io);
} else {
$this->executeCommand(MigrationsDumpCommand::class, $newArgs, $io);
}
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@

/**
* This object is based loosely on: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
*
* TODO(mark) Having both Migrations\Db\Table and Migrations\Db\Table\Table seems redundant.
* The table models should be joined together so that we have a simpler API exposed.
*
* @internal
*/
class Table
{
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/**
* @internal
* @TODO rename this to `TableMetadata` having two classes with very similar names is confusing for me.
* @TODO rename this to `TableMetadata` having two classes with very similar names is confusing.
*/
class Table
{
Expand Down
6 changes: 3 additions & 3 deletions src/Util/ColumnParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ColumnParser
/**
* Parses a list of arguments into an array of fields
*
* @param array<string> $arguments A list of arguments being parsed
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string, array>
*/
public function parseFields(array $arguments): array
Expand Down Expand Up @@ -95,7 +95,7 @@ public function parseFields(array $arguments): array
/**
* Parses a list of arguments into an array of indexes
*
* @param array<string> $arguments A list of arguments being parsed
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string, array>
*/
public function parseIndexes(array $arguments): array
Expand Down Expand Up @@ -144,7 +144,7 @@ public function parseIndexes(array $arguments): array
* Parses a list of arguments into an array of fields composing the primary key
* of the table
*
* @param array<string> $arguments A list of arguments being parsed
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string>
*/
public function parsePrimaryKey(array $arguments): array
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function getColumnOption(array $options): array
}

// TODO this can be cleaned up when we stop using phinx data structures for column definitions
if ($columnOptions['precision'] === null) {
if (!isset($columnOptions['precision']) || $columnOptions['precision'] == null) {
unset($columnOptions['precision']);
} else {
// due to Phinx using different naming for the precision and scale to CakePHP
Expand Down
6 changes: 6 additions & 0 deletions templates/bake/config/snapshot.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,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 Down
1 change: 1 addition & 0 deletions tests/TestCase/Command/BakeMigrationDiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function setUp(): void
parent::setUp();

$this->generatedFiles = [];
Configure::write('Migrations.backend', 'builtin');
}

public function tearDown(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function setUp(): void
$this->migrationPath = ROOT . DS . 'config' . DS . 'Migrations' . DS;

$this->generatedFiles = [];
Configure::write('Migrations.backend', 'builtin');
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,10 @@ protected function runMigrateSnapshots(string $basePath, string $filename, array
// change class name to avoid conflict with other classes
// to avoid 'Fatal error: Cannot declare class Test...., because the name is already in use'
$content = file_get_contents($destination . $copiedFileName);
$pattern = ' extends AbstractMigration';
$content = str_replace($pattern, 'NewSuffix' . $pattern, $content);
$patterns = [' extends AbstractMigration', ' extends BaseMigration'];
foreach ($patterns as $pattern) {
$content = str_replace($pattern, 'NewSuffix' . $pattern, $content);
}
file_put_contents($destination . $copiedFileName, $content);

$migrations = new Migrations([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotNotEmptyPgsql extends AbstractMigration
class TestSnapshotNotEmptyPgsql extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotPluginBlogPgsql extends AbstractMigration
class TestSnapshotPluginBlogPgsql extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotNotEmptySqlite extends AbstractMigration
class TestSnapshotNotEmptySqlite extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotPluginBlogSqlite extends AbstractMigration
class TestSnapshotPluginBlogSqlite extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotNotEmptySqlserver extends AbstractMigration
class TestSnapshotNotEmptySqlserver extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotPluginBlogSqlserver extends AbstractMigration
class TestSnapshotPluginBlogSqlserver extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
4 changes: 2 additions & 2 deletions tests/comparisons/Migration/test_snapshot_not_empty.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotNotEmpty extends AbstractMigration
class TestSnapshotNotEmpty extends BaseMigration
{
/**
* Up Method.
Expand Down
4 changes: 2 additions & 2 deletions tests/comparisons/Migration/test_snapshot_plugin_blog.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotPluginBlog extends AbstractMigration
class TestSnapshotPluginBlog extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotWithAutoIdCompatibleSignedPrimaryKeys extends AbstractMigration
class TestSnapshotWithAutoIdCompatibleSignedPrimaryKeys extends BaseMigration
{
/**
* Up Method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Migrations\BaseMigration;

class TestSnapshotWithNonDefaultCollation extends AbstractMigration
class TestSnapshotWithNonDefaultCollation extends BaseMigration
{
/**
* Up Method.
Expand Down

0 comments on commit 7644abd

Please sign in to comment.