Skip to content

Commit 7335708

Browse files
authored
Merge pull request #1641 from JRDuncan/master
Allow creation of MSSQL identity columns with user specified seed and increment
2 parents ae0d5ce + f985782 commit 7335708

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Phinx/Db/Adapter/SqlServerAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,9 @@ protected function getColumnSqlDefinition(Column $column, $create = true)
11861186
}
11871187

11881188
if ($column->isIdentity()) {
1189-
$buffer[] = 'IDENTITY(1, 1)';
1189+
$seed = $column->getSeed() ?: 1;
1190+
$increment = $column->getIncrement() ?: 1;
1191+
$buffer[] = sprintf('IDENTITY(%d,%d)', $seed, $increment);
11901192
}
11911193

11921194
return implode(' ', $buffer);

src/Phinx/Db/Table/Column.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ class Column
6666
*/
6767
protected $identity = false;
6868

69+
/**
70+
* @var integer
71+
*/
72+
protected $seed;
73+
74+
/**
75+
* @var integer
76+
*/
77+
protected $increment;
78+
6979
/**
7080
* @var integer
7181
*/
@@ -349,6 +359,52 @@ public function getPrecision()
349359
return $this->limit;
350360
}
351361

362+
/**
363+
* Gets the column identity seed.
364+
*
365+
* @return int
366+
*/
367+
public function getSeed()
368+
{
369+
return $this->seed;
370+
}
371+
372+
/**
373+
* Gets the column identity increment.
374+
*
375+
* @return int
376+
*/
377+
public function getIncrement()
378+
{
379+
return $this->increment;
380+
}
381+
382+
/**
383+
* Sets the column identity seed.
384+
*
385+
* @param int $seed Number seed
386+
* @return \Phinx\Db\Table\Column
387+
*/
388+
public function setSeed($seed)
389+
{
390+
$this->seed = $seed;
391+
392+
return $this;
393+
}
394+
395+
/**
396+
* Sets the column identity increment.
397+
*
398+
* @param int $increment Number increment
399+
* @return \Phinx\Db\Table\Column
400+
*/
401+
public function setIncrement($increment)
402+
{
403+
$this->increment = $increment;
404+
405+
return $this;
406+
}
407+
352408
/**
353409
* Sets the number scale for decimal or float column.
354410
*
@@ -627,6 +683,8 @@ protected function getValidOptions()
627683
'values',
628684
'collation',
629685
'encoding',
686+
'seed',
687+
'increment',
630688
];
631689
}
632690

tests/Phinx/Db/Adapter/SqlServerAdapterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,22 @@ public function testCreateTableCustomIdColumn()
144144
$this->assertTrue($this->adapter->hasColumn('ntable', 'email'));
145145
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));
146146
}
147+
public function testCreateTableIdentityColumn()
148+
{
149+
$table = new \Phinx\Db\Table('ntable', ['id' => false, 'primary_key' => 'id'], $this->adapter);
150+
$table->addColumn('id', 'integer', ['identity' => true, 'seed' => 1, 'increment' => 10 ])
151+
->save();
152+
$this->assertTrue($this->adapter->hasTable('ntable'));
153+
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
147154

155+
$rows = $this->adapter->fetchAll("SELECT CAST(seed_value AS INT) seed_value, CAST(increment_value AS INT) increment_value
156+
FROM sys.columns c JOIN sys.tables t ON c.object_id=t.object_id
157+
JOIN sys.identity_columns ic ON c.object_id=ic.object_id AND c.column_id=ic.column_id
158+
WHERE t.name='ntable'");
159+
$identity = $rows[0];
160+
$this->assertEquals($identity['seed_value'], '1');
161+
$this->assertEquals($identity['increment_value'], '10');
162+
}
148163
public function testCreateTableWithNoPrimaryKey()
149164
{
150165
$options = [

0 commit comments

Comments
 (0)