Skip to content

Commit

Permalink
Merge pull request #1872 from MasterOdin/column_null_default
Browse files Browse the repository at this point in the history
Set column null by default unless identity
  • Loading branch information
dereuromark committed Sep 28, 2020
2 parents cbf059d + dd4e49b commit a95dbef
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function createSchemaTable()
];

$table = new Table($this->getSchemaTableName(), $options, $this);
$table->addColumn('version', 'biginteger')
$table->addColumn('version', 'biginteger', ['null' => false])
->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
Expand Down
6 changes: 4 additions & 2 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setSigned(isset($options['signed']) ? $options['signed'] : true)
->setIdentity(true);
->setOptions([
'signed' => $options['signed'] ?? true,
'identity' => true,
]);

array_unshift($columns, $column);
if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function setConnection(PDO $connection)
}
if (!$table->hasColumn('breakpoint')) {
$table
->addColumn('breakpoint', 'boolean', ['default' => false])
->addColumn('breakpoint', 'boolean', ['default' => false, 'null' => false])
->save();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setIdentity(true);
->setOptions(['identity' => true]);

array_unshift($columns, $column);
if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setIdentity(true);
->setOptions(['identity' => true]);

array_unshift($columns, $column);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setIdentity(true);
->setOptions(['identity' => true]);

array_unshift($columns, $column);
if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) {
Expand Down
21 changes: 11 additions & 10 deletions src/Phinx/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,17 @@ public function addTimestamps($createdAt = 'created_at', $updatedAt = 'updated_a
$updatedAt = $updatedAt === null ? 'updated_at' : $updatedAt;

$this->addColumn($createdAt, 'timestamp', [
'default' => 'CURRENT_TIMESTAMP',
'update' => '',
'timezone' => $withTimezone,
])
->addColumn($updatedAt, 'timestamp', [
'null' => true,
'default' => null,
'update' => 'CURRENT_TIMESTAMP',
'timezone' => $withTimezone,
]);
'null' => false,
'default' => 'CURRENT_TIMESTAMP',
'update' => '',
'timezone' => $withTimezone,
])
->addColumn($updatedAt, 'timestamp', [
'null' => true,
'default' => null,
'update' => 'CURRENT_TIMESTAMP',
'timezone' => $withTimezone,
]);

return $this;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Column
/**
* @var bool
*/
protected $null = false;
protected $null = true;

