Skip to content

Commit

Permalink
Merge pull request #1957 from MasterOdin/mpeveler/feature-limit-pk
Browse files Browse the repository at this point in the history
 Add limit option for primary key migrations
  • Loading branch information
dereuromark authored Feb 23, 2021
2 parents 14de58e + 3f33a53 commit 3519c5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/en/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ row_format set the table row format
engine define table engine *(defaults to ``InnoDB``)*
collation define table collation *(defaults to ``utf8_general_ci``)*
signed whether the primary key is ``signed`` *(defaults to ``true``)*
limit set the maximum length for the primary key
========== ===========

By default the primary key is ``signed``.
Expand Down Expand Up @@ -1273,7 +1274,7 @@ By default Phinx instructs the database adapter to create a normal index. We
can pass an additional parameter ``unique`` to the ``addIndex()`` method to
specify a unique index. We can also explicitly specify a name for the index
using the ``name`` parameter, the index columns sort order can also be specified using
the ``order`` parameter. The order parameter takes an array of column names and sort order key/value pairs.
the ``order`` parameter. The order parameter takes an array of column names and sort order key/value pairs.

.. code-block:: php
Expand Down Expand Up @@ -1352,7 +1353,7 @@ The single column index can define its index length with or without defining col
}
}
The SQL Server and PostgreSQL adapters also supports ``include`` (non-key) columns on indexes.
The SQL Server and PostgreSQL adapters also supports ``include`` (non-key) columns on indexes.

.. code-block:: php
Expand Down
4 changes: 4 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public function createTable(Table $table, array $columns = [], array $indexes =
->setSigned(isset($options['signed']) ? $options['signed'] : true)
->setIdentity(true);

if (isset($options['limit'])) {
$column->setLimit($options['limit']);
}

array_unshift($columns, $column);
if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) {
throw new InvalidArgumentException('You cannot enable an auto incrementing ID field and a primary key');
Expand Down
10 changes: 10 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ public function testCreateTableWithUnsignedNamedPK()
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));
}

public function testCreateTableWithLimitPK()
{
$table = new \Phinx\Db\Table('ntable', ['id' => 'id', 'limit' => 4], $this->adapter);
$table->save();
$this->assertTrue($this->adapter->hasTable('ntable'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
$column_definitions = $this->adapter->getColumns('ntable');
$this->assertSame($this->usingMysql8() ? null : 4, $column_definitions[0]->getLimit());
}

public function testCreateTableWithSchema()
{
$table = new \Phinx\Db\Table(MYSQL_DB_CONFIG['name'] . '.ntable', [], $this->adapter);
Expand Down

0 comments on commit 3519c5c

Please sign in to comment.