Skip to content

Commit

Permalink
Merge pull request #1915 from MasterOdin/0.next-postgres
Browse files Browse the repository at this point in the history
add support for smallserial alias for postgres
  • Loading branch information
dereuromark authored Nov 12, 2020
2 parents 616d104 + d468ce1 commit 1777515
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/en/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ Option Description
values Can be a comma separated list or an array of values
========= ===========

For ``integer`` and ``biginteger`` columns:
For ``smallinteger``, ``integer`` and ``biginteger`` columns:

======== ===========
Option Description
Expand All @@ -847,6 +847,9 @@ identity enable or disable automatic incrementing
signed enable or disable the ``unsigned`` option *(only applies to MySQL)*
======== ===========

For Postgres, when using ``identity``, it will utilize the ``serial`` type appropriate for the integer size, so that
``smallinteger`` will give you ``smallserial``, ``integer`` gives ``serial``, and ``biginteger`` gives ``bigserial``.

For ``timestamp`` columns:

======== ===========
Expand Down
8 changes: 7 additions & 1 deletion src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,13 @@ protected function getColumnSqlDefinition(Column $column)
{
$buffer = [];
if ($column->isIdentity()) {
$buffer[] = $column->getType() === 'biginteger' ? 'BIGSERIAL' : 'SERIAL';
if ($column->getType() === 'smallinteger') {
$buffer[] = 'SMALLSERIAL';
} elseif ($column->getType() === 'biginteger') {
$buffer[] = 'BIGSERIAL';
} else {
$buffer[] = 'SERIAL';
}
} elseif ($column->getType() instanceof Literal) {
$buffer[] = (string)$column->getType();
} else {
Expand Down
24 changes: 24 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2115,4 +2115,28 @@ public function testRenameMixedCaseTableAndColumns()
$this->assertTrue($this->adapter->hasColumn('OrganizationSettings', 'SettingTypeId'));
$this->assertFalse($this->adapter->hasColumn('OrganizationSettings', 'SettingType'));
}

public function serialProvider(): array
{
return [
[\Phinx\Db\Adapter\AdapterInterface::PHINX_TYPE_SMALL_INTEGER],
[\Phinx\Db\Adapter\AdapterInterface::PHINX_TYPE_INTEGER],
[\Phinx\Db\Adapter\AdapterInterface::PHINX_TYPE_BIG_INTEGER],
];
}

/**
* @dataProvider serialProvider
*/
public function testSerialAliases(string $columnType): void
{
$table = new \Phinx\Db\Table('test', ['id' => false], $this->adapter);
$table->addColumn('id', $columnType, ['identity' => true])->create();

$columns = $table->getColumns();
$this->assertCount(1, $columns);
$column = $columns[0];
$this->assertSame($columnType, $column->getType());
$this->assertSame("nextval('test_id_seq'::regclass)", (string)$column->getDefault());
}
}

0 comments on commit 1777515

Please sign in to comment.