/**
* @var mixed|null
Expand Down Expand Up @@ -758,6 +758,10 @@ public function setOptions($options)
$validOptions = $this->getValidOptions();
$aliasOptions = $this->getAliasedOptions();

if (isset($options['identity']) && $options['identity'] && !isset($options['null'])) {
$options['null'] = false;
}

foreach ($options as $option => $value) {
if (isset($aliasOptions[$option])) {
// proxy alias -> option
Expand Down
22 changes: 11 additions & 11 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public function testCreateTableWithMultiplePrimaryKeys()
'primary_key' => ['user_id', 'tag_id'],
];
$table = new \Phinx\Db\Table('table1', $options, $this->adapter);
$table->addColumn('user_id', 'integer')
->addColumn('tag_id', 'integer')
$table->addColumn('user_id', 'integer', ['null' => false])
->addColumn('tag_id', 'integer', ['null' => false])
->save();
$this->assertTrue($this->adapter->hasIndex('table1', ['user_id', 'tag_id']));
$this->assertTrue($this->adapter->hasIndex('table1', ['USER_ID', 'tag_id']));
Expand All @@ -307,7 +307,7 @@ public function testCreateTableWithPrimaryKeyAsUuid()
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'uuid')->save();
$table->addColumn('id', 'uuid', ['null' => false])->save();
$table->addColumn('user_id', 'integer')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
Expand All @@ -324,7 +324,7 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'binaryuuid')->save();
$table->addColumn('id', 'binaryuuid', ['null' => false])->save();
$table->addColumn('user_id', 'integer')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
Expand Down Expand Up @@ -487,7 +487,7 @@ public function testChangePrimaryKey()
{
$table = new \Phinx\Db\Table('table1', ['id' => false, 'primary_key' => 'column1'], $this->adapter);
$table
->addColumn('column1', 'integer')
->addColumn('column1', 'integer', ['null' => false])
->addColumn('column2', 'integer')
->addColumn('column3', 'integer')
->save();
Expand All @@ -504,7 +504,7 @@ public function testDropPrimaryKey()
{
$table = new \Phinx\Db\Table('table1', ['id' => false, 'primary_key' => 'column1'], $this->adapter);
$table
->addColumn('column1', 'integer')
->addColumn('column1', 'integer', ['null' => false])
->save();

$table
Expand Down Expand Up @@ -1727,13 +1727,13 @@ public function testDumpCreateTable()

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

$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->addColumn('column3', 'string', ['default' => 'test'])
->addColumn('column3', 'string', ['default' => 'test', 'null' => false])
->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`id` INT(11) NOT NULL AUTO_INCREMENT, `column1` VARCHAR(255) NOT NULL, `column2` INT(11) NOT NULL, `column3` VARCHAR(255) NOT NULL DEFAULT 'test', PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `table1` (`id` INT(11) NOT NULL AUTO_INCREMENT, `column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, `column3` VARCHAR(255) NOT NULL DEFAULT 'test', PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
OUTPUT;
$actualOutput = $consoleOutput->fetch();
$this->assertStringContainsString($expectedOutput, $actualOutput, 'Passing the --dry-run option does not dump create table query to the output');
Expand Down Expand Up @@ -1839,7 +1839,7 @@ public function testDumpCreateTableAndThenInsert()

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

$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->save();

Expand All @@ -1850,7 +1850,7 @@ public function testDumpCreateTableAndThenInsert()
])->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`column1` VARCHAR(255) NOT NULL, `column2` INT(11) NOT NULL, PRIMARY KEY (`column1`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `table1` (`column1` VARCHAR(255) NOT NULL, `column2` INT(11) NULL, PRIMARY KEY (`column1`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO `table1` (`column1`, `column2`) VALUES ('id1', 1);
OUTPUT;
$actualOutput = $consoleOutput->fetch();
Expand Down
16 changes: 8 additions & 8 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1867,12 +1867,12 @@ public function testDumpCreateTable()
$table = new \Phinx\Db\Table('table1', [], $this->adapter);

$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->addColumn('column3', 'string', ['default' => 'test'])
->addColumn('column2', 'integer', ['null' => true])
->addColumn('column3', 'string', ['default' => 'test', 'null' => false])
->save();

$expectedOutput = 'CREATE TABLE "public"."table1" ("id" SERIAL NOT NULL, "column1" CHARACTER VARYING (255) ' .
'NOT NULL, "column2" INTEGER NOT NULL, "column3" CHARACTER VARYING (255) NOT NULL DEFAULT \'test\', CONSTRAINT ' .
'NULL, "column2" INTEGER NULL, "column3" CHARACTER VARYING (255) NOT NULL DEFAULT \'test\', CONSTRAINT ' .
'"table1_pkey" PRIMARY KEY ("id"));';
$actualOutput = $consoleOutput->fetch();
$this->assertStringContainsString(
Expand All @@ -1893,12 +1893,12 @@ public function testDumpCreateTableWithSchema()
$table = new \Phinx\Db\Table('schema1.table1', [], $this->adapter);

$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->addColumn('column3', 'string', ['default' => 'test'])
->addColumn('column2', 'integer', ['null' => true])
->addColumn('column3', 'string', ['default' => 'test', 'null' => false])
->save();

$expectedOutput = 'CREATE TABLE "schema1"."table1" ("id" SERIAL NOT NULL, "column1" CHARACTER VARYING (255) ' .
'NOT NULL, "column2" INTEGER NOT NULL, "column3" CHARACTER VARYING (255) NOT NULL DEFAULT \'test\', CONSTRAINT ' .
'NULL, "column2" INTEGER NULL, "column3" CHARACTER VARYING (255) NOT NULL DEFAULT \'test\', CONSTRAINT ' .
'"table1_pkey" PRIMARY KEY ("id"));';
$actualOutput = $consoleOutput->fetch();
$this->assertStringContainsString(
Expand Down Expand Up @@ -2010,7 +2010,7 @@ public function testDumpCreateTableAndThenInsert()
$this->adapter->setOutput($consoleOutput);

$table = new \Phinx\Db\Table('schema1.table1', ['id' => false, 'primary_key' => ['column1']], $this->adapter);
$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->save();

Expand All @@ -2021,7 +2021,7 @@ public function testDumpCreateTableAndThenInsert()
])->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE "schema1"."table1" ("column1" CHARACTER VARYING (255) NOT NULL, "column2" INTEGER NOT NULL, CONSTRAINT "table1_pkey" PRIMARY KEY ("column1"));
CREATE TABLE "schema1"."table1" ("column1" CHARACTER VARYING (255) NOT NULL, "column2" INTEGER NULL, CONSTRAINT "table1_pkey" PRIMARY KEY ("column1"));
INSERT INTO "schema1"."table1" ("column1", "column2") VALUES ('id1', 1);
OUTPUT;
$actualOutput = $consoleOutput->fetch();
Expand Down
12 changes: 6 additions & 6 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,13 +990,13 @@ public function testDumpCreateTable()

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

$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->addColumn('column3', 'string', ['default' => 'test'])
->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `column1` VARCHAR NOT NULL, `column2` INTEGER NOT NULL, `column3` VARCHAR NOT NULL DEFAULT 'test');
CREATE TABLE `table1` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `column1` VARCHAR NOT NULL, `column2` INTEGER NULL, `column3` VARCHAR NULL DEFAULT 'test');
OUTPUT;
$actualOutput = $consoleOutput->fetch();
$this->assertStringContainsString($expectedOutput, $actualOutput, 'Passing the --dry-run option does not dump create table query to the output');
Expand Down Expand Up @@ -1098,7 +1098,7 @@ public function testDumpCreateTableAndThenInsert()

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

$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->save();

Expand All @@ -1111,7 +1111,7 @@ public function testDumpCreateTableAndThenInsert()
])->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE `table1` (`column1` VARCHAR NOT NULL, `column2` INTEGER NOT NULL, PRIMARY KEY (`column1`));
CREATE TABLE `table1` (`column1` VARCHAR NOT NULL, `column2` INTEGER NULL, PRIMARY KEY (`column1`));
INSERT INTO `table1` (`column1`, `column2`) VALUES ('id1', 1);
OUTPUT;
$actualOutput = $consoleOutput->fetch();
Expand Down Expand Up @@ -1172,14 +1172,14 @@ public function testAlterTableColumnAdd()

$table->addColumn('string_col', 'string', ['default' => '']);
$table->addColumn('string_col_2', 'string', ['null' => true]);
$table->addColumn('string_col_3', 'string');
$table->addColumn('string_col_3', 'string', ['null' => false]);
$table->addTimestamps();
$table->save();

$columns = $this->adapter->getColumns('table1');
$expected = [
['name' => 'id', 'type' => 'integer', 'default' => null, 'null' => false],
['name' => 'string_col', 'type' => 'string', 'default' => '', 'null' => false],
['name' => 'string_col', 'type' => 'string', 'default' => '', 'null' => true],
['name' => 'string_col_2', 'type' => 'string', 'default' => null, 'null' => true],
['name' => 'string_col_3', 'type' => 'string', 'default' => null, 'null' => false],
['name' => 'created_at', 'type' => 'timestamp', 'default' => 'CURRENT_TIMESTAMP', 'null' => false],
Expand Down
24 changes: 12 additions & 12 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ public function testCreateTableWithMultiplePrimaryKeys()
'primary_key' => ['user_id', 'tag_id'],
];
$table = new \Phinx\Db\Table('table1', $options, $this->adapter);
$table->addColumn('user_id', 'integer')
->addColumn('tag_id', 'integer')
$table->addColumn('user_id', 'integer', ['null' => false])
->addColumn('tag_id', 'integer', ['null' => false])
->save();
$this->assertTrue($this->adapter->hasIndex('table1', ['user_id', 'tag_id']));
$this->assertTrue($this->adapter->hasIndex('table1', ['tag_id', 'USER_ID']));
Expand All @@ -241,7 +241,7 @@ public function testCreateTableWithPrimaryKeyAsUuid()
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'uuid')->save();
$table->addColumn('id', 'uuid', ['null' => false])->save();
$table->addColumn('user_id', 'integer')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
Expand All @@ -255,7 +255,7 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
'primary_key' => 'id',
];
$table = new \Phinx\Db\Table('ztable', $options, $this->adapter);
$table->addColumn('id', 'binaryuuid')->save();
$table->addColumn('id', 'binaryuuid', ['null' => false])->save();
$table->addColumn('user_id', 'integer')->save();
$this->assertTrue($this->adapter->hasColumn('ztable', 'id'));
$this->assertTrue($this->adapter->hasIndex('ztable', 'id'));
Expand Down Expand Up @@ -301,7 +301,7 @@ public function testAddPrimaryKey()
{
$table = new \Phinx\Db\Table('table1', ['id' => false], $this->adapter);
$table
->addColumn('column1', 'integer')
->addColumn('column1', 'integer', ['null' => false])
->save();

$table
Expand All @@ -315,9 +315,9 @@ public function testChangePrimaryKey()
{
$table = new \Phinx\Db\Table('table1', ['id' => false, 'primary_key' => 'column1'], $this->adapter);
$table
->addColumn('column1', 'integer')
->addColumn('column2', 'integer')
->addColumn('column3', 'integer')
->addColumn('column1', 'integer', ['null' => false])
->addColumn('column2', 'integer', ['null' => false])
->addColumn('column3', 'integer', ['null' => false])
->save();

$table
Expand All @@ -332,7 +332,7 @@ public function testDropPrimaryKey()
{
$table = new \Phinx\Db\Table('table1', ['id' => false, 'primary_key' => 'column1'], $this->adapter);
$table
->addColumn('column1', 'integer')
->addColumn('column1', 'integer', ['null' => false])
->save();

$table
Expand Down Expand Up @@ -489,7 +489,7 @@ public function testChangeColumnType()
public function testChangeColumnNameAndNull()
{
$table = new \Phinx\Db\Table('t', [], $this->adapter);
$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->save();
$newColumn2 = new \Phinx\Db\Table\Column();
$newColumn2->setName('column2')
Expand Down Expand Up @@ -953,7 +953,7 @@ public function testDumpCreateTableAndThenInsert()

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

$table->addColumn('column1', 'string')
$table->addColumn('column1', 'string', ['null' => false])
->addColumn('column2', 'integer')
->save();

Expand All @@ -966,7 +966,7 @@ public function testDumpCreateTableAndThenInsert()
])->save();

$expectedOutput = <<<'OUTPUT'
CREATE TABLE [table1] ([column1] NVARCHAR (255) NOT NULL , [column2] INT NOT NULL , CONSTRAINT PK_table1 PRIMARY KEY ([column1]));
CREATE TABLE [table1] ([column1] NVARCHAR (255) NOT NULL , [column2] INT NULL DEFAULT NULL, CONSTRAINT PK_table1 PRIMARY KEY ([column1]));
INSERT INTO [table1] ([column1], [column2]) VALUES ('id1', 1);
OUTPUT;
$actualOutput = str_replace("\r\n", "\n", $consoleOutput->fetch());
Expand Down
11 changes: 11 additions & 0 deletions tests/Phinx/Db/Table/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ public function testSetOptionThrowsExceptionIfOptionIsNotString()

$column->setOptions(['identity']);
}

public function testSetOptionsIdentity()
{
$column = new Column();
$this->assertTrue($column->isNull());
$this->assertFalse($column->isIdentity());

$column->setOptions(['identity' => true]);
$this->assertFalse($column->isNull());
$this->assertTrue($column->isIdentity());
}
}

0 comments on commit a95dbef

Please sign in to comment.