public function resetSequence($tableName, $value = null)
{
$db = $this->db;
$table = $db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
if ($value === null) {
$key = reset($table->primaryKey);
$tableName = $db->quoteTableName($tableName);
$value = $this->db->useMaster(function (Connection $db) use($key, $tableName) {
return $db->createCommand("SELECT MAX('{$key}') FROM {$tableName}")->queryScalar();
});
} else {
$value = (int) $value - 1;
}
try {
$db->createCommand("UPDATE sqlite_sequence SET seq='{$value}' WHERE name='{$table->name}'")->execute();
} catch (Exception $e) {
// it's possible that sqlite_sequence does not exist
}
} elseif ($table === null) {
throw new InvalidParamException("Table not found: {$tableName}");
} else {
throw new InvalidParamException("There is not sequence associated with table '{$tableName}'.'");
}
}