Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,18 @@ 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_primary_keys``: Should Phinx create primary keys as unsigned integers? (default: ``true``)
* ``column_null_default``: Should Phinx create columns as null by default? (default: ``true``)

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

.. code-block:: php

Phinx\Config\FeatureFlags::$unsignedPrimaryKeys = 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 $unsignedPrimaryKeys = true;
/**
* @var bool Should Phinx create columns NULL by default?
*/
public static $columnNullDefault = 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_primary_keys'])) {
self::$unsignedPrimaryKeys = (bool)$config['unsigned_primary_keys'];
}
if (isset($config['column_null_default'])) {
self::$columnNullDefault = (bool)$config['column_null_default'];
}
}
}
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::$unsignedPrimaryKeys,
'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::$columnNullDefault;
}

/**
* 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_primary_keys' => false,
'column_null_default' => false,
];
$this->assertTrue(FeatureFlags::$unsignedPrimaryKeys);
$this->assertTrue(FeatureFlags::$columnNullDefault);
FeatureFlags::setFlagsFromConfig($config);
$this->assertFalse(FeatureFlags::$unsignedPrimaryKeys);
$this->assertFalse(FeatureFlags::$columnNullDefault);
}
}
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::$unsignedPrimaryKeys = 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::$columnNullDefault = false;
$column = new Column();
$this->assertFalse($column->isNull());
}
}