Skip to content

Commit b20b2bb

Browse files
Fix method resetSequence(). (#99)
* Fix method `resetSequence()`.
1 parent f65ffba commit b20b2bb

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/DMLQueryBuilder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,8 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
217217

218218
/**
219219
* @throws InvalidArgumentException
220-
*
221-
* @psalm-suppress RiskyCast
222220
*/
223-
public function resetSequence(string $tableName, array|int|string|null $value = null): string
221+
public function resetSequence(string $tableName, int|string $value = null): string
224222
{
225223
$tableSchema = $this->schema->getTableSchema($tableName);
226224

@@ -229,13 +227,12 @@ public function resetSequence(string $tableName, array|int|string|null $value =
229227
}
230228

231229
$sequenceName = $tableSchema->getSequenceName();
230+
232231
if ($sequenceName === null) {
233232
throw new InvalidArgumentException("There is no sequence associated with table: $tableName");
234233
}
235234

236-
if ($value !== null) {
237-
$value = (int) $value;
238-
} elseif (count($tableSchema->getPrimaryKey()) > 1) {
235+
if ($value === null && count($tableSchema->getPrimaryKey()) > 1) {
239236
throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $tableName");
240237
}
241238

tests/QueryBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@ public function testResetSequence(): void
266266
$db->createCommand($sql)->execute();
267267
$result = $db->createCommand($checkSql)->queryScalar();
268268
$this->assertEquals(4, $result);
269+
270+
$sql = $qb->resetSequence('item', '1');
271+
$expected = <<<SQL
272+
declare
273+
lastSeq number := 1;
274+
begin
275+
if lastSeq IS NULL then lastSeq := 1; end if;
276+
execute immediate 'DROP SEQUENCE "item_SEQ"';
277+
execute immediate 'CREATE SEQUENCE "item_SEQ" START WITH ' || lastSeq || ' INCREMENT BY 1 NOMAXVALUE NOCACHE';
278+
end;
279+
SQL;
280+
$this->assertEquals($expected, $sql);
281+
282+
$db->createCommand($sql)->execute();
283+
$result = $db->createCommand($checkSql)->queryScalar();
284+
$this->assertEquals(1, $result);
269285
}
270286

271287
/**

0 commit comments

Comments
 (0)