Skip to content

Commit 62dbc6f

Browse files
authored
Merge 9655e57 into 6845480
2 parents 6845480 + 9655e57 commit 62dbc6f

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/DMLQueryBuilder.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use function ltrim;
2424
use function strrpos;
2525
use function is_array;
26-
use function reset;
2726

2827
final class DMLQueryBuilder extends AbstractDMLQueryBuilder
2928
{
@@ -90,9 +89,7 @@ public function resetSequence(string $tableName, int|string $value = null): stri
9089
$tableName = $this->quoter->quoteTableName($tableName);
9190

9291
if ($value === null) {
93-
$pk = $table->getPrimaryKey();
94-
$key = $this->quoter->quoteColumnName(reset($pk));
95-
$value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1";
92+
return "DBCC CHECKIDENT ('$tableName', RESEED, 0) WITH NO_INFOMSGS;DBCC CHECKIDENT ('$tableName', RESEED)";
9693
}
9794

9895
return "DBCC CHECKIDENT ('$tableName', RESEED, $value)";

tests/CommandTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,16 @@ public function testInsertExWithRowVersionNullColumn(): void
343343
$this->assertEquals($insertedString, $result['stringcol']);
344344
$this->assertEquals(1, $result['id']);
345345
}
346+
347+
public function testExecuteResetSequence(): void
348+
{
349+
$db = $this->getConnection(true);
350+
$oldRow = $db->createCommand()->insertEx('item', ['name' => 'insert_value_for_sequence', 'category_id' => 1]);
351+
$db->createCommand()->delete('item', ['id' => $oldRow['id']])->execute();
352+
353+
$db->createCommand()->executeResetSequence('item')->execute();
354+
$newRow = $db->createCommand()->insertEx('item', ['name' => 'insert_value_for_sequence', 'category_id' => 1]);
355+
356+
$this->assertEquals($oldRow['id'], $newRow['id']);
357+
}
346358
}

tests/QueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public function testResetSequence(): void
447447
$qb = $this->getConnection(true)->getQueryBuilder();
448448

449449
$sql = $qb->resetSequence('item');
450-
$this->assertSame("DBCC CHECKIDENT ('[item]', RESEED, (SELECT COALESCE(MAX([id]),0) FROM [item])+1)", $sql);
450+
$this->assertSame("DBCC CHECKIDENT ('[item]', RESEED, 0) WITH NO_INFOMSGS;DBCC CHECKIDENT ('[item]', RESEED)", $sql);
451451

452452
$sql = $qb->resetSequence('item', 4);
453453
$this->assertSame("DBCC CHECKIDENT ('[item]', RESEED, 4)", $sql);

0 commit comments

Comments
 (0)