Skip to content

Commit

Permalink
Merge pull request #15865 from cakephp/backport-15859
Browse files Browse the repository at this point in the history
Reset ident only if values were generated
  • Loading branch information
othercorey committed Sep 9, 2021
2 parents e071530 + 67fa534 commit 0c79d3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Database/Schema/SqlserverSchemaDialect.php
Expand Up @@ -649,7 +649,9 @@ public function truncateTableSql(TableSchema $schema): array
$column = $schema->getColumn($pk[0]);
if (in_array($column['type'], ['integer', 'biginteger'])) {
$queries[] = sprintf(
"DBCC CHECKIDENT('%s', RESEED, 0)",
"IF EXISTS (SELECT * FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = '%s' AND " .
"last_value IS NOT NULL) DBCC CHECKIDENT('%s', RESEED, 0)",
$schema->name(),
$schema->name()
);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -1125,7 +1125,11 @@ public function testTruncateSql()
$result = $table->truncateSql($connection);
$this->assertCount(2, $result);
$this->assertSame('DELETE FROM [schema_articles]', $result[0]);
$this->assertSame("DBCC CHECKIDENT('schema_articles', RESEED, 0)", $result[1]);
$this->assertSame(
"IF EXISTS (SELECT * FROM sys.identity_columns WHERE OBJECT_NAME(OBJECT_ID) = 'schema_articles' AND last_value IS NOT NULL) " .
"DBCC CHECKIDENT('schema_articles', RESEED, 0)",
$result[1]
);
}

/**
Expand Down

0 comments on commit 0c79d3f

Please sign in to comment.