Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature flags to disable some breaking changes #2159

Merged
merged 8 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,17 @@ Within the bootstrap script, the following variables will be available:
* @var \Symfony\Component\Console\Output\OutputInterface $output The executing command's output object
* @var \Phinx\Console\Command\AbstractCommand $context the executing command object
*/

Feature Flags
-------------

For some breaking changes, Phinx offers a way to opt-out of new behavior. The following flags are available:

* ``unsigned_pks``: Should Phinx create primary keys as unsigned integers? (default: ``true``)
* ``column_null``: Should Phinx create columns as null by default? (default: ``true``)
dereuromark marked this conversation as resolved.
Show resolved Hide resolved

These values can also be set by modifying class fields on the ```Phinx\Config\FeatureFlags``` class, for example:

.. code-block:: php

Phinx\Config\FeatureFlags::$unsignedPks = false;
4 changes: 4 additions & 0 deletions src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function __construct(array $configArray, ?string $configFilePath = null)
{
$this->configFilePath = $configFilePath;
$this->values = $this->replaceTokens($configArray);

if (isset($this->values['feature_flags'])) {
FeatureFlags::setFlagsFromConfig($this->values['feature_flags']);
}
}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/Phinx/Config/FeatureFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Phinx\Config;

/**
* Class to hold features flags to toggle breaking changes in Phinx.
*
* New flags should be added very sparingly.
*/
class FeatureFlags
{
/**
* @var bool Should Phinx create unsigned primary keys by default?
*/
public static $unsignedPks = true;
/**
* @var bool Should Phinx create columns NULL by default?
*/
public static $columnNull = true;

/**
* Set the feature flags from the `feature_flags` section of the overall
* config.
*
* @param array $config The `feature_flags` section of the config
*/
public static function setFlagsFromConfig(array $config): void
{
if (isset($config['unsigned_pks'])) {
self::$unsignedPks = (bool)$config['unsigned_pks'];
MasterOdin marked this conversation as resolved.
Show resolved Hide resolved
}
if (isset($config['column_null'])) {
self::$columnNull = (bool)$config['column_null'];
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
3 changes: 2 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Cake\Database\Driver\Mysql as MysqlDriver;
use InvalidArgumentException;
use PDO;
use Phinx\Config\FeatureFlags;
use Phinx\Db\Table\Column;
use Phinx\Db\Table\ForeignKey;
use Phinx\Db\Table\Index;
Expand Down Expand Up @@ -285,7 +286,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column->setName($options['id'])
->setType('integer')
->setOptions([
'signed' => $options['signed'] ?? false,
'signed' => $options['signed'] ?? !FeatureFlags::$unsignedPks,
'identity' => true,
]);

Expand Down
9 changes: 9 additions & 0 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Phinx\Db\Table;

use Phinx\Config\FeatureFlags;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\PostgresAdapter;
use RuntimeException;
Expand Down Expand Up @@ -158,6 +159,14 @@ class Column
*/
protected $values;

/**
* Column constructor
*/
public function __construct()
{
$this->null = FeatureFlags::$columnNull;
}

/**
* Sets the column name.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Phinx/Config/FeatureFlagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Test\Phinx\Config;

use Phinx\Config\FeatureFlags;
use PHPUnit\Framework\TestCase;

class FeatureFlagsTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testSetFlagsFromConfig(): void
{
$config = [
'unsigned_pks' => false,
'column_null' => false,
];
$this->assertTrue(FeatureFlags::$unsignedPks);
$this->assertTrue(FeatureFlags::$columnNull);
FeatureFlags::setFlagsFromConfig($config);
$this->assertFalse(FeatureFlags::$unsignedPks);
$this->assertFalse(FeatureFlags::$columnNull);
}
}
43 changes: 43 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Test\Phinx\Db\Adapter;

use PDOException;
use Phinx\Config\FeatureFlags;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Util\Literal;
Expand Down Expand Up @@ -156,6 +157,11 @@ public function testCreateTable()
$this->assertTrue($this->adapter->hasColumn('ntable', 'realname'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'email'));
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));

$columns = $this->adapter->getColumns('ntable');
$this->assertCount(3, $columns);
$this->assertSame('id', $columns[0]->getName());
$this->assertFalse($columns[0]->isSigned());
}

public function testCreateTableWithComment()
Expand Down Expand Up @@ -421,6 +427,25 @@ public function testCreateTableWithLatin1Collate()
$this->assertEquals('latin1_general_ci', $row['Collation']);
}

public function testCreateTableWithSignedPK()
{
$table = new \Phinx\Db\Table('ntable', ['signed' => true], $this->adapter);
$table->addColumn('realname', 'string')
->addColumn('email', 'integer')
->save();
$this->assertTrue($this->adapter->hasTable('ntable'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'realname'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'email'));
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));
$column_definitions = $this->adapter->getColumns('ntable');
foreach ($column_definitions as $column_definition) {
if ($column_definition->getName() === 'id') {
$this->assertTrue($column_definition->getSigned());
}
}
}

public function testCreateTableWithUnsignedPK()
{
$table = new \Phinx\Db\Table('ntable', ['signed' => false], $this->adapter);
Expand Down Expand Up @@ -459,6 +484,24 @@ public function testCreateTableWithUnsignedNamedPK()
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));
}

/**
* @runInSeparateProcess
*/
public function testUnsignedPksFeatureFlag()
{
$this->adapter->connect();

FeatureFlags::$unsignedPks = false;

$table = new \Phinx\Db\Table('table1', [], $this->adapter);
$table->create();

$columns = $this->adapter->getColumns('table1');
$this->assertCount(1, $columns);
$this->assertSame('id', $columns[0]->getName());
$this->assertTrue($columns[0]->getSigned());
}

public function testCreateTableWithLimitPK()
{
$table = new \Phinx\Db\Table('ntable', ['id' => 'id', 'limit' => 4], $this->adapter);
Expand Down
14 changes: 14 additions & 0 deletions tests/Phinx/Db/Table/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Phinx\Db\Table;

use Phinx\Config\FeatureFlags;
use Phinx\Db\Table\Column;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand All @@ -28,4 +29,17 @@ public function testSetOptionsIdentity()
$this->assertFalse($column->isNull());
$this->assertTrue($column->isIdentity());
}

/**
* @runInSeparateProcess
*/
public function testColumnNullFeatureFlag()
{
$column = new Column();
$this->assertTrue($column->isNull());

FeatureFlags::$columnNull = false;
$column = new Column();
$this->assertFalse($column->isNull());
}
}