Skip to content

Commit

Permalink
Convert FeatureFlags to Configure operations
Browse files Browse the repository at this point in the history
Instead of Phinx's feature flags we should use Configure. I've chosen to
read and write directly to Configure to avoid more complexity.

The additional default values in `Migrator` will improve compatibility
with existing usage.
  • Loading branch information
markstory committed Apr 13, 2024
1 parent dee907f commit 5f7fb5d
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Migrations\Db\Table\ForeignKey;
use Migrations\Db\Table\Index;
use Migrations\Db\Table\Table;
use Phinx\Config\FeatureFlags;

/**
* Phinx MySQL Adapter.
Expand Down Expand Up @@ -232,12 +231,13 @@ public function createTable(Table $table, array $columns = [], array $indexes =
}

if (isset($options['id']) && is_string($options['id'])) {
$useUnsigned = (bool)Configure::read('Migrations.unsigned_primary_keys');

Check failure on line 234 in src/Db/Adapter/MysqlAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Call to static method read() on an unknown class Migrations\Db\Adapter\Configure.

Check failure on line 234 in src/Db/Adapter/MysqlAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

UndefinedClass

src/Db/Adapter/MysqlAdapter.php:234:34: UndefinedClass: Class, interface or enum named Migrations\Db\Adapter\Configure does not exist (see https://psalm.dev/019)
// Handle id => "field_name" to support AUTO_INCREMENT
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setOptions([
'signed' => $options['signed'] ?? !FeatureFlags::$unsignedPrimaryKeys,
'signed' => $options['signed'] ?? !$useUnsigned,
'identity' => true,
]);

Expand Down
4 changes: 2 additions & 2 deletions src/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Migrations\Db\Table;

use Cake\Core\Configure;
use Migrations\Db\Literal;
use Phinx\Config\FeatureFlags;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\PostgresAdapter;
use RuntimeException;
Expand Down Expand Up @@ -166,7 +166,7 @@ class Column
*/
public function __construct()
{
$this->null = FeatureFlags::$columnNullDefault;
$this->null = (bool)Configure::read('Migrations.column_null_default');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Migration/BuiltinBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Console\TestSuite\StubConsoleOutput;
use DateTime;
use InvalidArgumentException;
use Migrations\Config\ConfigInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -218,7 +219,9 @@ public function markMigrated(int|string|null $version = null, array $options = [
*/
public function seed(array $options = []): bool
{
$options['source'] ??= ConfigInterface::DEFAULT_SEED_FOLDER;
$seed = $options['seed'] ?? null;

$manager = $this->getManager($options);
$manager->seed($seed);

Expand All @@ -237,7 +240,7 @@ public function getManager(array $options): Manager

$factory = new ManagerFactory([
'plugin' => $options['plugin'] ?? null,
'source' => $options['source'] ?? null,
'source' => $options['source'] ?? ConfigInterface::DEFAULT_MIGRATION_FOLDER,
'connection' => $options['connection'] ?? 'default',
]);
$io = new ConsoleIo(
Expand Down
5 changes: 5 additions & 0 deletions src/Migration/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Migrations\Migration;

use Cake\Console\ConsoleIo;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -120,6 +121,10 @@ public function createConfig(): ConfigInterface
'environment' => $adapterConfig,
'plugin' => $plugin,
'source' => (string)$this->getOption('source'),
'feature_flags' => [
'unsigned_primary_keys' => Configure::read('Migrations.unsigned_primary_keys'),
'column_null_default' => Configure::read('Migrations.column_null_default'),
],
// TODO do we want to support the DI container in migrations?
];

Expand Down
5 changes: 1 addition & 4 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,7 @@ public function hasAutoIdIncompatiblePrimaryKey(array $tables): bool
return false;
}

$useUnsignedPrimaryKes = Configure::read(
'Migrations.unsigned_primary_keys',
FeatureFlags::$unsignedPrimaryKeys
);
$useUnsignedPrimaryKes = (bool)Configure::read('Migrations.unsigned_primary_keys');

foreach ($tables as $table) {
$schema = $table;
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleInput;
use Cake\Console\TestSuite\StubConsoleOutput;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Query;
use Cake\Datasource\ConnectionManager;
Expand All @@ -17,7 +18,6 @@
use Migrations\Db\Table\Column;
use PDO;
use PDOException;
use Phinx\Config\FeatureFlags;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

Expand Down Expand Up @@ -465,7 +465,7 @@ public function testUnsignedPksFeatureFlag()
{
$this->adapter->connect();

FeatureFlags::$unsignedPrimaryKeys = false;
Configure::write('Migrations.unsigned_primary_keys', false);

$table = new Table('table1', [], $this->adapter);
$table->create();
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Db/Table/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Migrations\Test\TestCase\Db\Table;

use Cake\Core\Configure;
use Migrations\Db\Table\Column;
use Phinx\Config\FeatureFlags;
use PHPUnit\Framework\TestCase;
use RuntimeException;

Expand Down Expand Up @@ -39,7 +39,7 @@ public function testColumnNullFeatureFlag()
$column = new Column();
$this->assertTrue($column->isNull());

FeatureFlags::$columnNullDefault = false;
Configure::write('Migrations.column_null_default', false);
$column = new Column();
$this->assertFalse($column->isNull());
}
Expand Down
3 changes: 0 additions & 3 deletions tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Exception;
use InvalidArgumentException;
use Migrations\Migrations;
use Phinx\Config\FeatureFlags;
use Phinx\Db\Adapter\WrapperInterface;
use function Cake\Core\env;

Expand Down Expand Up @@ -118,8 +117,6 @@ public function tearDown(): void
unlink($file);
}
}

FeatureFlags::setFlagsFromConfig(Configure::read('Migrations'));
}

public static function backendProvider(): array
Expand Down
3 changes: 0 additions & 3 deletions tests/TestCase/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Cake\Routing\Router;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase as BaseTestCase;
use Phinx\Config\FeatureFlags;

abstract class TestCase extends BaseTestCase
{
Expand Down Expand Up @@ -63,8 +62,6 @@ public function tearDown(): void
}
$this->generatedFiles = [];
}

FeatureFlags::setFlagsFromConfig(Configure::read('Migrations'));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Cake\Routing\Router;
use Cake\TestSuite\Fixture\SchemaLoader;
use Migrations\MigrationsPlugin;
use Phinx\Config\FeatureFlags;
use SimpleSnapshot\Plugin as SimpleSnapshotPlugin;
use TestBlog\Plugin as TestBlogPlugin;
use function Cake\Core\env;
Expand Down Expand Up @@ -69,8 +68,8 @@
]);

Configure::write('Migrations', [
'unsigned_primary_keys' => FeatureFlags::$unsignedPrimaryKeys,
'column_null_default' => FeatureFlags::$columnNullDefault,
'unsigned_primary_keys' => true,
'column_null_default' => true,
]);

Cache::setConfig([
Expand Down

0 comments on commit 5f7fb5d

Please sign in to comment.