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(compare): honor the connection query timeout for data compare (#13364)

dienasoa382bd25a4a8
diff --git a/crates/dbx-core/src/data/data_compare.rs b/crates/dbx-core/src/data/data_compare.rs
index 3c4ce7f7ae..005a4f8818 100644
--- a/crates/dbx-core/src/data/data_compare.rs
+++ b/crates/dbx-core/src/data/data_compare.rs
@@ -26,6 +26,21 @@ use super::sample_pairing::retain_shared_sample_rows_by_key;
const DATA_SYNC_INSERT_BATCH_SIZE: usize = 500;
const DATA_SYNC_CONDITION_BATCH_SIZE: usize = 200;
+/// 数据比对的查询选项:查询超时跟随连接配置(0 = 不限时),而不是
+/// `QueryExecutionOptions` 默认的 30 秒。大表比较(行计数、批量读取、采样)
+/// 常常超过 30 秒,用户调大连接的查询超时后即可完成(#13330)。
+async fn compare_query_options(
+ state: &AppState,
+ connection_id: &str,
+ max_rows: Option<usize>,
+) -> QueryExecutionOptions {
+ let timeout_secs = {
+ let configs = state.configs.read().await;
+ configs.get(connection_id).map(|config| config.effective_query_timeout_secs())
+ };
+ QueryExecutionOptions { max_rows, timeout_secs, ..Default::default() }
+}
+
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompareDataRowsOptions {
@@ -359,7 +374,7 @@ pub async fn prepare_data_compare_from_tables(
&source_count_sql,
Some(&options.source_schema),
None,
- QueryExecutionOptions { max_rows: Some(1), ..Default::default() },
+ compare_query_options(state, &options.source_connection_id, Some(1)).await,
),
execute_sql_statement_with_options(
state,
@@ -368,7 +383,7 @@ pub async fn prepare_data_compare_from_tables(
&target_count_sql,
Some(&options.target_schema),
None,
- QueryExecutionOptions { max_rows: Some(1), ..Default::default() },
+ compare_query_options(state, &options.target_connection_id, Some(1)).await,
)
)?;
let source_row_count = first_count(&source_count_result.rows)?;
@@ -554,7 +569,7 @@ pub async fn prepare_data_compare_missing_target(
&source_count_sql,
Some(&options.source_schema),
None,
- QueryExecutionOptions { max_rows: Some(1), ..Default::default() },
+ compare_query_options(state, &options.source_connection_id, Some(1)).await,
)
.await?;
let source_row_count = first_count(&source_count_result.rows)?;
@@ -1432,7 +1447,7 @@ async fn fetch_compare_rows_by_offset(
&sql,
Some(schema),
None,
- QueryExecutionOptions { max_rows: Some(batch_size), ..Default::default() },
+ compare_query_options(state, connection_id, Some(batch_size)).await,
)
.await?;
let fetched = result.rows.len();
@@ -1486,7 +1501,7 @@ async fn fetch_compare_rows_by_key(
&sql,
Some(schema),
None,
- QueryExecutionOptions { max_rows: Some(batch_size), ..Default::default() },
+ compare_query_options(state, connection_id, Some(batch_size)).await,
)
.await?;
let fetched = result.rows.len();
@@ -2046,7 +2061,7 @@ async fn fetch_sampled_compare_rows(
&sql,
Some(schema),
None,
- QueryExecutionOptions { max_rows: Some(sample_size), ..Default::default() },
+ compare_query_options(state, connection_id, Some(sample_size)).await,
)
.await?;
@@ -2179,6 +2194,41 @@ mod tests {
DataGridColumnInfo { extra: Some(extra.to_string()), ..data_compare_column(name, data_type) }
}
+ #[tokio::test]
+ async fn compare_query_options_follows_connection_query_timeout() {
+ // #13330: compare queries used to run on the 30-second default regardless of
+ // the connection's configured query timeout, so large tables could never
+ // finish. The compare options must carry the connection's setting, with an
+ // explicit 0 meaning "no limit".
+ let directory = tempfile::tempdir().unwrap();
+ let storage = crate::persistence::test_storage::open(&directory.path().join("state.db")).await.unwrap();
+ let state = AppState::new(storage);
+ let config: crate::models::connection::ConnectionConfig = serde_json::from_value(json!({
+ "id": "compare-timeout",
+ "name": "compare timeout",
+ "db_type": DatabaseType::Mysql,
+ "host": "localhost",
+ "port": 3306,
+ "username": "root",
+ "password": "",
+ "query_timeout_secs": 0,
+ }))
+ .unwrap();
+ state.configs.write().await.insert(config.id.clone(), config);
+
+ let unlimited = compare_query_options(&state, "compare-timeout", Some(500)).await;
+ assert_eq!(unlimited.timeout_secs, Some(0));
+
+ state.configs.write().await.get_mut("compare-timeout").unwrap().query_timeout_secs = 120;
+ let configured = compare_query_options(&state, "compare-timeout", Some(500)).await;
+ assert_eq!(configured.timeout_secs, Some(120));
+
+ // An unknown connection keeps the historical 30-second default instead of
+ // silently dropping the timeout.
+ let unknown = compare_query_options(&state, "missing", Some(500)).await;
+ assert_eq!(unknown.timeout_secs, None);
+ }
+
#[test]
fn compares_rows_by_primary_key_and_reports_added_removed_and_modified_rows() {
let diff = compare_data_rows(CompareDataRowsOptions {