feat: Query history + saved queries (#5)

* fix: restore frontend test baseline (vitest jsdom env + tsc + mock fixes)

* feat: v6 migration — favorite column + queries table (Task 1)

* feat: extend TS types for favorites + saved queries (Task 2)

* feat: dedup consecutive + prune to 500 in insert_query_history (Task 3)

* feat: favorite column threading + set_history_favorite command (Task 4)

* feat: saved queries store CRUD (Task 5)

* feat: saved query commands + registration (Task 6)

* feat: queryStore — Zustand cache for history + saved queries (Task 7)

* feat: QueryHistoryDropdown — toolbar history dropdown (Task 8)

* feat: SaveQueryDialog — save query modal (Task 9)

* feat: QueryToolbar — add History + Save icons (Task 10)

* feat: QueriesPanel — History + Saved tabs (Task 11)

* feat: wire Queries view + toolbar to panel (Task 12)

* fix: global scope wrappers, empty/spinner states, cache invalidation (Task 13)

* feat: Queries view — history sidebar + tabbed query workspace (PR feedback)

* fix: toolbar action order + view-specific empty state (PR feedback)

* fix: view-specific empty state icon (PR feedback)

* fix: portal Tooltip to body so it is never clipped by overflow containers (PR feedback)

* feat: Queries sidebar — Explorer-style header + per-connection scoping (PR feedback)

* fix: move History/Saved dropdown to right side of Queries header (PR feedback)

* fix: History/Saved dropdown on its own row in Queries header (PR feedback)

* fix: Queries header order (search above dropdown) + sidebar width matches Explorer (PR feedback)

* fix: Queries header spacing — tight rows, pb-3 on container (PR feedback)

* fix: Queries header spacing — space-y-2 on container, no mb on title row (PR feedback)

* feat: history/saved rows click-to-load, remove sub-buttons (PR feedback)

* feat: merge Functions/Triggers/Sequences/Enums/Extensions into single Objects view (PR feedback)

* fix: Schema Visualizer nav icon — node graph glyph (PR feedback)

* fix: Objects view — type dropdown replaces static header label (PR feedback)

* fix: Objects empty state — remove bg circle, larger icon (PR feedback)

* fix: center icon in Objects empty state (PR feedback)

* feat: merge Backup/Restore/DB Sync into single Tools view (PR feedback)

* docs: update AGENTS.md + README for query history/saved queries, merged Objects + Tools views
This commit is contained in:
2026-08-01 17:43:40 +08:00
committed by GitHub
parent 80962d7d11
commit 7db66f1160
33 changed files with 3069 additions and 192 deletions
+211
View File
@@ -32,6 +32,46 @@ pub struct QueryHistoryEntry {
pub status: String,
pub error_message: Option<String>,
pub executed_at: String,
pub favorite: bool, // NEW — v6
}
// ---------------------------------------------------------------------------
// SavedQueryCommand
// ---------------------------------------------------------------------------
/// A saved query, returned to the frontend.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SavedQueryCommand {
pub id: String,
pub connection_id: Option<String>,
pub name: String,
pub query_text: String,
pub folder: String,
pub created_at: String,
pub updated_at: String,
}
/// Patch body for `update_saved_query` — all fields optional.
#[derive(Debug, Deserialize)]
pub struct UpdateSavedQueryPatch {
pub name: Option<String>,
#[serde(rename = "queryText")]
pub query_text: Option<String>,
pub folder: Option<String>,
}
impl From<crate::store::SavedQueryRow> for SavedQueryCommand {
fn from(r: crate::store::SavedQueryRow) -> Self {
Self {
id: r.id,
connection_id: r.connection_id,
name: r.name,
query_text: r.query_text,
folder: r.folder,
created_at: r.created_at,
updated_at: r.updated_at,
}
}
}
// ---------------------------------------------------------------------------
@@ -494,6 +534,57 @@ pub(crate) fn clear_query_history_inner(
store.clear_query_history(connection_id)
}
pub(crate) fn set_history_favorite_inner(
db_store: &std::sync::Mutex<crate::store::Store>,
id: &str,
connection_id: &str,
) -> Result<(), String> {
let store = db_store.lock().map_err(|e| e.to_string())?;
store.set_history_favorite(id, connection_id)
}
pub(crate) fn save_query_inner(
db_store: &std::sync::Mutex<crate::store::Store>,
connection_id: Option<String>,
name: String,
query_text: String,
folder: String,
) -> Result<SavedQueryCommand, String> {
let store = db_store.lock().map_err(|e| e.to_string())?;
store
.save_query(connection_id.as_deref(), &name, &query_text, &folder)
.map(SavedQueryCommand::from)
}
pub(crate) fn get_saved_queries_inner(
db_store: &std::sync::Mutex<crate::store::Store>,
connection_id: Option<String>,
) -> Result<Vec<SavedQueryCommand>, String> {
let store = db_store.lock().map_err(|e| e.to_string())?;
store
.list_saved_queries(connection_id.as_deref())
.map(|rows| rows.into_iter().map(SavedQueryCommand::from).collect())
}
pub(crate) fn update_saved_query_inner(
db_store: &std::sync::Mutex<crate::store::Store>,
id: String,
name: Option<String>,
query_text: Option<String>,
folder: Option<String>,
) -> Result<(), String> {
let store = db_store.lock().map_err(|e| e.to_string())?;
store.update_saved_query(&id, name.as_deref(), query_text.as_deref(), folder.as_deref())
}
pub(crate) fn delete_saved_query_inner(
db_store: &std::sync::Mutex<crate::store::Store>,
id: String,
) -> Result<(), String> {
let store = db_store.lock().map_err(|e| e.to_string())?;
store.delete_saved_query(&id)
}
// ---------------------------------------------------------------------------
// Tauri commands
// ---------------------------------------------------------------------------
@@ -534,6 +625,57 @@ pub async fn clear_query_history(
store.clear_query_history(connection_id.as_deref())
}
#[tauri::command]
pub async fn set_history_favorite(
id: String,
connection_id: String,
state: State<'_, crate::AppState>,
) -> Result<(), String> {
set_history_favorite_inner(&state.db_store, &id, &connection_id)
}
#[tauri::command]
pub async fn save_query(
connection_id: Option<String>,
name: String,
query_text: String,
folder: Option<String>,
state: State<'_, crate::AppState>,
) -> Result<SavedQueryCommand, String> {
save_query_inner(
&state.db_store,
connection_id,
name,
query_text,
folder.unwrap_or_default(),
)
}
#[tauri::command]
pub async fn get_saved_queries(
connection_id: Option<String>,
state: State<'_, crate::AppState>,
) -> Result<Vec<SavedQueryCommand>, String> {
get_saved_queries_inner(&state.db_store, connection_id)
}
#[tauri::command]
pub async fn update_saved_query(
id: String,
patch: UpdateSavedQueryPatch,
state: State<'_, crate::AppState>,
) -> Result<(), String> {
update_saved_query_inner(&state.db_store, id, patch.name, patch.query_text, patch.folder)
}
#[tauri::command]
pub async fn delete_saved_query(
id: String,
state: State<'_, crate::AppState>,
) -> Result<(), String> {
delete_saved_query_inner(&state.db_store, id)
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
@@ -735,6 +877,75 @@ mod tests {
}
}
// ------------------------------------------------------------------
// Test 5: Saved query command roundtrip (save → list → update → delete)
// ------------------------------------------------------------------
#[test]
fn save_query_command_roundtrip() {
use crate::models::ConnectionInput;
let conn = SqliteConnection::open_in_memory().unwrap();
crate::store::migrations::run_migrations(&conn).unwrap();
let store = Mutex::new(Store::from_connection(conn));
let sc2 = store
.lock()
.unwrap()
.create_connection(ConnectionInput {
name: "sc2".into(),
db_type: "postgresql".into(),
host: "h".into(),
port: Some(5432),
username: None,
folder_id: None,
tag_ids: vec![],
password: None,
database: None,
environment: None,
ssh_host: None,
ssh_port: None,
ssh_user: None,
ssh_auth_method: None,
ssh_private_key_path: None,
ssh_passphrase: None,
ssl_mode: None,
ssl_ca_path: None,
ssl_cert_path: None,
ssl_key_path: None,
})
.unwrap();
// save
let result = save_query_inner(
&store,
Some(sc2.id.clone()),
"Q1".to_string(),
"SELECT 1".to_string(),
"r".to_string(),
)
.unwrap();
assert_eq!(result.name, "Q1");
assert_eq!(result.connection_id, Some(sc2.id.clone()));
// list
let list = get_saved_queries_inner(&store, Some(sc2.id.clone())).unwrap();
assert_eq!(list.len(), 1);
// update (bogus id "update" — no-op, exercises the code path)
update_saved_query_inner(
&store,
"update".to_string(),
Some("Renamed".to_string()),
None,
None,
)
.unwrap();
// delete
delete_saved_query_inner(&store, result.id).unwrap();
let after = get_saved_queries_inner(&store, Some(sc2.id.clone())).unwrap();
assert!(after.is_empty());
}
// ------------------------------------------------------------------
// Helper: unwrap a DbHandle::Sqlite to get the connection reference
// ------------------------------------------------------------------