Skip to content

Commit cd47ccb

Browse files
feat(rust): Postgres sqlx adapter — connect + query stream (#1882)
Wire sqlx Postgres behind ConnectionRegistry with batched QueryEvent streaming (parity with SQLite). Session password is IPC-only; ProfileStore still never writes secrets. Live tests hit docker querya-postgres.
1 parent 596c69d commit cd47ccb

10 files changed

Lines changed: 1322 additions & 15 deletions

File tree

‎apps/tauri/src-tauri/Cargo.lock‎

Lines changed: 686 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/tauri/src-tauri/Cargo.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ serde_json = "1"
2121
uuid = { version = "1", features = ["v4", "serde"] }
2222
rusqlite = { version = "0.32", features = ["bundled"] }
2323
dirs = "5"
24+
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
25+
sqlx = { version = "0.8", default-features = false, features = [
26+
"runtime-tokio",
27+
"postgres",
28+
"json",
29+
"chrono",
30+
] }
31+
futures-util = "0.3"
32+
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
2433

2534
[profile.release]
2635
codegen-units = 1

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ pub fn db_connect_sqlite(
1515
registry.connect(ConnectionConfig::Sqlite { path, id })
1616
}
1717

18+
#[tauri::command]
19+
pub fn db_connect_postgres(
20+
host: String,
21+
port: u16,
22+
database: String,
23+
user: String,
24+
password: Option<String>,
25+
id: Option<String>,
26+
registry: State<'_, ConnectionRegistry>,
27+
) -> Result<ConnectionId, ConnectionError> {
28+
registry.connect(ConnectionConfig::Postgres {
29+
host,
30+
port,
31+
database,
32+
user,
33+
password,
34+
id,
35+
})
36+
}
37+
1838
#[tauri::command]
1939
pub fn profiles_list(
2040
store: State<'_, ProfileStore>,
@@ -208,7 +228,7 @@ pub fn db_execute_query(
208228
registry: State<'_, ConnectionRegistry>,
209229
) -> Result<(), ConnectionError> {
210230
let limit = limit.map(|n| n as usize);
211-
registry.execute_sqlite_query(&id, &sql, limit, |ev| {
231+
registry.execute_query(&id, &sql, limit, |ev| {
212232
on_event
213233
.send(ev)
214234
.map_err(|e| ConnectionError::io(format!("query channel closed: {e}")))

‎apps/tauri/src-tauri/src/connection/manager.rs‎

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::error::{ConnectionError, ConnectionResult};
22
use super::id::{ConnectionId, DriverKind};
3+
use super::postgres::PostgresSession;
34
use super::query::QueryEvent;
45
use super::sqlite::SqliteSession;
56
use serde::{Deserialize, Serialize};
@@ -19,19 +20,27 @@ pub enum ConnectionConfig {
1920
#[serde(default)]
2021
id: Option<String>,
2122
},
22-
/// Stub until the Postgres adapter epic wires connect.
2323
Postgres {
2424
host: String,
2525
port: u16,
2626
database: String,
2727
user: String,
28+
/// Session-only secret — never written to ProfileStore (keyring: #1878).
29+
#[serde(default)]
30+
password: Option<String>,
31+
#[serde(default)]
32+
id: Option<String>,
2833
},
2934
/// Stub until the MySQL adapter epic wires connect.
3035
Mysql {
3136
host: String,
3237
port: u16,
3338
database: String,
3439
user: String,
40+
#[serde(default)]
41+
password: Option<String>,
42+
#[serde(default)]
43+
id: Option<String>,
3544
},
3645
}
3746

@@ -82,7 +91,7 @@ impl ConnectionRegistry {
8291
Self::with_builtin_drivers()
8392
}
8493

85-
/// Registry with SQLite opener wired (PG/MySQL return UnsupportedDriver).
94+
/// Registry with SQLite + Postgres openers (MySQL still UnsupportedDriver).
8695
pub fn with_builtin_drivers() -> Self {
8796
Self::with_opener(|config| match config {
8897
ConnectionConfig::Sqlite { path, id } => {
@@ -94,6 +103,24 @@ impl ConnectionRegistry {
94103
}?;
95104
Ok(Arc::new(session) as SessionArc)
96105
}
106+
ConnectionConfig::Postgres {
107+
host,
108+
port,
109+
database,
110+
user,
111+
password,
112+
id,
113+
} => {
114+
let session = super::postgres::open_postgres(super::postgres::PostgresConnectParams {
115+
host,
116+
port,
117+
database,
118+
user,
119+
password,
120+
id: id.filter(|s| !s.trim().is_empty()).map(ConnectionId::from),
121+
})?;
122+
Ok(Arc::new(session) as SessionArc)
123+
}
97124
other => Err(ConnectionError::UnsupportedDriver {
98125
driver: other.driver_kind(),
99126
}),
@@ -153,6 +180,39 @@ impl ConnectionRegistry {
153180
self.with_sqlite(id, |s| s.execute_streaming(sql, limit, emit))
154181
}
155182

183+
pub fn with_postgres<T>(
184+
&self,
185+
id: &ConnectionId,
186+
f: impl FnOnce(&PostgresSession) -> ConnectionResult<T>,
187+
) -> ConnectionResult<T> {
188+
let session = self.session_arc(id)?;
189+
let pg = session
190+
.as_any()
191+
.downcast_ref::<PostgresSession>()
192+
.ok_or(ConnectionError::UnsupportedDriver {
193+
driver: session.driver(),
194+
})?;
195+
f(pg)
196+
}
197+
198+
/// Driver-aware query stream (SQLite + Postgres).
199+
pub fn execute_query(
200+
&self,
201+
id: &ConnectionId,
202+
sql: &str,
203+
limit: Option<usize>,
204+
emit: impl FnMut(QueryEvent) -> ConnectionResult<()>,
205+
) -> ConnectionResult<()> {
206+
match self.driver_of(id) {
207+
Some(DriverKind::Sqlite) => self.execute_sqlite_query(id, sql, limit, emit),
208+
Some(DriverKind::Postgres) => {
209+
self.with_postgres(id, |s| s.execute_streaming(sql, limit, emit))
210+
}
211+
Some(other) => Err(ConnectionError::UnsupportedDriver { driver: other }),
212+
None => Err(ConnectionError::NotFound { id: id.clone() }),
213+
}
214+
}
215+
156216
/// Driver-agnostic object tree for a live connection (SQLite first).
157217
pub fn object_tree(
158218
&self,
@@ -300,24 +360,42 @@ mod tests {
300360
}
301361

302362
#[test]
303-
fn registry_postgres_is_unsupported_for_now() {
363+
fn registry_mysql_is_unsupported_for_now() {
304364
let reg = ConnectionRegistry::with_builtin_drivers();
305365
let err = reg
306-
.connect(ConnectionConfig::Postgres {
366+
.connect(ConnectionConfig::Mysql {
307367
host: "localhost".into(),
308-
port: 5432,
368+
port: 3306,
309369
database: "db".into(),
310370
user: "u".into(),
371+
password: None,
372+
id: None,
311373
})
312374
.unwrap_err();
313375
assert!(matches!(
314376
err,
315377
ConnectionError::UnsupportedDriver {
316-
driver: DriverKind::Postgres
378+
driver: DriverKind::Mysql
317379
}
318380
));
319381
}
320382

383+
#[test]
384+
fn registry_postgres_rejects_empty_host() {
385+
let reg = ConnectionRegistry::with_builtin_drivers();
386+
let err = reg
387+
.connect(ConnectionConfig::Postgres {
388+
host: " ".into(),
389+
port: 5432,
390+
database: "querya".into(),
391+
user: "querya".into(),
392+
password: Some("querya".into()),
393+
id: None,
394+
})
395+
.unwrap_err();
396+
assert!(matches!(err, ConnectionError::InvalidConfig { .. }));
397+
}
398+
321399
#[test]
322400
fn execute_sqlite_query_streams_via_session() {
323401
let path = std::env::temp_dir().join(format!(

‎apps/tauri/src-tauri/src/connection/mod.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ mod id;
55
mod introspect;
66
mod manager;
77
mod object_tree;
8+
mod postgres;
9+
pub(crate) mod postgres_query;
810
mod query;
11+
mod runtime;
912
mod smoke_slice;
1013
mod sqlite;
1114
mod sqlite_introspect;

0 commit comments

Comments
 (0)