Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix: skip MySQL dump helper statements on import

t8y2e97e965ea1c9
diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rs
index 0b4dad8dff..2d919419ba 100644
--- a/crates/dbx-core/src/sql.rs
+++ b/crates/dbx-core/src/sql.rs
@@ -261,16 +261,24 @@ pub fn prepare_sql_file_statement(
driver_profile: Option<&str>,
) -> SqlFileStatementAction {
let statement = statement.trim();
+ let is_mysql_compatible_target = is_mysql_compatible_import_target(db_type, driver_profile);
+ if is_mysql_compatible_target && is_mysql_lock_table_statement(statement) {
+ return SqlFileStatementAction::Skip;
+ }
+
let Some(body) = mysql_executable_comment_body(statement) else {
+ if is_mysql_compatible_target && is_mysql_session_restore_statement(statement) {
+ return SqlFileStatementAction::Skip;
+ }
return SqlFileStatementAction::Execute(statement.to_string());
};
- if !is_mysql_compatible_import_target(db_type, driver_profile) {
+ if !is_mysql_compatible_target {
return SqlFileStatementAction::Skip;
}
let body = body.trim();
- if body.is_empty() || is_mysql_key_toggle_statement(body) {
+ if body.is_empty() || is_mysql_key_toggle_statement(body) || is_mysql_session_restore_statement(body) {
return SqlFileStatementAction::Skip;
}
@@ -359,6 +367,65 @@ fn is_mysql_key_toggle_statement(statement: &str) -> bool {
upper.starts_with("ALTER TABLE ") && (upper.ends_with(" ENABLE KEYS") || upper.ends_with(" DISABLE KEYS"))
}
+fn is_mysql_lock_table_statement(statement: &str) -> bool {
+ let executable = leading_executable_sql(statement);
+ let upper = executable.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_uppercase();
+ upper == "UNLOCK TABLES" || (upper.starts_with("LOCK TABLES ") && upper.ends_with(" WRITE"))
+}
+
+fn is_mysql_session_restore_statement(statement: &str) -> bool {
+ let executable = leading_executable_sql(statement);
+ let upper = executable.split_whitespace().collect::<Vec<_>>().join(" ").to_ascii_uppercase();
+ if !upper.starts_with("SET ") {
+ return false;
+ }
+
+ let assignment = upper.trim_start_matches("SET ").trim();
+ if assignment.starts_with('@') {
+ return false;
+ }
+
+ assignment.contains("= @OLD_")
+ || assignment.contains("=@OLD_")
+ || assignment.contains("= @SAVED_")
+ || assignment.contains("=@SAVED_")
+}
+
+fn leading_executable_sql(sql: &str) -> &str {
+ let bytes = sql.as_bytes();
+ let mut i = 0;
+
+ while i < bytes.len() {
+ while i < bytes.len() && bytes[i].is_ascii_whitespace() {
+ i += 1;
+ }
+
+ if i + 1 < bytes.len() && bytes[i] == b'-' && bytes[i + 1] == b'-' {
+ i += 2;
+ while i < bytes.len() && bytes[i] != b'\n' {
+ i += 1;
+ }
+ continue;
+ }
+
+ if i + 1 < bytes.len() && bytes[i] == b'/' && bytes[i + 1] == b'*' {
+ if i + 2 < bytes.len() && (bytes[i + 2] == b'!' || (i + 3 < bytes.len() && &bytes[i + 2..i + 4] == b"M!")) {
+ break;
+ }
+
+ let Some(close) = find_block_comment_close(bytes, i + 2) else {
+ return &sql[sql.len()..];
+ };
+ i = close + 2;
+ continue;
+ }
+
+ break;
+ }
+
+ &sql[i..]
+}
+
fn first_executable_sql_token(sql: &str) -> Option<&str> {
let bytes = sql.as_bytes();
let mut i = 0;
@@ -616,26 +683,70 @@ mod tests {
#[test]
fn prepares_mysql_executable_comments_for_mysql_compatible_imports() {
+ assert_eq!(
+ prepare_sql_file_statement("/*!40101 SET NAMES utf8mb4 */", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Execute("SET NAMES utf8mb4".to_string())
+ );
+ }
+
+ #[test]
+ fn skips_mysql_key_toggle_comments_for_mysql_compatible_imports() {
+ assert_eq!(
+ prepare_sql_file_statement(" /*!40000 ALTER TABLE `dd_admin` ENABLE KEYS */", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Skip
+ );
+ assert_eq!(
+ prepare_sql_file_statement("/*!40000 ALTER TABLE `dd_admin` DISABLE KEYS */", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Skip
+ );
+ }
+
+ #[test]
+ fn skips_mysql_lock_table_statements_for_mysql_compatible_imports() {
+ assert_eq!(
+ prepare_sql_file_statement("LOCK TABLES `dd_geo_json` WRITE", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Skip
+ );
+ assert_eq!(
+ prepare_sql_file_statement("UNLOCK TABLES", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Skip
+ );
assert_eq!(
prepare_sql_file_statement(
- "/*!40101 SET character_set_client = @saved_cs_client */",
+ "-- Dumping data for table `dd_geo_json`\nLOCK TABLES `dd_geo_json` WRITE",
&DatabaseType::Mysql,
None
),
- SqlFileStatementAction::Execute("SET character_set_client = @saved_cs_client".to_string())
+ SqlFileStatementAction::Skip
);
}
#[test]
- fn skips_mysql_key_toggle_comments_for_mysql_compatible_imports() {
+ fn skips_mysql_session_restore_statements_for_mysql_compatible_imports() {
assert_eq!(
- prepare_sql_file_statement(" /*!40000 ALTER TABLE `dd_admin` ENABLE KEYS */", &DatabaseType::Mysql, None),
+ prepare_sql_file_statement(
+ "/*!40101 SET character_set_client = @saved_cs_client */",
+ &DatabaseType::Mysql,
+ None
+ ),
SqlFileStatementAction::Skip
);
assert_eq!(
- prepare_sql_file_statement("/*!40000 ALTER TABLE `dd_admin` DISABLE KEYS */", &DatabaseType::Mysql, None),
+ prepare_sql_file_statement("/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */", &DatabaseType::Mysql, None),
SqlFileStatementAction::Skip
);
+ assert_eq!(
+ prepare_sql_file_statement("SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS", &DatabaseType::Mysql, None),
+ SqlFileStatementAction::Skip
+ );
+ assert_eq!(
+ prepare_sql_file_statement(
+ "/*!40101 SET @saved_cs_client = @@character_set_client */",
+ &DatabaseType::Mysql,
+ None
+ ),
+ SqlFileStatementAction::Execute("SET @saved_cs_client = @@character_set_client".to_string())
+ );
}
#[test]
diff --git a/docs/content/docs/sql-file.cn.mdx b/docs/content/docs/sql-file.cn.mdx
index e46a50b66d..b3282e91b7 100644
--- a/docs/content/docs/sql-file.cn.mdx
+++ b/docs/content/docs/sql-file.cn.mdx
@@ -46,7 +46,7 @@ DBX 会按流式方式读取 SQL 文件,并尽量正确识别语句边界,
- 字符串中的分号不会被当作语句结束
- 行注释和块注释中的分号不会被当作语句结束
- PostgreSQL 的 dollar-quoted 函数体会保持为同一条语句
-- MySQL executable comments 会在导入前按目标连接处理:MySQL 兼容连接会展开可执行内容,非 MySQL 连接会跳过;`ENABLE/DISABLE KEYS` 这类恢复优化指令会跳过
+- MySQL executable comments 会在导入前按目标连接处理:MySQL 兼容连接会展开可执行内容,非 MySQL 连接会跳过;`ENABLE/DISABLE KEYS`、`LOCK/UNLOCK TABLES`、session 状态恢复这类导入辅助指令会跳过
## 错误处理
diff --git a/docs/content/docs/sql-file.mdx b/docs/content/docs/sql-file.mdx
index 2b1b28ebc5..3fa5567ac3 100644
--- a/docs/content/docs/sql-file.mdx
+++ b/docs/content/docs/sql-file.mdx
@@ -46,7 +46,7 @@ DBX reads SQL files as a stream and tries to detect statement boundaries correct
- Semicolons inside strings are not treated as statement endings
- Semicolons inside line and block comments are ignored
- PostgreSQL dollar-quoted function bodies are kept together
-- MySQL executable comments are prepared for the target connection before import: MySQL-compatible connections unwrap executable content, non-MySQL connections skip it, and restore-only `ENABLE/DISABLE KEYS` directives are skipped
+- MySQL executable comments are prepared for the target connection before import: MySQL-compatible connections unwrap executable content, non-MySQL connections skip it, and import-helper directives such as `ENABLE/DISABLE KEYS`, `LOCK/UNLOCK TABLES`, and session state restores are skipped
## Error Handling