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。

feat(handoff): 增加连接 ID 和稳定排序

Illuminated2020037e08210429
diff --git a/crates/dbx-core/src/handoff.rs b/crates/dbx-core/src/handoff.rs
index 4f4d980c7f..b239647f2c 100644
--- a/crates/dbx-core/src/handoff.rs
+++ b/crates/dbx-core/src/handoff.rs
@@ -21,6 +21,8 @@ pub struct HandoffItem {
pub id: String,
pub created_at: DateTime<Utc>,
pub created_by: String,
+ #[serde(default)]
+ pub connection_id: String,
pub connection_name: String,
pub database: Option<String>,
pub title: String,
@@ -36,6 +38,7 @@ pub struct HandoffItem {
impl HandoffItem {
pub fn queued(
+ connection_id: String,
connection_name: String,
database: Option<String>,
title: String,
@@ -49,6 +52,7 @@ impl HandoffItem {
id: Uuid::new_v4().to_string(),
created_at: Utc::now(),
created_by: "dbx-cli".to_string(),
+ connection_id,
connection_name,
database,
title,
diff --git a/crates/dbx-core/src/storage.rs b/crates/dbx-core/src/storage.rs
index 43686c03a0..4447929499 100644
--- a/crates/dbx-core/src/storage.rs
+++ b/crates/dbx-core/src/storage.rs
@@ -86,7 +86,8 @@ const SCHEMA_STATEMENTS: &[&str] = &[
updated_at TEXT NOT NULL DEFAULT ''
)",
"CREATE TABLE IF NOT EXISTS handoffs (
- id TEXT PRIMARY KEY,
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
+ id TEXT NOT NULL UNIQUE,
payload_json TEXT NOT NULL,
status TEXT NOT NULL,
created_at TEXT NOT NULL
@@ -108,6 +109,7 @@ impl Storage {
sqlx::query(statement).execute(&pool).await.map_err(|e| e.to_string())?;
}
ensure_history_columns(&pool).await?;
+ ensure_handoffs_sequence(&pool).await?;
Ok(Self { db: pool })
}
@@ -141,6 +143,48 @@ async fn ensure_history_columns(pool: &SqlitePool) -> Result<(), String> {
Ok(())
}
+async fn ensure_handoffs_sequence(pool: &SqlitePool) -> Result<(), String> {
+ let (seq_columns,): (i64,) =
+ sqlx::query_as("SELECT COUNT(*) FROM pragma_table_info('handoffs') WHERE name = 'seq'")
+ .fetch_one(pool)
+ .await
+ .map_err(|e| e.to_string())?;
+
+ if seq_columns > 0 {
+ return Ok(());
+ }
+
+ let mut tx = pool.begin().await.map_err(|e| e.to_string())?;
+ sqlx::query(
+ "CREATE TABLE handoffs_migration (
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
+ id TEXT NOT NULL UNIQUE,
+ payload_json TEXT NOT NULL,
+ status TEXT NOT NULL,
+ created_at TEXT NOT NULL
+ )",
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(|e| e.to_string())?;
+ sqlx::query(
+ "INSERT INTO handoffs_migration (id, payload_json, status, created_at)
+ SELECT id, payload_json, status, created_at
+ FROM handoffs
+ ORDER BY created_at ASC, rowid ASC",
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(|e| e.to_string())?;
+ sqlx::query("DROP TABLE handoffs").execute(&mut *tx).await.map_err(|e| e.to_string())?;
+ sqlx::query("ALTER TABLE handoffs_migration RENAME TO handoffs")
+ .execute(&mut *tx)
+ .await
+ .map_err(|e| e.to_string())?;
+ tx.commit().await.map_err(|e| e.to_string())?;
+ Ok(())
+}
+
// ---------------------------------------------------------------------------
// History
// ---------------------------------------------------------------------------
@@ -262,8 +306,12 @@ impl Storage {
.ok_or_else(|| "Failed to serialize handoff status".to_string())?;
sqlx::query(
- "INSERT OR REPLACE INTO handoffs (id, payload_json, status, created_at) \
- VALUES (?, ?, ?, ?)",
+ "INSERT INTO handoffs (id, payload_json, status, created_at) \
+ VALUES (?, ?, ?, ?) \
+ ON CONFLICT(id) DO UPDATE SET \
+ payload_json = excluded.payload_json, \
+ status = excluded.status, \
+ created_at = excluded.created_at",
)
.bind(&item.id)
.bind(json)
@@ -280,7 +328,7 @@ impl Storage {
let rows: Vec<(String,)> = sqlx::query_as(
"SELECT payload_json FROM handoffs \
WHERE status IN ('queued', 'shown') \
- ORDER BY created_at ASC",
+ ORDER BY created_at ASC, seq ASC",
)
.fetch_all(&self.db)
.await
@@ -959,6 +1007,7 @@ mod handoff_tests {
fn queued_handoff(title: &str) -> HandoffItem {
HandoffItem::queued(
+ "prod-main-id".to_string(),
"prod-main".to_string(),
Some("app".to_string()),
title.to_string(),
@@ -990,6 +1039,42 @@ mod handoff_tests {
assert!(loaded[0].is_production);
}
+ #[tokio::test]
+ async fn load_pending_handoffs_keeps_fifo_order_for_matching_created_at() {
+ let storage = open_temp_storage().await;
+ let first = queued_handoff("first");
+ let mut second = queued_handoff("second");
+ second.created_at = first.created_at;
+
+ storage.save_handoff(&first).await.unwrap();
+ storage.save_handoff(&second).await.unwrap();
+
+ let loaded = storage.load_pending_handoffs().await.unwrap();
+
+ assert_eq!(loaded.iter().map(|item| item.title.as_str()).collect::<Vec<_>>(), vec!["first", "second"]);
+ }
+
+ #[tokio::test]
+ async fn handoffs_table_has_stable_autoincrement_sequence() {
+ let storage = open_temp_storage().await;
+
+ let columns: Vec<(String,)> =
+ sqlx::query_as("SELECT name FROM pragma_table_info('handoffs')").fetch_all(&storage.db).await.unwrap();
+
+ assert!(columns.iter().any(|(name,)| name == "seq"));
+ }
+
+ #[tokio::test]
+ async fn handoff_item_serializes_connection_id_and_display_name() {
+ let item = queued_handoff("serialize");
+
+ let value = serde_json::to_value(&item).unwrap();
+
+ assert_eq!(item.connection_id, "prod-main-id");
+ assert_eq!(value["connectionId"], "prod-main-id");
+ assert_eq!(value["connectionName"], "prod-main");
+ }
+
#[tokio::test]
async fn load_pending_handoffs_excludes_terminal_statuses() {
let storage = open_temp_storage().await;