diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rs index 2c2ab3909..1a5bc0ae7 100644 --- a/crates/dbx-core/src/schema.rs +++ b/crates/dbx-core/src/schema.rs @@ -1,6 +1,7 @@ use crate::connection::{AppState, MysqlMode, PoolKind}; use crate::db; use crate::models::connection::DatabaseType; +use std::future::Future; macro_rules! extract_pool { ($connections:expr, $key:expr, $variant:ident) => { @@ -242,6 +243,10 @@ fn clickhouse_metadata_database<'a>(database: &'a str, schema: &'a str) -> &'a s } pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Result, String> { + retry_metadata_connection(state, connection_id, None, || list_databases_once(state, connection_id)).await +} + +async fn list_databases_once(state: &AppState, connection_id: &str) -> Result, String> { log::info!("[list_databases] connection_id={connection_id}"); { let connections = state.connections.read().await; @@ -292,6 +297,13 @@ pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Resul } pub async fn list_schemas_core(state: &AppState, connection_id: &str, database: &str) -> Result, String> { + retry_metadata_connection(state, connection_id, Some(database), || { + list_schemas_once(state, connection_id, database) + }) + .await +} + +async fn list_schemas_once(state: &AppState, connection_id: &str, database: &str) -> Result, String> { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; { @@ -332,6 +344,20 @@ pub async fn list_tables_core( schema: &str, filter: Option<&str>, limit: Option, +) -> Result, String> { + retry_metadata_connection(state, connection_id, Some(database), || { + list_tables_once(state, connection_id, database, schema, filter, limit) + }) + .await +} + +async fn list_tables_once( + state: &AppState, + connection_id: &str, + database: &str, + schema: &str, + filter: Option<&str>, + limit: Option, ) -> Result, String> { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; let duckdb_attached_names = duckdb_attached_database_names(state, connection_id).await; @@ -469,6 +495,18 @@ pub async fn list_objects_core( connection_id: &str, database: &str, schema: &str, +) -> Result, String> { + retry_metadata_connection(state, connection_id, Some(database), || { + list_objects_once(state, connection_id, database, schema) + }) + .await +} + +async fn list_objects_once( + state: &AppState, + connection_id: &str, + database: &str, + schema: &str, ) -> Result, String> { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; @@ -540,6 +578,26 @@ pub async fn list_objects_core( } } +async fn retry_metadata_connection( + state: &AppState, + connection_id: &str, + database: Option<&str>, + mut operation: F, +) -> Result +where + F: FnMut() -> Fut, + Fut: Future>, +{ + let result = operation().await; + match result { + Err(error) if crate::query::is_connection_error(&error) => { + state.reconnect_pool(connection_id, database).await?; + operation().await + } + _ => result, + } +} + pub async fn get_columns_core( state: &AppState, connection_id: &str,