Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset ident only if values were generated #15865

Merged
merged 1 commit into from Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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