Skip to content

Commit

Permalink
Merge pull request #1899 from MasterOdin/unsigned_pk_mysql
Browse files Browse the repository at this point in the history
make default PKs for mysql unsigned by default
  • Loading branch information
dereuromark authored Oct 23, 2020
2 parents 8f8c622 + 1e7eee9 commit 616d104
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions docs/en/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ comment set a text comment on the table
row_format set the table row format
engine define table engine *(defaults to ``InnoDB``)*
collation define table collation *(defaults to ``utf8mb4_unicode_ci``)*
signed whether the primary key is ``signed`` *(defaults to ``true``)*
signed whether the primary key is ``signed`` *(defaults to ``false``)*
========== ===========

By default the primary key is ``signed``.
To simply set it to unsigned just pass ``signed`` option with a ``false`` value:
By default, the primary key is ``unsigned``.
To simply set it to be signed just pass ``signed`` option with a ``true`` value:

.. code-block:: php
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
$column->setName($options['id'])
->setType('integer')
->setOptions([
'signed' => $options['signed'] ?? true,
'signed' => $options['signed'] ?? false,
'identity' => true,
]);

Expand Down
34 changes: 17 additions & 17 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function testCreateTableWithForeignKeys()

$table = new \Phinx\Db\Table('ntable', [], $this->adapter);
$table->addColumn('realname', 'string')
->addColumn('tag_id', 'integer')
->addColumn('tag_id', 'integer', ['signed' => false])
->addForeignKey('tag_id', 'ntable_tag', 'id', ['delete' => 'NO_ACTION', 'update' => 'NO_ACTION'])
->save();

Expand Down Expand Up @@ -1412,21 +1412,21 @@ public function testAddForeignKey()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id']));
}

public function testAddForeignKeyForTableWithUnsignedPK()
public function testAddForeignKeyForTableWithSignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => false], $this->adapter);
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => true], $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addColumn('ref_table_id', 'integer')
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

Expand All @@ -1440,22 +1440,22 @@ public function testDropForeignKey()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

$table->dropForeignKey(['ref_table_id'])->save();
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), ['ref_table_id']));
}

public function testDropForeignKeyForTableWithUnsignedPK()
public function testDropForeignKeyForTableWithSignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => false], $this->adapter);
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => true], $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addColumn('ref_table_id', 'integer')
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

Expand All @@ -1470,7 +1470,7 @@ public function testDropForeignKeyAsString()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

Expand All @@ -1485,7 +1485,7 @@ public function testHasForeignKeyAsString()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

Expand All @@ -1500,22 +1500,22 @@ public function testHasForeignKeyWithConstraint()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKeyWithName('my_constraint', ['ref_table_id'], 'ref_table', ['id'])
->save();

$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id'], 'my_constraint'));
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), ['ref_table_id'], 'my_constraint2'));
}

public function testHasForeignKeyWithConstraintForTableWithUnsignedPK()
public function testHasForeignKeyWithConstraintForTableWithSignedPK()
{
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => false], $this->adapter);
$refTable = new \Phinx\Db\Table('ref_table', ['signed' => true], $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addColumn('ref_table_id', 'integer')
->addForeignKeyWithName('my_constraint', ['ref_table_id'], 'ref_table', ['id'])
->save();

Expand All @@ -1530,7 +1530,7 @@ public function testsHasForeignKeyWithSchemaDotTableName()

$table = new \Phinx\Db\Table('table', [], $this->adapter);
$table
->addColumn('ref_table_id', 'integer')
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey(['ref_table_id'], 'ref_table', ['id'])
->save();

Expand Down Expand Up @@ -1733,7 +1733,7 @@ public function testDumpCreateTable()
->save();

$expectedOutput = <<<'OUTPUT'
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 utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE `table1` (`id` INT(11) unsigned 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 utf8mb4 COLLATE utf8mb4_unicode_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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
->addColumn('id', 'integer', [
'null' => false,
'limit' => 20,
'identity' => 'enable',
'identity' => true,
])
->create();

Expand All @@ -34,7 +34,7 @@ public function up()
->addColumn('id', 'integer', [
'null' => false,
'limit' => 20,
'identity' => 'enable',
'identity' => true,
])
->create();

Expand All @@ -50,7 +50,7 @@ public function up()
->addColumn('id', 'integer', [
'null' => false,
'limit' => 20,
'identity' => 'enable',
'identity' => true,
])
->addColumn('table2_id', 'integer', [
'null' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function change()
{
$this->table('my_table')
->addColumn('name', 'string')
->addColumn('entity_id', 'integer')
->addColumn('entity_id', 'integer', ['signed' => false])
->create();

$this->table('my_other_table')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function change()
->create();

$this->table('orders')
->addColumn('customer_id', 'integer')
->addColumn('customer_id', 'integer', ['signed' => false])
->addForeignKey('customer_id', 'customers', 'id')
->update();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function change()
{
// user logins table
$table = $this->table('user_logins');
$table->addColumn('user_id', Column::INTEGER)
$table->addColumn('user_id', Column::INTEGER, ['signed' => false])
->addColumn('created', Column::DATETIME)
->create();

// add a foreign key back to the users table
$table->addForeignKey('user_id', 'users', ['id'])
$table->addForeignKey('user_id', 'users')
->update();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function change()
->addColumn('user_id', Column::INTEGER, [
'null' => true,
'limit' => 20,
'signed' => false,
])
->addIndex(['user_id'], [
'name' => 'statuses_users_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function change()
{
// user logins table
$table = $this->table('user_logins_baz');
$table->addColumn('user_id', Column::INTEGER)
$table->addColumn('user_id', Column::INTEGER, ['signed' => false])
->addColumn('created', Column::DATETIME)
->create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function change()
{
// user logins table
$table = $this->table('user_logins_foo_bar');
$table->addColumn('user_id', 'integer')
$table->addColumn('user_id', 'integer', ['signed' => false])
->addColumn('created', 'datetime')
->create();

Expand Down

0 comments on commit 616d104

Please sign in to comment.