From 8a848bd11fdf8639de137f3c54241092e0920341 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 12 Dec 2023 16:20:20 +0100 Subject: [PATCH 1/3] Add force-autoid config. --- src/Command/BakeMigrationSnapshotCommand.php | 9 +++++++++ src/View/Helper/MigrationHelper.php | 4 ++-- templates/bake/config/diff.twig | 3 +-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Command/BakeMigrationSnapshotCommand.php b/src/Command/BakeMigrationSnapshotCommand.php index 4b0427e7..4fec4628 100644 --- a/src/Command/BakeMigrationSnapshotCommand.php +++ b/src/Command/BakeMigrationSnapshotCommand.php @@ -105,6 +105,10 @@ public function templateData(Arguments $arguments): array if ($arguments->hasOption('disable-autoid')) { $autoId = !$arguments->getOption('disable-autoid'); } + $forceAutoId = false; + if ($arguments->hasOption('force-autoid')) { + $forceAutoId = !$arguments->getOption('force-autoid'); + } return [ 'plugin' => $this->plugin, @@ -115,6 +119,7 @@ public function templateData(Arguments $arguments): array 'action' => 'create_table', 'name' => $this->_name, 'autoId' => $autoId, + 'forceAutoId' => $forceAutoId, ]; } @@ -171,6 +176,10 @@ public function getOptionParser(): ConsoleOptionParser 'boolean' => true, 'default' => false, 'help' => 'Disable phinx behavior of automatically adding an id field.', + ])->addOption('force-autoid', [ + 'boolean' => true, + 'default' => false, + 'help' => 'Force automatically adding an id field. This will overwrite the disable-autoid option and ignore signed integers.', ]) ->addOption('no-lock', [ 'help' => 'If present, no lock file will be generated after baking', diff --git a/src/View/Helper/MigrationHelper.php b/src/View/Helper/MigrationHelper.php index e892cfa9..513b9360 100644 --- a/src/View/Helper/MigrationHelper.php +++ b/src/View/Helper/MigrationHelper.php @@ -308,7 +308,7 @@ public function hasAutoIdIncompatiblePrimaryKey(array $tables): bool return false; } - $useUnsignedPrimaryKes = Configure::read( + $useUnsignedPrimaryKeys = Configure::read( 'Migrations.unsigned_primary_keys', FeatureFlags::$unsignedPrimaryKeys ); @@ -321,7 +321,7 @@ public function hasAutoIdIncompatiblePrimaryKey(array $tables): bool foreach ($schema->getPrimaryKey() as $column) { $data = $schema->getColumn($column); - if (isset($data['unsigned']) && $data['unsigned'] === !$useUnsignedPrimaryKes) { + if (isset($data['unsigned']) && $data['unsigned'] === !$useUnsignedPrimaryKeys) { return true; } } diff --git a/templates/bake/config/diff.twig b/templates/bake/config/diff.twig index 21b56a22..7b95252b 100644 --- a/templates/bake/config/diff.twig +++ b/templates/bake/config/diff.twig @@ -14,9 +14,8 @@ */ #} {% set tables = data['fullTables'] %} -{# unset($data['fullTables']) #} {% set constraints = [] %} -{% set autoId = not Migration.hasAutoIdIncompatiblePrimaryKey(tables['add'] + tables['remove']) %} +{% set autoId = forceAutoId OR not Migration.hasAutoIdIncompatiblePrimaryKey(tables['add'] + tables['remove']) %} Date: Wed, 20 Dec 2023 10:55:36 +0100 Subject: [PATCH 2/3] Add force-autoid config. --- templates/bake/config/diff.twig | 2 +- templates/bake/config/snapshot.twig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/bake/config/diff.twig b/templates/bake/config/diff.twig index 7b95252b..616bf4d1 100644 --- a/templates/bake/config/diff.twig +++ b/templates/bake/config/diff.twig @@ -15,7 +15,7 @@ #} {% set tables = data['fullTables'] %} {% set constraints = [] %} -{% set autoId = forceAutoId OR not Migration.hasAutoIdIncompatiblePrimaryKey(tables['add'] + tables['remove']) %} +{% set autoId = not Migration.hasAutoIdIncompatiblePrimaryKey(tables['add'] + tables['remove']) %} Date: Wed, 20 Dec 2023 10:56:44 +0100 Subject: [PATCH 3/3] Add force-autoid config. --- templates/bake/config/snapshot.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/bake/config/snapshot.twig b/templates/bake/config/snapshot.twig index a0529be2..224623ea 100644 --- a/templates/bake/config/snapshot.twig +++ b/templates/bake/config/snapshot.twig @@ -26,7 +26,7 @@ use Migrations\AbstractMigration; class {{ name }} extends AbstractMigration { -{% if not autoId %} +{% if not autoId and not forceAutoId %} public bool $autoId = false; {% endif %}