public function resetSequence($tableName, $value = null)
{
$table = $this->db->getTableSchema($tableName);
if ($table !== null && $table->sequenceName !== null) {
$tableName = $this->db->quoteTableName($tableName);
if ($value === null) {
$key = reset($table->primaryKey);
$value = $this->db->createCommand("SELECT MAX(`{$key}`) FROM {$tableName}")->queryScalar() + 1;
} else {
$value = (int) $value;
}
return "ALTER TABLE {$tableName} AUTO_INCREMENT={$value}";
} elseif ($table === null) {
throw new InvalidParamException("Table not found: {$tableName}");
} else {
throw new InvalidParamException("There is no sequence associated with table '{$tableName}'.");
}
}