Skip to content
Permalink
Browse files

Restart identity columns in SQLServer if they exist.

Since SQLServer won't let us truncate tables with foreign keys we can
use delete from and restart the identity sequences.
  • Loading branch information...
markstory committed Sep 8, 2014
1 parent ef5f78e commit e3fcc629f250cd2d9cdd1a6d6f2d87143bce1752
Showing with 16 additions and 3 deletions.
  1. +14 −2 src/Database/Schema/SqlserverSchema.php
  2. +2 −1 tests/TestCase/Database/Schema/SqlserverSchemaTest.php
@@ -429,9 +429,21 @@ public function createTableSql(Table $table, $columns, $constraints, $indexes) {
*/
public function truncateTableSql(Table $table) {
$name = $this->_driver->quoteIdentifier($table->name());
return [
sprintf('TRUNCATE TABLE %s', $name)
$queries = [
sprintf('DELETE FROM TABLE %s', $name)
];
$pk = $table->primaryKey();
foreach ($pk as $column) {
$column = $table->column($column);
if (!empty($column['autoIncrement'])) {
$queries[] = sprintf(
'DBCC CHECKIDENT(%s, RESEED, 0)',
$this->_driver->quoteIdentifier($column)
);
break;
}
}
return $queries;
}
}
@@ -710,8 +710,9 @@ public function testTruncateSql() {
'columns' => ['id']
]);
$result = $table->truncateSql($connection);
$this->assertCount(1, $result);
$this->assertCount(2, $result);
$this->assertEquals('TRUNCATE TABLE [schema_articles]', $result[0]);
$this->assertEquals('DBCC CHECKIDENT([schema_articles], RESEED, 0)', $result[1]);
}
/**

0 comments on commit e3fcc62

Please sign in to comment.
You can’t perform that action at this time.