feat(mysql): add auto-increment counter action

Closes #3019
This commit is contained in:
t8y2 committed 2026-08-14 01:59:04 +00:00
1 parent 278429ab07
commit 4c6c3142c0
18 files changed
+456 -7

No files matched your search

+92
View File
@@ -101,6 +101,19 @@ pub struct TableAdminSqlOptions {
pub cascade: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MysqlAutoIncrementSqlOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub database_type: Option<DatabaseType>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub driver_profile: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
pub table_name: String,
pub value: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DropTableChildObjectSqlOptions {
@@ -501,6 +514,35 @@ pub fn build_truncate_table_sql(options: TableAdminSqlOptions) -> String {
}
}
pub fn build_mysql_auto_increment_sql(options: MysqlAutoIncrementSqlOptions) -> Result<String, String> {
let profile = options.driver_profile.as_deref().map(str::trim).unwrap_or_default();
if options.database_type != Some(DatabaseType::Mysql)
|| (!profile.is_empty() && !profile.eq_ignore_ascii_case("mysql"))
{
return Err("Setting AUTO_INCREMENT is supported only for native MySQL connections.".to_string());
}
let value = options.value.as_str();
if value.is_empty()
|| !value.bytes().all(|byte| byte.is_ascii_digit())
|| value.starts_with('0')
|| value.parse::<u64>().is_err()
{
return Err("AUTO_INCREMENT must be a decimal integer from 1 to 18446744073709551615.".to_string());
}
let table = if options.schema.as_deref().is_some_and(|schema| !schema.is_empty()) {
format!(
"{}.{}",
quote_rename_identifier(options.database_type, options.schema.as_deref().unwrap()),
quote_rename_identifier(options.database_type, &options.table_name)
)
} else {
quote_rename_identifier(options.database_type, &options.table_name)
};
Ok(format!("ALTER TABLE {table} AUTO_INCREMENT = {value};"))
}
fn supports_truncate_table_cascade(database_type: Option<DatabaseType>) -> bool {
matches!(
database_type,
@@ -1392,6 +1434,56 @@ mod tests {
);
}
#[test]
fn builds_mysql_auto_increment_sql_and_rejects_invalid_values() {
let options = MysqlAutoIncrementSqlOptions {
database_type: Some(DatabaseType::Mysql),
driver_profile: Some("mysql".to_string()),
schema: Some("sales`archive".to_string()),
table_name: "order`items".to_string(),
value: "18446744073709551615".to_string(),
};
assert_eq!(
build_mysql_auto_increment_sql(options),
Ok("ALTER TABLE `sales``archive`.`order``items` AUTO_INCREMENT = 18446744073709551615;".to_string())
);
for value in ["", "0", "01", " 1", "1 ", "+1", "-1", "1; DROP TABLE users", "18446744073709551616"] {
assert!(
build_mysql_auto_increment_sql(MysqlAutoIncrementSqlOptions {
database_type: Some(DatabaseType::Mysql),
driver_profile: None,
schema: None,
table_name: "events".to_string(),
value: value.to_string(),
})
.is_err(),
"value {value:?} should be rejected"
);
}
for (database_type, driver_profile) in [
(Some(DatabaseType::Postgres), None),
(Some(DatabaseType::Jdbc), Some("mysql".to_string())),
(Some(DatabaseType::Mysql), Some("mariadb".to_string())),
(Some(DatabaseType::Mysql), Some("tidb".to_string())),
(Some(DatabaseType::Mysql), Some("oceanbase".to_string())),
(Some(DatabaseType::Mysql), Some("dolt".to_string())),
(Some(DatabaseType::Goldendb), Some("goldendb".to_string())),
(Some(DatabaseType::Doris), Some("selectdb".to_string())),
(Some(DatabaseType::StarRocks), Some("starrocks".to_string())),
] {
assert!(build_mysql_auto_increment_sql(MysqlAutoIncrementSqlOptions {
database_type,
driver_profile,
schema: None,
table_name: "events".to_string(),
value: "1".to_string(),
})
.is_err());
}
}
#[test]
fn builds_drop_object_database_and_schema_sql() {
assert_eq!(