Skip to content

Commit 4a7febb

Browse files
committed
fix(connection): transparently reconnect lost or evicted sessions
Store the ServerConfig for each active connection and add AppState::ensure_connection(), which recreates the adapter from the stored config when a session is lost or idle-evicted. Migrate all command lookups (browse, query, transfer, MCP bridge, capabilities) to it so user actions no longer fail with 'No active connection found'. The guardian's reconnect loop now recreates the adapter instead of pinging the stale one.
1 parent 37da22d commit 4a7febb

10 files changed

Lines changed: 108 additions & 229 deletions

File tree

‎src-tauri/src/capabilities/commands.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ fn to_metadata(cap: &Capability) -> Value {
5858

5959
async fn resolve_connection_config(app: &AppHandle, connection_id: &str) -> Result<Value, String> {
6060
let state: tauri::State<'_, crate::state::AppState> = app.state();
61-
let conns = state.connections.read().await;
62-
let _active = conns
63-
.get(connection_id)
64-
.ok_or_else(|| format!("Connection not found: {}", connection_id))?;
61+
state.ensure_connection(connection_id).await?;
6562

6663
Ok(json!({ "connectionId": connection_id }))
6764
}

‎src-tauri/src/capabilities/sql.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ async fn resolve_adapter(connection_id: &str) -> Result<ActiveConnection, String
6565
// Store the adapter for future use
6666
{
6767
let state: tauri::State<'_, crate::state::AppState> = app.state();
68-
let mut conns = state.connections.write().await;
69-
conns.insert(connection_id.to_string(), adapter.clone());
68+
state.connections.write().await.insert(connection_id.to_string(), adapter.clone());
69+
state.configs.write().await.insert(connection_id.to_string(), server_config);
7070
}
7171

7272
Ok(adapter)

‎src-tauri/src/commands/browse.rs‎

Lines changed: 22 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,7 @@ pub async fn list_databases(
147147
connection_id: String,
148148
state: State<'_, AppState>,
149149
) -> Result<Vec<DatabaseSchema>, String> {
150-
let connection = {
151-
let connections = state.connections.read().await;
152-
connections
153-
.get(&connection_id)
154-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
155-
.clone()
156-
};
150+
let connection = state.ensure_connection(&connection_id).await?;
157151
let databases = connection
158152
.list_databases()
159153
.await
@@ -179,13 +173,7 @@ pub async fn list_schemas(
179173
database: String,
180174
state: State<'_, AppState>,
181175
) -> Result<Vec<String>, String> {
182-
let connection = {
183-
let connections = state.connections.read().await;
184-
connections
185-
.get(&connection_id)
186-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
187-
.clone()
188-
};
176+
let connection = state.ensure_connection(&connection_id).await?;
189177

190178
let schemas = connection
191179
.list_schemas(Some(&database))
@@ -214,13 +202,7 @@ pub async fn list_tables(
214202
schema: Option<String>,
215203
state: State<'_, AppState>,
216204
) -> Result<Vec<TableInfo>, String> {
217-
let connection = {
218-
let connections = state.connections.read().await;
219-
connections
220-
.get(&connection_id)
221-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
222-
.clone()
223-
};
205+
let connection = state.ensure_connection(&connection_id).await?;
224206

225207
let tables = connection
226208
.list_tables(Some(&database), schema.as_deref())
@@ -251,13 +233,7 @@ pub async fn get_table_info(
251233
table_name: String,
252234
state: State<'_, AppState>,
253235
) -> Result<TableInfo, String> {
254-
let connection = {
255-
let connections = state.connections.read().await;
256-
connections
257-
.get(&connection_id)
258-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
259-
.clone()
260-
};
236+
let connection = state.ensure_connection(&connection_id).await?;
261237

262238
let table_info = connection
263239
.get_table_info(Some(&database), schema.as_deref(), &table_name)
@@ -276,13 +252,7 @@ pub async fn list_columns(
276252
table_name: String,
277253
state: State<'_, AppState>,
278254
) -> Result<Vec<ColumnInfo>, String> {
279-
let connection = {
280-
let connections = state.connections.read().await;
281-
connections
282-
.get(&connection_id)
283-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
284-
.clone()
285-
};
255+
let connection = state.ensure_connection(&connection_id).await?;
286256

287257
let columns = match &connection {
288258
ActiveConnection::Postgres(adapter) => {
@@ -344,13 +314,7 @@ pub async fn get_foreign_keys(
344314
schema: Option<String>,
345315
state: State<'_, AppState>,
346316
) -> Result<Vec<ForeignKeyInfo>, String> {
347-
let connection = {
348-
let connections = state.connections.read().await;
349-
connections
350-
.get(&connection_id)
351-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
352-
.clone()
353-
};
317+
let connection = state.ensure_connection(&connection_id).await?;
354318

355319
let result = connection
356320
.get_foreign_keys(Some(&database), schema.as_deref())
@@ -377,13 +341,7 @@ pub async fn get_table_data(
377341
query: TableDataQuery,
378342
state: State<'_, AppState>,
379343
) -> Result<QueryResult, String> {
380-
let connection = {
381-
let connections = state.connections.read().await;
382-
connections
383-
.get(&connection_id)
384-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
385-
.clone()
386-
};
344+
let connection = state.ensure_connection(&connection_id).await?;
387345

388346
let limit_val = query.limit.unwrap_or(100);
389347
let offset_val = query.offset.unwrap_or(0);
@@ -510,13 +468,7 @@ pub async fn get_table_count(
510468
filter: Option<String>,
511469
state: State<'_, AppState>,
512470
) -> Result<u64, String> {
513-
let connection = {
514-
let connections = state.connections.read().await;
515-
connections
516-
.get(&connection_id)
517-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
518-
.clone()
519-
};
471+
let connection = state.ensure_connection(&connection_id).await?;
520472

521473
let filter_ref = filter.as_deref();
522474
let db_type = get_db_type_string(&connection);
@@ -685,13 +637,7 @@ pub async fn update_table_row(
685637
return Ok(());
686638
}
687639

688-
let connection = {
689-
let connections = state.connections.read().await;
690-
connections
691-
.get(&connection_id)
692-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
693-
.clone()
694-
};
640+
let connection = state.ensure_connection(&connection_id).await?;
695641

696642
let db_type = get_db_type_string(&connection);
697643

@@ -823,13 +769,7 @@ pub async fn delete_table_row(
823769
return Err("Cannot delete row: no primary key values provided".to_string());
824770
}
825771

826-
let connection = {
827-
let connections = state.connections.read().await;
828-
connections
829-
.get(&connection_id)
830-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
831-
.clone()
832-
};
772+
let connection = state.ensure_connection(&connection_id).await?;
833773

834774
let db_type = get_db_type_string(&connection);
835775

@@ -930,13 +870,7 @@ pub async fn list_views(
930870
schema: Option<String>,
931871
state: State<'_, AppState>,
932872
) -> Result<Vec<ObjectInfo>, String> {
933-
let connection = {
934-
let connections = state.connections.read().await;
935-
connections
936-
.get(&connection_id)
937-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
938-
.clone()
939-
};
873+
let connection = state.ensure_connection(&connection_id).await?;
940874

941875
connection
942876
.list_views(Some(&database), schema.as_deref())
@@ -952,13 +886,7 @@ pub async fn list_procedures(
952886
schema: Option<String>,
953887
state: State<'_, AppState>,
954888
) -> Result<Vec<ObjectInfo>, String> {
955-
let connection = {
956-
let connections = state.connections.read().await;
957-
connections
958-
.get(&connection_id)
959-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
960-
.clone()
961-
};
889+
let connection = state.ensure_connection(&connection_id).await?;
962890

963891
connection
964892
.list_procedures(Some(&database), schema.as_deref())
@@ -974,13 +902,7 @@ pub async fn list_functions(
974902
schema: Option<String>,
975903
state: State<'_, AppState>,
976904
) -> Result<Vec<ObjectInfo>, String> {
977-
let connection = {
978-
let connections = state.connections.read().await;
979-
connections
980-
.get(&connection_id)
981-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
982-
.clone()
983-
};
905+
let connection = state.ensure_connection(&connection_id).await?;
984906

985907
connection
986908
.list_functions(Some(&database), schema.as_deref())
@@ -997,13 +919,7 @@ pub async fn list_triggers(
997919
table: String,
998920
state: State<'_, AppState>,
999921
) -> Result<Vec<TriggerInfo>, String> {
1000-
let connection = {
1001-
let connections = state.connections.read().await;
1002-
connections
1003-
.get(&connection_id)
1004-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1005-
.clone()
1006-
};
922+
let connection = state.ensure_connection(&connection_id).await?;
1007923

1008924
connection
1009925
.list_triggers(Some(&database), schema.as_deref(), &table)
@@ -1020,13 +936,7 @@ pub async fn list_indexes(
1020936
table: String,
1021937
state: State<'_, AppState>,
1022938
) -> Result<Vec<IndexInfo>, String> {
1023-
let connection = {
1024-
let connections = state.connections.read().await;
1025-
connections
1026-
.get(&connection_id)
1027-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1028-
.clone()
1029-
};
939+
let connection = state.ensure_connection(&connection_id).await?;
1030940

1031941
connection
1032942
.list_indexes(Some(&database), schema.as_deref(), &table)
@@ -1043,13 +953,7 @@ pub async fn list_foreign_keys(
1043953
table: String,
1044954
state: State<'_, AppState>,
1045955
) -> Result<Vec<ForeignKeyInfo>, String> {
1046-
let connection = {
1047-
let connections = state.connections.read().await;
1048-
connections
1049-
.get(&connection_id)
1050-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1051-
.clone()
1052-
};
956+
let connection = state.ensure_connection(&connection_id).await?;
1053957

1054958
connection
1055959
.list_foreign_keys_for_table(Some(&database), schema.as_deref(), &table)
@@ -1067,13 +971,7 @@ pub async fn get_object_ddl(
1067971
object_type: String,
1068972
state: State<'_, AppState>,
1069973
) -> Result<String, String> {
1070-
let connection = {
1071-
let connections = state.connections.read().await;
1072-
connections
1073-
.get(&connection_id)
1074-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1075-
.clone()
1076-
};
974+
let connection = state.ensure_connection(&connection_id).await?;
1077975

1078976
connection
1079977
.get_object_ddl(
@@ -1096,13 +994,7 @@ pub async fn drop_object(
1096994
object_type: String,
1097995
state: State<'_, AppState>,
1098996
) -> Result<(), String> {
1099-
let connection = {
1100-
let connections = state.connections.read().await;
1101-
connections
1102-
.get(&connection_id)
1103-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1104-
.clone()
1105-
};
997+
let connection = state.ensure_connection(&connection_id).await?;
1106998

1107999
connection
11081000
.drop_object(
@@ -1126,13 +1018,7 @@ pub async fn rename_object(
11261018
new_name: String,
11271019
state: State<'_, AppState>,
11281020
) -> Result<(), String> {
1129-
let connection = {
1130-
let connections = state.connections.read().await;
1131-
connections
1132-
.get(&connection_id)
1133-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?
1134-
.clone()
1135-
};
1021+
let connection = state.ensure_connection(&connection_id).await?;
11361022

11371023
connection
11381024
.rename_object(
@@ -1205,14 +1091,11 @@ pub async fn build_table_search_filter(
12051091
search_term: String,
12061092
state: State<'_, AppState>,
12071093
) -> Result<String, String> {
1208-
let connections = state.connections.read().await;
1209-
let connection = connections
1210-
.get(&connection_id)
1211-
.ok_or_else(|| format!("No active connection found for ID '{}'", connection_id))?;
1094+
let connection = state.ensure_connection(&connection_id).await?;
12121095

1213-
let db_type = get_db_type_string(connection);
1096+
let db_type = get_db_type_string(&connection);
12141097

1215-
let columns = match connection {
1098+
let columns = match &connection {
12161099
ActiveConnection::Postgres(adapter) => {
12171100
let adapter = adapter.lock().await;
12181101
if Some(database.as_str()) != adapter.config.database.as_deref() {

‎src-tauri/src/commands/connection.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub async fn connect_server(
2828
let mut connections = state.connections.write().await;
2929
connections.insert(id.clone(), connection.clone());
3030
drop(connections);
31+
// Keep the config so a lost session can be transparently recreated later.
32+
state.configs.write().await.insert(id.clone(), config);
3133
let status = connection
3234
.test_connection()
3335
.await
@@ -46,6 +48,8 @@ pub async fn disconnect_server(id: String, state: State<'_, AppState>) -> Result
4648
.remove(&id)
4749
.ok_or_else(|| format!("No active connection found for server '{}'", id))?;
4850

51+
state.configs.write().await.remove(&id);
52+
4953
let disconnect_result = connection.disconnect().await;
5054

5155
if let Err(e) = disconnect_result {

0 commit comments

Comments
 (0)