diff --git a/core/crates/storage/src/paths.rs b/core/crates/storage/src/paths.rs index bcf4721904..515c6d5c13 100644 --- a/core/crates/storage/src/paths.rs +++ b/core/crates/storage/src/paths.rs @@ -46,6 +46,10 @@ impl StoragePaths { self.config.join("filter_settings.json") } + pub fn highlight_rules_file(&self) -> PathBuf { + self.config.join("highlight_rules.json") + } + pub fn drafts_dir(&self) -> PathBuf { self.data.join("drafts") } @@ -85,6 +89,10 @@ mod tests { assert_eq!(paths.runtime, root.join("runtime/app/app.tablepro.TablePro")); assert_eq!(paths.history_database(), root.join("state/tablepro/history.db")); assert_eq!(paths.connections_file(), root.join("config/tablepro/connections.json")); + assert_eq!( + paths.highlight_rules_file(), + root.join("config/tablepro/highlight_rules.json") + ); assert_eq!(paths.snippets_dir(), root.join("data/tablepro/snippets")); assert_eq!(paths.account_file(), root.join("state/tablepro/account.json")); assert_eq!(paths.telemetry_file(), root.join("state/tablepro/telemetry.json")); diff --git a/linux/changelog.d/4129.added.md b/linux/changelog.d/4129.added.md new file mode 100644 index 0000000000..c26a28dec4 --- /dev/null +++ b/linux/changelog.d/4129.added.md @@ -0,0 +1 @@ +Highlight Rules are kept for each table across restarts, including database files and other databases. diff --git a/linux/crates/app/src/application/app_services.rs b/linux/crates/app/src/application/app_services.rs index 3f9375b96b..454de6d95a 100644 --- a/linux/crates/app/src/application/app_services.rs +++ b/linux/crates/app/src/application/app_services.rs @@ -362,6 +362,7 @@ impl AppServices { )), column_layouts: self.storage.column_layouts().clone(), filters: self.storage.filter_settings().clone(), + highlight_rules: self.storage.highlight_rules().clone(), workspaces: self.storage.workspaces().clone(), history, spawner: self.tasks.clone(), diff --git a/linux/crates/app/src/persistence/column_layout_store.rs b/linux/crates/app/src/persistence/column_layout_store.rs index 5322b8d0c9..4dd6588edf 100644 --- a/linux/crates/app/src/persistence/column_layout_store.rs +++ b/linux/crates/app/src/persistence/column_layout_store.rs @@ -3,12 +3,10 @@ use std::rc::Rc; use tablepro_session::runtime::Tasks; use uuid::Uuid; -use super::StateFile; use super::column_layouts_document::ColumnLayoutsDocument; +use super::{StateFile, StateFileError, TableStateKey}; use crate::services::grid::ColumnLayout; -/// Each table's column order, hidden columns and widths, remembered per -/// connection, schema and table. #[derive(Clone)] pub struct ColumnLayoutStore { file: Rc>, @@ -31,16 +29,12 @@ impl ColumnLayoutStore { self.file.path() } - pub fn layout(&self, connection_id: Uuid, schema: Option<&str>, table: &str) -> ColumnLayout { - self.read(connection_id, schema, table, |layout| { - layout.cloned().unwrap_or_default() - }) + pub fn layout(&self, key: impl Into, schema: Option<&str>, table: &str) -> ColumnLayout { + self.read(&key.into(), schema, table, |layout| layout.cloned().unwrap_or_default()) } - /// Keep `layout` as the table's whole layout. The grid merges what it - /// shows into the stored layout before it saves. - pub fn save(&self, connection_id: Uuid, schema: Option<&str>, table: &str, layout: &ColumnLayout) { - self.change(connection_id, schema, table, |stored| { + pub fn save(&self, key: impl Into, schema: Option<&str>, table: &str, layout: &ColumnLayout) { + self.change(&key.into(), schema, table, |stored| { if stored == layout { return false; } @@ -49,20 +43,43 @@ impl ColumnLayoutStore { }); } - /// Drop everything for a connection the user deleted, so the file - /// does not grow with entries nothing can reach. pub fn forget_connection(&self, connection_id: Uuid) { - self.file - .update(|document| document.connections.remove(&connection_id.to_string()).is_some()); + self.file.update(|document| { + let before = document.connections.len(); + document + .connections + .retain(|key, _| !TableStateKey::belongs_to(key, connection_id)); + before != document.connections.len() + }); + } + + pub fn failing(&self) -> tokio::sync::watch::Receiver { + self.file.failing() + } + + pub fn revision(&self) -> u64 { + self.file.revision() + } + + pub fn has_unsaved_changes(&self) -> bool { + self.file.has_unsaved_changes() + } + + pub fn current_error(&self) -> Option { + self.file.current_error() + } + + pub fn retry(&self) -> bool { + self.file.retry() } - pub fn flush(&self) -> impl Future + Send + use<> { + pub fn flush(&self) -> impl Future> + Send + use<> { self.file.flush() } fn read( &self, - connection_id: Uuid, + key: &TableStateKey, schema: Option<&str>, table: &str, with: impl FnOnce(Option<&ColumnLayout>) -> R, @@ -71,33 +88,30 @@ impl ColumnLayoutStore { with( document .connections - .get(&connection_id.to_string()) + .get(key.as_str()) .and_then(|schemas| schemas.get(schema_key(schema))) .and_then(|tables| tables.get(table)), ) }) } - /// Change one table's layout. `change` returns whether anything moved. - /// A layout left with nothing in it is removed with any map it empties, - /// so a reset shrinks the file back. fn change( &self, - connection_id: Uuid, + key: &TableStateKey, schema: Option<&str>, table: &str, change: impl FnOnce(&mut ColumnLayout) -> bool, ) { - let mut layout = self.layout(connection_id, schema, table); + let mut layout = self.layout(key, schema, table); if !change(&mut layout) { return; } self.file.update(|document| match layout == ColumnLayout::default() { - true => prune(document, connection_id, schema, table), + true => prune(document, key, schema, table), false => { document .connections - .entry(connection_id.to_string()) + .entry(key.as_str().to_owned()) .or_default() .entry(schema_key(schema).to_owned()) .or_default() @@ -108,14 +122,13 @@ impl ColumnLayoutStore { } } -/// `None` is stored as the empty string so every JSON key is concrete. fn schema_key(schema: Option<&str>) -> &str { schema.unwrap_or("") } -fn prune(document: &mut ColumnLayoutsDocument, connection_id: Uuid, schema: Option<&str>, table: &str) -> bool { - let connection_key = connection_id.to_string(); - let Some(schemas) = document.connections.get_mut(&connection_key) else { +fn prune(document: &mut ColumnLayoutsDocument, key: &TableStateKey, schema: Option<&str>, table: &str) -> bool { + let connection_key = key.as_str(); + let Some(schemas) = document.connections.get_mut(connection_key) else { return false; }; let mut removed = false; @@ -126,7 +139,7 @@ fn prune(document: &mut ColumnLayoutsDocument, connection_id: Uuid, schema: Opti } } if schemas.is_empty() { - document.connections.remove(&connection_key); + document.connections.remove(connection_key); } removed } @@ -160,7 +173,7 @@ mod tests { let store = store_at(&path); store.save(id, Some("public"), "users", &arranged()); - store.flush().await; + store.flush().await.expect("saved"); let reopened = store_at(&path); assert_eq!(reopened.layout(id, Some("public"), "users"), arranged()); @@ -203,7 +216,7 @@ mod tests { store.save(id, Some("public"), "users", &arranged()); store.save(id, Some("public"), "users", &ColumnLayout::default()); - store.flush().await; + store.flush().await.expect("saved"); assert!( !read_text_file(&path).contains(&id.to_string()), diff --git a/linux/crates/app/src/persistence/filter_settings_store.rs b/linux/crates/app/src/persistence/filter_settings_store.rs index 69d50a9a88..dc396471b9 100644 --- a/linux/crates/app/src/persistence/filter_settings_store.rs +++ b/linux/crates/app/src/persistence/filter_settings_store.rs @@ -4,11 +4,9 @@ use tablepro_core::FilterSet; use tablepro_session::runtime::Tasks; use uuid::Uuid; -use super::StateFile; use super::filter_settings_document::FilterSettingsDocument; +use super::{StateFile, StateFileError, TableStateKey}; -/// The filter each table was left with, remembered per connection, -/// schema and table. #[derive(Clone)] pub struct FilterSettingsStore { file: Rc>, @@ -25,11 +23,12 @@ impl FilterSettingsStore { self.file.path() } - pub fn filters(&self, connection_id: Uuid, schema: Option<&str>, table: &str) -> FilterSet { + pub fn filters(&self, key: impl Into, schema: Option<&str>, table: &str) -> FilterSet { + let key = key.into(); self.file.read(|document| { document .connections - .get(&connection_id.to_string()) + .get(key.as_str()) .and_then(|schemas| schemas.get(schema_key(schema))) .and_then(|tables| tables.get(table)) .cloned() @@ -37,15 +36,16 @@ impl FilterSettingsStore { }) } - pub fn set_filters(&self, connection_id: Uuid, schema: Option<&str>, table: &str, set: FilterSet) { + pub fn set_filters(&self, key: impl Into, schema: Option<&str>, table: &str, set: FilterSet) { + let key = key.into(); self.file.update(|document| match set.is_empty() { // Clearing a filter shrinks the file back rather than // leaving an empty blob behind. - true => prune(document, connection_id, schema, table), + true => prune(document, &key, schema, table), false => { let slot = document .connections - .entry(connection_id.to_string()) + .entry(key.as_str().to_owned()) .or_default() .entry(schema_key(schema).to_owned()) .or_default() @@ -60,26 +60,48 @@ impl FilterSettingsStore { }); } - /// Drop everything for a connection the user deleted. pub fn forget_connection(&self, connection_id: Uuid) { - self.file - .update(|document| document.connections.remove(&connection_id.to_string()).is_some()); + self.file.update(|document| { + let before = document.connections.len(); + document + .connections + .retain(|key, _| !TableStateKey::belongs_to(key, connection_id)); + before != document.connections.len() + }); + } + + pub fn failing(&self) -> tokio::sync::watch::Receiver { + self.file.failing() + } + + pub fn revision(&self) -> u64 { + self.file.revision() + } + + pub fn has_unsaved_changes(&self) -> bool { + self.file.has_unsaved_changes() + } + + pub fn current_error(&self) -> Option { + self.file.current_error() + } + + pub fn retry(&self) -> bool { + self.file.retry() } - pub fn flush(&self) -> impl Future + Send + use<> { + pub fn flush(&self) -> impl Future> + Send + use<> { self.file.flush() } } -/// `None` is stored as the empty string so every JSON key is concrete. fn schema_key(schema: Option<&str>) -> &str { schema.unwrap_or("") } -/// Remove the entry and any map it leaves empty. -fn prune(document: &mut FilterSettingsDocument, connection_id: Uuid, schema: Option<&str>, table: &str) -> bool { - let connection_key = connection_id.to_string(); - let Some(schemas) = document.connections.get_mut(&connection_key) else { +fn prune(document: &mut FilterSettingsDocument, key: &TableStateKey, schema: Option<&str>, table: &str) -> bool { + let connection_key = key.as_str(); + let Some(schemas) = document.connections.get_mut(connection_key) else { return false; }; let mut removed = false; @@ -90,7 +112,7 @@ fn prune(document: &mut FilterSettingsDocument, connection_id: Uuid, schema: Opt } } if schemas.is_empty() { - document.connections.remove(&connection_key); + document.connections.remove(connection_key); } removed } @@ -140,7 +162,7 @@ mod tests { store.set_filters(id, Some("public"), "users", sample_set()); store.set_filters(id, Some("public"), "users", FilterSet::default()); - store.flush().await; + store.flush().await.expect("saved"); assert!(store.filters(id, Some("public"), "users").is_empty()); let written = std::fs::read_to_string(&path).expect("the file"); @@ -173,7 +195,7 @@ mod tests { let id = Uuid::new_v4(); let store = FilterSettingsStore::load(path.clone(), &paused_tasks()); store.set_filters(id, None, "users", sample_set()); - store.flush().await; + store.flush().await.expect("saved"); let reopened = FilterSettingsStore::load(path, &paused_tasks()); diff --git a/linux/crates/app/src/persistence/highlight_rules_document.rs b/linux/crates/app/src/persistence/highlight_rules_document.rs new file mode 100644 index 0000000000..48dbd91a93 --- /dev/null +++ b/linux/crates/app/src/persistence/highlight_rules_document.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; +use tablepro_storage::document::VersionedDocument; + +use crate::services::grid::HighlightRule; + +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +pub struct HighlightRulesDocument { + #[serde(default)] + pub connections: HashMap, +} + +pub type Schemas = HashMap; +pub type Tables = HashMap>; + +impl VersionedDocument for HighlightRulesDocument { + const KIND: &'static str = "highlight-rules"; + const VERSION: u32 = 1; +} diff --git a/linux/crates/app/src/persistence/highlight_rules_store.rs b/linux/crates/app/src/persistence/highlight_rules_store.rs new file mode 100644 index 0000000000..f90a6dd40b --- /dev/null +++ b/linux/crates/app/src/persistence/highlight_rules_store.rs @@ -0,0 +1,148 @@ +use std::rc::Rc; + +use tablepro_session::runtime::Tasks; +use uuid::Uuid; + +use super::highlight_rules_document::HighlightRulesDocument; +use super::{StateFile, StateFileError, TableStateKey}; +use crate::services::grid::HighlightRule; + +#[derive(Clone)] +pub struct HighlightRulesStore { + file: Rc>, +} + +impl HighlightRulesStore { + pub fn load(path: std::path::PathBuf, tasks: &Tasks) -> Self { + Self { + file: Rc::new(StateFile::load(path, tasks)), + } + } + + pub fn path(&self) -> &std::path::Path { + self.file.path() + } + + pub fn rules(&self, key: impl Into, schema: Option<&str>, table: &str) -> Vec { + let key = key.into(); + self.file.read(|document| { + document + .connections + .get(key.as_str()) + .and_then(|schemas| schemas.get(schema.unwrap_or(""))) + .and_then(|tables| tables.get(table)) + .cloned() + .unwrap_or_default() + }) + } + + pub fn set_rules( + &self, + key: impl Into, + schema: Option<&str>, + table: &str, + rules: Vec, + ) { + let key = key.into(); + self.file.update(|document| { + let schemas = document.connections.entry(key.as_str().to_owned()).or_default(); + let schema_key = schema.unwrap_or("").to_owned(); + if rules.is_empty() { + let removed = schemas + .get_mut(&schema_key) + .is_some_and(|tables| tables.remove(table).is_some()); + if schemas.get(&schema_key).is_some_and(|tables| tables.is_empty()) { + schemas.remove(&schema_key); + } + if schemas.is_empty() { + document.connections.remove(key.as_str()); + } + return removed; + } + let slot = schemas + .entry(schema_key) + .or_default() + .entry(table.to_owned()) + .or_default(); + if *slot == rules { + return false; + } + *slot = rules; + true + }); + } + + pub fn forget_connection(&self, connection_id: Uuid) { + self.file.update(|document| { + let before = document.connections.len(); + document + .connections + .retain(|key, _| !TableStateKey::belongs_to(key, connection_id)); + before != document.connections.len() + }); + } + + pub fn failing(&self) -> tokio::sync::watch::Receiver { + self.file.failing() + } + + pub fn revision(&self) -> u64 { + self.file.revision() + } + + pub fn has_unsaved_changes(&self) -> bool { + self.file.has_unsaved_changes() + } + + pub fn current_error(&self) -> Option { + self.file.current_error() + } + + pub fn retry(&self) -> bool { + self.file.retry() + } + + pub fn flush(&self) -> impl Future> + Send + use<> { + self.file.flush() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::paused_tasks; + + fn store(root: &tempfile::TempDir) -> HighlightRulesStore { + HighlightRulesStore::load(root.path().join("highlight_rules.json"), &paused_tasks()) + } + + #[tokio::test] + async fn rules_are_kept_per_table_and_schema() { + let root = tempfile::tempdir().expect("tempdir"); + let store = store(&root); + let id = Uuid::new_v4(); + let rule = HighlightRule::new("status"); + + store.set_rules(id, Some("public"), "orders", vec![rule.clone()]); + + assert_eq!(store.rules(id, Some("public"), "orders"), [rule]); + assert!(store.rules(id, Some("audit"), "orders").is_empty()); + assert!(store.rules(id, Some("public"), "users").is_empty()); + assert!(store.rules(Uuid::new_v4(), Some("public"), "orders").is_empty()); + } + + #[tokio::test] + async fn no_rules_removes_the_entry_and_a_deleted_connection_takes_its_rules() { + let root = tempfile::tempdir().expect("tempdir"); + let store = store(&root); + let id = Uuid::new_v4(); + store.set_rules(id, None, "orders", vec![HighlightRule::new("status")]); + store.set_rules(id, None, "orders", Vec::new()); + assert!(store.rules(id, None, "orders").is_empty()); + + store.set_rules(id, None, "users", vec![HighlightRule::new("name")]); + store.forget_connection(id); + + assert!(store.rules(id, None, "users").is_empty()); + } +} diff --git a/linux/crates/app/src/persistence/mod.rs b/linux/crates/app/src/persistence/mod.rs index df14d9dd69..7f316435d4 100644 --- a/linux/crates/app/src/persistence/mod.rs +++ b/linux/crates/app/src/persistence/mod.rs @@ -1,16 +1,14 @@ -//! The small versioned JSON files the app keeps beside the user's -//! state: column layouts and per-table filters. -//! -//! Each is read once at startup and answered from memory after that, so -//! nothing the user does waits on the disk. A file that cannot be read, -//! or that a newer TablePro wrote, is left exactly as it is. - mod column_layout_store; mod column_layouts_document; mod filter_settings_document; mod filter_settings_store; +mod highlight_rules_document; +mod highlight_rules_store; mod state_file; +mod table_state_key; pub use column_layout_store::ColumnLayoutStore; pub use filter_settings_store::FilterSettingsStore; -pub use state_file::StateFile; +pub use highlight_rules_store::HighlightRulesStore; +pub use state_file::{StateFile, StateFileError}; +pub use table_state_key::TableStateKey; diff --git a/linux/crates/app/src/persistence/state_file.rs b/linux/crates/app/src/persistence/state_file.rs index c22ab84591..d443384733 100644 --- a/linux/crates/app/src/persistence/state_file.rs +++ b/linux/crates/app/src/persistence/state_file.rs @@ -1,48 +1,98 @@ use std::cell::{Cell, RefCell}; use std::path::PathBuf; +use std::sync::{Arc, Mutex, PoisonError}; use tablepro_session::runtime::{LatestWinsWriter, Tasks}; use tablepro_storage::document::{VersionedDocument, decode_document, write_document_blocking}; -use tablepro_storage::{DocumentProblem, DocumentProblemKind}; - -/// A small versioned JSON file the app keeps in memory and writes back -/// behind the user. -/// -/// Reads answer from memory, so a drag never waits on the disk. Writes -/// go through a latest-wins writer on a blocking thread, so a burst -/// costs two writes rather than one per event. +use tablepro_storage::{DocumentProblem, DocumentProblemKind, StorageError}; +use tokio::sync::watch; + +#[derive(Debug, Clone, thiserror::Error)] +pub enum StateFileError { + #[error(transparent)] + Protected(Arc), + #[error(transparent)] + Write(Arc), + #[error("the state writer stopped before saving {}", .path.display())] + WriterStopped { path: PathBuf }, +} + +impl StateFileError { + pub fn is_retryable(&self) -> bool { + matches!(self, Self::Write(_)) + } +} + pub struct StateFile { path: PathBuf, value: RefCell, - persistence: Cell, - writer: LatestWinsWriter, + revision: Cell, + protected: bool, + status: Arc>, + failing: Arc>, + writer: LatestWinsWriter>, +} + +struct Pending { + sequence: u64, + document: D, } -/// Whether this file may be written back. -/// -/// A file a newer TablePro wrote, or one that cannot be read at all, is -/// left exactly as it is: overwriting it would destroy state the user -/// gets back by upgrading or by fixing permissions. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Persistence { - Enabled, - Suppressed, +struct WriteStatus { + submitted: u64, + completed: u64, + unsaved: bool, + result: Result<(), StateFileError>, + failure: Option, } impl StateFile where D: VersionedDocument + Default + Clone + Send + Sync + 'static, { - /// Read the file once at startup. It is a few kilobytes, so this is - /// cheaper than making every later read fallible. pub fn load(path: PathBuf, tasks: &Tasks) -> Self { - let (value, persistence) = read(&path); + let (value, problem) = read(&path); + let protected = problem.is_some(); + let failure = problem.map(|problem| StateFileError::Protected(Arc::new(problem))); + let status = Arc::new(Mutex::new(WriteStatus { + submitted: 0, + completed: 0, + unsaved: false, + result: failure.clone().map_or(Ok(()), Err), + failure, + })); + let failing = Arc::new(watch::channel(protected).0); let path_for_writes = path.clone(); - let writer = LatestWinsWriter::spawn(tasks, move |document: &D| write(&path_for_writes, document)); + let status_for_writes = status.clone(); + let failing_for_writes = failing.clone(); + let writer = LatestWinsWriter::spawn(tasks, move |pending: &Pending| { + let result = write_document_blocking(&path_for_writes, &pending.document) + .map_err(|error| StateFileError::Write(Arc::new(error))); + if let Err(error) = &result { + tracing::warn!(%error, kind = D::KIND, "could not save state"); + } + let mut status = status_for_writes.lock().unwrap_or_else(PoisonError::into_inner); + status.completed = pending.sequence; + status.result = result.clone(); + match result { + Err(error) => status.failure = Some(error), + // An older successful snapshot must not dismiss a failure while newer edits are pending. + Ok(()) if status.submitted == pending.sequence => { + status.failure = None; + status.unsaved = false; + } + Ok(()) => {} + } + let failed = status.failure.is_some(); + failing_for_writes.send_if_modified(|previous| std::mem::replace(previous, failed) != failed); + }); Self { path, value: RefCell::new(value), - persistence: Cell::new(persistence), + revision: Cell::new(0), + protected, + status, + failing, writer, } } @@ -55,8 +105,26 @@ where with(&self.value.borrow()) } - /// Change the document. `change` returns whether anything moved, so - /// a no-op edit costs no write. + pub fn failing(&self) -> watch::Receiver { + self.failing.subscribe() + } + + pub fn current_error(&self) -> Option { + self.status + .lock() + .unwrap_or_else(PoisonError::into_inner) + .failure + .clone() + } + + pub fn revision(&self) -> u64 { + self.revision.get() + } + + pub fn has_unsaved_changes(&self) -> bool { + self.status.lock().unwrap_or_else(PoisonError::into_inner).unsaved + } + pub fn update(&self, change: impl FnOnce(&mut D) -> bool) { let snapshot = { let mut value = self.value.borrow_mut(); @@ -65,24 +133,57 @@ where } value.clone() }; - if self.persistence.get() == Persistence::Enabled { - self.writer.put(snapshot); + self.revision.set(self.revision.get().wrapping_add(1)); + self.schedule(snapshot); + } + + pub fn retry(&self) -> bool { + if self.protected { + return false; } + self.schedule(self.value.borrow().clone()); + true } - /// Resolves once everything changed so far is on disk. - pub fn flush(&self) -> impl Future + Send + use { - self.writer.flush() + fn schedule(&self, document: D) { + let sequence = { + let mut status = self.status.lock().unwrap_or_else(PoisonError::into_inner); + status.unsaved = true; + if self.protected { + return; + } + status.submitted += 1; + status.submitted + }; + self.writer.put(Pending { sequence, document }); + } + + pub fn flush(&self) -> impl Future> + Send + use { + let target = self.status.lock().unwrap_or_else(PoisonError::into_inner).submitted; + let written = self.writer.flush(); + let status = self.status.clone(); + let failing = self.failing.clone(); + let path = self.path.clone(); + async move { + written.await; + let mut status = status.lock().unwrap_or_else(PoisonError::into_inner); + if status.completed < target { + // LatestWinsWriter also finishes a wait when its task stopped without calling the writer. + let error = StateFileError::WriterStopped { path }; + status.failure = Some(error.clone()); + failing.send_if_modified(|previous| !std::mem::replace(previous, true)); + return Err(error); + } + status.result.clone() + } } } -/// Load the document, applying the recovery rule for each way a file -/// can be unusable. -fn read(path: &std::path::Path) -> (D, Persistence) { +fn read(path: &std::path::Path) -> (D, Option) { let bytes = match std::fs::read(path) { Ok(bytes) => bytes, Err(error) if error.kind() == std::io::ErrorKind::NotFound => { - return (D::default(), Persistence::Enabled); + return (D::default(), None); } Err(error) => { return suppress(DocumentProblem::new( @@ -95,37 +196,27 @@ fn read(path: &std::path::Path) -> (D, Persisten }; match decode_document::(path, &bytes) { - Ok(document) => (document, Persistence::Enabled), - // A newer TablePro wrote this. Upgrading gets the state back; - // writing over it does not. + Ok(document) => (document, None), + // A newer TablePro can recover its own document; writing over it would destroy that option. Err(problem) if problem.is_newer_version() => suppress(problem), Err(problem) => start_over(path, problem), } } -fn suppress(problem: DocumentProblem) -> (D, Persistence) { +fn suppress(problem: DocumentProblem) -> (D, Option) { tracing::warn!(%problem, "keeping this file as it is and not saving to it this session"); - (D::default(), Persistence::Suppressed) + (D::default(), Some(problem)) } -/// Corrupt, unversioned or too old: nothing can read it and nothing -/// will, so it is moved aside under a stamped name and the app starts -/// from empty. The bytes stay on disk for anyone who wants to look. -fn start_over(path: &std::path::Path, problem: DocumentProblem) -> (D, Persistence) { +fn start_over(path: &std::path::Path, problem: DocumentProblem) -> (D, Option) { match tablepro_storage::fs::move_aside_blocking(path, std::time::SystemTime::now()) { Ok(moved) => tracing::warn!(%problem, moved = %moved.display(), "moved the unusable file aside"), Err(error) => { tracing::warn!(%problem, %error, "could not move the unusable file aside"); - return (D::default(), Persistence::Suppressed); + return (D::default(), Some(problem)); } } - (D::default(), Persistence::Enabled) -} - -fn write(path: &std::path::Path, document: &D) { - if let Err(error) = write_document_blocking(path, document) { - tracing::warn!(%error, kind = D::KIND, "could not save state"); - } + (D::default(), None) } #[cfg(test)] @@ -152,12 +243,12 @@ mod tests { path } - async fn push(file: &StateFile, item: &str) { + async fn push(file: &StateFile, item: &str) -> Result<(), StateFileError> { file.update(|sample| { sample.items.push(item.to_owned()); true }); - file.flush().await; + file.flush().await } #[tokio::test] @@ -167,7 +258,7 @@ mod tests { let file = StateFile::::load(path.clone(), &paused_tasks()); assert!(!path.exists()); - push(&file, "one").await; + push(&file, "one").await.expect("write"); let bytes = std::fs::read_to_string(&path).expect("the file was written"); assert!(bytes.contains("\"version\": 1"), "{bytes}"); @@ -194,7 +285,7 @@ mod tests { original ); - push(&file, "one").await; + push(&file, "one").await.expect("write"); assert!(path.exists(), "the store refused to write after starting over"); } @@ -215,7 +306,12 @@ mod tests { let path = seed(&root, original); let file = StateFile::::load(path.clone(), &paused_tasks()); - push(&file, "one").await; + assert!(!file.has_unsaved_changes(), "a failed read is not a local edit"); + assert!(matches!(push(&file, "one").await, Err(StateFileError::Protected(_)))); + assert!(file.has_unsaved_changes(), "protected local edits are still unsaved"); + assert!(*file.failing().borrow()); + assert!(!file.retry(), "retry must not overwrite protected data"); + assert!(!file.current_error().expect("read failure").is_retryable()); assert_eq!( std::fs::read_to_string(&path).expect("read"), @@ -234,7 +330,12 @@ mod tests { std::fs::create_dir(&path).expect("block the path"); let file = StateFile::::load(path.clone(), &paused_tasks()); - push(&file, "one").await; + assert!(!file.has_unsaved_changes(), "a failed read is not a local edit"); + assert!(matches!(push(&file, "one").await, Err(StateFileError::Protected(_)))); + assert!(file.has_unsaved_changes(), "protected local edits are still unsaved"); + assert!(*file.failing().borrow()); + assert!(!file.retry(), "retry must not overwrite protected data"); + assert!(!file.current_error().expect("read failure").is_retryable()); assert!(path.is_dir(), "the store wrote over the blocked path"); } @@ -246,9 +347,205 @@ mod tests { let file = StateFile::::load(path.clone(), &paused_tasks()); file.update(|_| false); - file.flush().await; + file.flush().await.expect("flush"); assert!(!path.exists(), "a no-op update still wrote the file"); + assert!(!file.has_unsaved_changes()); + } + + #[tokio::test] + async fn failed_writes_report_once_and_retry_saves_the_latest_document() { + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("sample.json"); + let file = StateFile::::load(path.clone(), &paused_tasks()); + let mut failing = file.failing(); + assert!(!*failing.borrow_and_update()); + assert!(!file.has_unsaved_changes()); + std::fs::create_dir(&path).expect("block writes after the initial read"); + + assert!(matches!(push(&file, "one").await, Err(StateFileError::Write(_)))); + assert!(failing.has_changed().expect("watch")); + assert!(file.has_unsaved_changes()); + assert!(*failing.borrow_and_update()); + assert!(file.current_error().expect("write error").is_retryable()); + assert!(push(&file, "two").await.is_err()); + assert!(!failing.has_changed().expect("watch"), "one toast per failure episode"); + + std::fs::remove_dir(&path).expect("unblock writes"); + file.update(|_| false); + assert!(file.flush().await.is_err(), "a no-op must not hide the failed write"); + assert!(file.retry()); + file.flush().await.expect("retry writes the latest snapshot"); + assert!(!file.has_unsaved_changes()); + assert!(file.current_error().is_none()); + assert!(!*failing.borrow_and_update()); + let reopened = StateFile::::load(path, &paused_tasks()); + assert_eq!(reopened.read(|sample| sample.items.clone()), ["one", "two"]); + } + + #[tokio::test] + async fn a_flush_captured_before_newer_edits_waits_for_its_snapshot() { + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("sample.json"); + let file = StateFile::::load(path.clone(), &paused_tasks()); + file.update(|sample| { + sample.items.push("one".to_owned()); + true + }); + let earlier = file.flush(); + file.update(|sample| { + sample.items.push("two".to_owned()); + true + }); + earlier.await.expect("the earlier snapshot or its successor landed"); + file.flush().await.expect("the latest snapshot landed"); + let reopened = StateFile::::load(path, &paused_tasks()); + assert_eq!(reopened.read(|sample| sample.items.clone()), ["one", "two"]); + } + + struct SerializeGate { + started: tokio::sync::mpsc::UnboundedSender<()>, + release: Mutex>, + } + + #[derive(Default, Clone, Deserialize)] + struct GatedSample { + items: Vec, + #[serde(skip)] + gate: Option>, + } + + impl Serialize for GatedSample { + fn serialize(&self, serializer: S) -> Result { + if let Some(gate) = &self.gate { + gate.started.send(()).map_err(serde::ser::Error::custom)?; + gate.release + .lock() + .map_err(serde::ser::Error::custom)? + .recv_timeout(std::time::Duration::from_secs(10)) + .map_err(serde::ser::Error::custom)?; + } + Sample { + items: self.items.clone(), + } + .serialize(serializer) + } + } + + impl VersionedDocument for GatedSample { + const KIND: &'static str = "gated-sample"; + const VERSION: u32 = 1; + } + + #[tokio::test] + async fn an_older_success_keeps_newer_edits_unsaved_when_their_write_fails() { + const WAIT: std::time::Duration = std::time::Duration::from_secs(10); + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("sample.json"); + let file = StateFile::::load(path.clone(), &paused_tasks()); + let gate = || { + let (started, starts) = tokio::sync::mpsc::unbounded_channel(); + let (release, released) = std::sync::mpsc::channel(); + ( + Arc::new(SerializeGate { + started, + release: Mutex::new(released), + }), + starts, + release, + ) + }; + let (first, mut first_started, release_first) = gate(); + let (second, mut second_started, release_second) = gate(); + file.update(|document| { + document.items = vec!["first".to_owned()]; + document.gate = Some(first); + true + }); + assert_eq!( + tokio::time::timeout(WAIT, first_started.recv()) + .await + .expect("first write starts"), + Some(()) + ); + let earlier = file.flush(); + file.update(|document| { + document.items = vec!["latest".to_owned()]; + document.gate = Some(second); + true + }); + release_first.send(()).expect("release first serializer"); + tokio::time::timeout(WAIT, earlier) + .await + .expect("first flush finishes") + .expect("first write succeeds"); + assert_eq!( + tokio::time::timeout(WAIT, second_started.recv()) + .await + .expect("second write starts"), + Some(()) + ); + assert!(file.has_unsaved_changes(), "the older success hid the pending edit"); + assert_eq!(file.revision(), 2); + let first_bytes = std::fs::read(&path).expect("first document landed"); + let first_document = decode_document::(&path, &first_bytes).expect("first document"); + assert_eq!(first_document.items, ["first"]); + + std::fs::remove_file(&path).expect("remove first document"); + std::fs::create_dir(&path).expect("block second write"); + release_second.send(()).expect("release second serializer"); + let latest = tokio::time::timeout(WAIT, file.flush()) + .await + .expect("second flush finishes"); + assert!(matches!(latest, Err(StateFileError::Write(_)))); + assert!(file.has_unsaved_changes(), "the failed latest edit must remain unsaved"); + assert_eq!(file.read(|document| document.items.clone()), ["latest"]); + } + + #[tokio::test] + async fn revisions_count_local_edits_even_when_protected_but_never_count_retries() { + for protected in [false, true] { + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("sample.json"); + if protected { + std::fs::write(&path, r#"{"version":99,"items":["kept"]}"#).expect("newer file"); + } + let file = StateFile::::load(path, &paused_tasks()); + assert_eq!(file.revision(), 0); + file.update(|_| false); + assert_eq!(file.revision(), 0); + assert_eq!(file.retry(), !protected); + assert_eq!(file.revision(), 0, "retrying is not a document edit"); + let result = push(&file, "one").await; + assert_eq!(result.is_err(), protected); + assert_eq!(file.revision(), 1); + assert_eq!(file.retry(), !protected); + assert_eq!(file.revision(), 1); + let result = push(&file, "two").await; + assert_eq!(result.is_err(), protected); + assert_eq!(file.revision(), 2); + } + } + + #[test] + fn a_stopped_writer_is_not_reported_as_a_successful_flush() { + use std::task::{Context, Poll, Waker}; + + let runtime = crate::test_support::test_runtime(); + let root = tempfile::tempdir().expect("tempdir"); + let file = StateFile::::load(root.path().join("sample.json"), &runtime.tasks()); + drop(runtime); + file.update(|sample| { + sample.items.push("never written".to_owned()); + true + }); + let mut flushing = std::pin::pin!(file.flush()); + let outcome = flushing.as_mut().poll(&mut Context::from_waker(Waker::noop())); + assert!(matches!( + outcome, + Poll::Ready(Err(StateFileError::WriterStopped { .. })) + )); + assert!(*file.failing().borrow()); } #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] @@ -280,7 +577,7 @@ mod tests { upgraded.items.push("new".to_owned()); true }); - file.flush().await; + file.flush().await.expect("flush"); let kept = copies(); assert_eq!(kept.len(), 1, "{kept:?}"); @@ -294,7 +591,7 @@ mod tests { let root = tempfile::tempdir().expect("tempdir"); let path = root.path().join("sample.json"); let file = StateFile::::load(path.clone(), &paused_tasks()); - push(&file, "one").await; + push(&file, "one").await.expect("write"); let reopened = StateFile::::load(path, &paused_tasks()); diff --git a/linux/crates/app/src/persistence/table_state_key.rs b/linux/crates/app/src/persistence/table_state_key.rs new file mode 100644 index 0000000000..22b9b1def7 --- /dev/null +++ b/linux/crates/app/src/persistence/table_state_key.rs @@ -0,0 +1,185 @@ +use tablepro_session::SessionKey; +use uuid::Uuid; + +use crate::workspace::WorkspaceFileName; + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct TableStateKey(String); + +impl TableStateKey { + pub fn for_session(session: &SessionKey) -> Self { + Self(WorkspaceFileName::for_key(session).storage_key().to_owned()) + } + + pub(super) fn as_str(&self) -> &str { + &self.0 + } + + pub(super) fn belongs_to(stored: &str, connection_id: Uuid) -> bool { + stored == connection_id.to_string() || stored.starts_with(&WorkspaceFileName::databases_prefix(connection_id)) + } +} + +impl From for TableStateKey { + fn from(id: Uuid) -> Self { + Self::for_session(&SessionKey::Saved(id)) + } +} + +impl From<&TableStateKey> for TableStateKey { + fn from(key: &TableStateKey) -> Self { + key.clone() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn saved_keys_keep_their_spelling_and_other_sessions_do_not_collide() { + let id = Uuid::new_v4(); + let saved = TableStateKey::from(id); + let sibling = TableStateKey::for_session(&SessionKey::SavedIn { + id, + database: "other".into(), + }); + let file = TableStateKey::for_session(&SessionKey::File("file:///tmp/data.sqlite".into())); + assert_eq!(saved.as_str(), id.to_string()); + assert_ne!(saved, sibling); + assert_ne!(saved, file); + assert_ne!(sibling, file); + assert!(TableStateKey::belongs_to(saved.as_str(), id)); + assert!(TableStateKey::belongs_to(sibling.as_str(), id)); + assert!(!TableStateKey::belongs_to(file.as_str(), id)); + assert!(!TableStateKey::belongs_to(sibling.as_str(), Uuid::new_v4())); + } +} + +#[cfg(test)] +mod persistence_tests { + use super::*; + use crate::persistence::{ColumnLayoutStore, FilterSettingsStore, HighlightRulesStore}; + use crate::services::grid::{ColumnLayout, HighlightRule}; + use crate::test_support::paused_tasks; + use tablepro_core::{FilterOp, FilterRule, FilterSet, FilterValue}; + + fn filter(value: &str) -> FilterSet { + FilterSet { + rules: vec![FilterRule { + column: "status".to_owned(), + op: FilterOp::Eq, + value: Some(FilterValue::Single(value.to_owned())), + }], + ..FilterSet::default() + } + } + + fn keys(id: Uuid) -> Vec { + vec![ + SessionKey::Saved(id), + SessionKey::SavedIn { + id, + database: "first".to_owned(), + }, + SessionKey::SavedIn { + id, + database: "second".to_owned(), + }, + SessionKey::File("file:///tmp/first.sqlite".to_owned()), + SessionKey::File("file:///tmp/second.sqlite".to_owned()), + SessionKey::Saved(Uuid::new_v4()), + ] + } + + #[tokio::test] + async fn each_session_keeps_all_table_state_across_reopening_the_stores() { + let root = tempfile::tempdir().expect("tempdir"); + let tasks = paused_tasks(); + let layout_path = root.path().join("layouts.json"); + let filter_path = root.path().join("filters.json"); + let rules_path = root.path().join("rules.json"); + let layouts = ColumnLayoutStore::load(layout_path.clone(), &tasks); + let filters = FilterSettingsStore::load(filter_path.clone(), &tasks); + let highlights = HighlightRulesStore::load(rules_path.clone(), &tasks); + let sessions = keys(Uuid::new_v4()); + let rules: Vec<_> = sessions + .iter() + .enumerate() + .map(|(index, _)| { + let mut rule = HighlightRule::new("status"); + rule.value = Some(FilterValue::Single(index.to_string())); + rule + }) + .collect(); + for (index, session) in sessions.iter().enumerate() { + let key = TableStateKey::for_session(session); + layouts.save( + &key, + Some("public"), + "orders", + &ColumnLayout::default().with_width("status", 100 + index as i32), + ); + filters.set_filters(&key, Some("public"), "orders", filter(&index.to_string())); + highlights.set_rules(&key, Some("public"), "orders", vec![rules[index].clone()]); + } + layouts.flush().await.expect("layouts saved"); + filters.flush().await.expect("filters saved"); + highlights.flush().await.expect("rules saved"); + drop((layouts, filters, highlights)); + let layouts = ColumnLayoutStore::load(layout_path, &tasks); + let filters = FilterSettingsStore::load(filter_path, &tasks); + let highlights = HighlightRulesStore::load(rules_path, &tasks); + for (index, session) in sessions.iter().enumerate() { + let key = TableStateKey::for_session(session); + assert_eq!( + layouts.layout(&key, Some("public"), "orders"), + ColumnLayout::default().with_width("status", 100 + index as i32) + ); + assert_eq!( + filters.filters(&key, Some("public"), "orders"), + filter(&index.to_string()) + ); + assert_eq!(highlights.rules(&key, Some("public"), "orders"), [rules[index].clone()]); + assert!(highlights.rules(&key, Some("audit"), "orders").is_empty()); + assert!(highlights.rules(&key, Some("public"), "users").is_empty()); + } + } + + #[tokio::test] + async fn deletion_purges_sibling_databases_from_all_table_state_files() { + let root = tempfile::tempdir().expect("tempdir"); + let tasks = paused_tasks(); + let layout_path = root.path().join("layouts.json"); + let filter_path = root.path().join("filters.json"); + let rules_path = root.path().join("rules.json"); + let layouts = ColumnLayoutStore::load(layout_path.clone(), &tasks); + let filters = FilterSettingsStore::load(filter_path.clone(), &tasks); + let highlights = HighlightRulesStore::load(rules_path.clone(), &tasks); + let dropped = Uuid::new_v4(); + let sessions = keys(dropped); + for session in &sessions { + let key = TableStateKey::for_session(session); + layouts.save(&key, None, "orders", &ColumnLayout::default().with_width("status", 100)); + filters.set_filters(&key, None, "orders", filter("open")); + highlights.set_rules(&key, None, "orders", vec![HighlightRule::new("status")]); + } + layouts.forget_connection(dropped); + filters.forget_connection(dropped); + highlights.forget_connection(dropped); + layouts.flush().await.expect("layouts purged"); + filters.flush().await.expect("filters purged"); + highlights.flush().await.expect("rules purged"); + drop((layouts, filters, highlights)); + let layouts = ColumnLayoutStore::load(layout_path, &tasks); + let filters = FilterSettingsStore::load(filter_path, &tasks); + let highlights = HighlightRulesStore::load(rules_path, &tasks); + for (index, session) in sessions.iter().enumerate() { + let key = TableStateKey::for_session(session); + let removed = index < 3; + assert_eq!(layouts.layout(&key, None, "orders") == ColumnLayout::default(), removed); + assert_eq!(filters.filters(&key, None, "orders").is_empty(), removed); + assert_eq!(highlights.rules(&key, None, "orders").is_empty(), removed); + } + } +} diff --git a/linux/crates/app/src/services/connection_deletion.rs b/linux/crates/app/src/services/connection_deletion.rs index 5d40e1743b..088ec9b3dc 100644 --- a/linux/crates/app/src/services/connection_deletion.rs +++ b/linux/crates/app/src/services/connection_deletion.rs @@ -9,7 +9,7 @@ use uuid::Uuid; use super::deletion_error::DeletionError; use super::deletion_report::{DeletionReport, PurgeFailure}; -use crate::persistence::{ColumnLayoutStore, FilterSettingsStore}; +use crate::persistence::{ColumnLayoutStore, FilterSettingsStore, HighlightRulesStore}; use crate::workspace::{WorkspaceSaveError, WorkspaceStore}; /// What the user chose in the delete dialog. @@ -31,6 +31,7 @@ pub struct ConnectionDeletion { pub connections: Arc, pub column_layouts: ColumnLayoutStore, pub filters: FilterSettingsStore, + pub highlight_rules: HighlightRulesStore, pub workspaces: WorkspaceStore, /// `None` when the history database did not open. pub history: Option, @@ -54,13 +55,25 @@ impl ConnectionDeletion { let mut report = DeletionReport::default(); self.column_layouts.forget_connection(id); self.filters.forget_connection(id); + self.highlight_rules.forget_connection(id); // Awaited inside the delete's hold, so a quit right after it // cannot leave them on disk. - self.column_layouts.flush().await; - self.filters.flush().await; + for result in [ + self.column_layouts.flush().await, + self.filters.flush().await, + self.highlight_rules.flush().await, + ] { + if let Err(error) = result { + report.purge_failures.push(PurgeFailure::Settings(error)); + } + } // Copies kept before an upgrade hold the connection's tables and filter values, and are not // worth keeping without them. - let files = [self.column_layouts.path().to_owned(), self.filters.path().to_owned()]; + let files = [ + self.column_layouts.path().to_owned(), + self.filters.path().to_owned(), + self.highlight_rules.path().to_owned(), + ]; let copies = self .spawner .spawn_blocking_task(move || { @@ -179,6 +192,7 @@ mod tests { vault: Arc, layouts: ColumnLayoutStore, filters: FilterSettingsStore, + highlight_rules: HighlightRulesStore, workspaces: WorkspaceStore, drafts: DraftStore, history: QueryHistory, @@ -251,6 +265,7 @@ mod tests { let vault = Arc::new(FakeSecretVault::new()); let layouts = ColumnLayoutStore::load(paths.column_layouts_file(), &tasks); let filters = FilterSettingsStore::load(paths.filter_settings_file(), &tasks); + let highlight_rules = HighlightRulesStore::load(paths.highlight_rules_file(), &tasks); let drafts = DraftStore::new(paths.drafts_dir()); let workspaces = WorkspaceStore::start(paths.workspaces_dir(), drafts.clone(), &tasks); let history = QueryHistory::open(&paths).await.expect("the history"); @@ -268,6 +283,12 @@ mod tests { .expect("the password"); layouts.save(connection_id, Some("public"), "users", &named_width()); filters.set_filters(connection_id, Some("public"), "users", filter()); + highlight_rules.set_rules( + connection_id, + Some("public"), + "users", + vec![crate::services::grid::HighlightRule::new("name")], + ); let key = SessionKey::Saved(connection_id); let draft = DraftId::new(); drafts @@ -309,6 +330,7 @@ mod tests { vault, layouts, filters, + highlight_rules, workspaces, drafts, history, @@ -330,6 +352,7 @@ mod tests { }), column_layouts: self.layouts.clone(), filters: self.filters.clone(), + highlight_rules: self.highlight_rules.clone(), workspaces: self.workspaces.clone(), history: Some(self.history.clone()), spawner: paused_tasks(), @@ -502,6 +525,7 @@ mod tests { for file in [ fixture.paths.column_layouts_file(), fixture.paths.filter_settings_file(), + fixture.paths.highlight_rules_file(), ] { let on_disk = std::fs::read_to_string(&file).expect("the file"); assert!( @@ -525,6 +549,28 @@ mod tests { fixture.kept_everything_for(fixture.other).await; } + #[tokio::test] + async fn a_failed_table_state_purge_is_reported() { + let fixture = fixture().await; + fixture.highlight_rules.flush().await.expect("initial write"); + let path = fixture.paths.highlight_rules_file(); + std::fs::remove_file(&path).expect("remove saved file"); + std::fs::create_dir(&path).expect("block replacement"); + let report = fixture + .deletion() + .run(fixture.id, DeletionOptions::default()) + .await + .expect("deleted entry"); + assert!(!fixture.listed(fixture.id)); + assert!( + report + .purge_failures + .iter() + .any(|failure| matches!(failure, PurgeFailure::Settings(_))) + ); + assert!(report.summary("Prod").expect("purge notice").contains("settings")); + } + #[tokio::test] async fn history_kept_unless_requested() { let fixture = fixture().await; diff --git a/linux/crates/app/src/services/deletion_report.rs b/linux/crates/app/src/services/deletion_report.rs index ba666cb0c0..400c518721 100644 --- a/linux/crates/app/src/services/deletion_report.rs +++ b/linux/crates/app/src/services/deletion_report.rs @@ -18,7 +18,7 @@ impl DeletionReport { /// behind, or `None` when it left nothing. pub fn summary(&self, name: &str) -> Option { let left = |wanted: fn(&PurgeFailure) -> bool| self.purge_failures.iter().any(wanted); - let tabs = left(|failure| matches!(failure, PurgeFailure::Workspace(_))); + let tabs = left(|failure| matches!(failure, PurgeFailure::Workspace(_) | PurgeFailure::Settings(_))); let queries = left(|failure| matches!(failure, PurgeFailure::SavedQueries(_))); let history = left(|failure| matches!(failure, PurgeFailure::History(_))); let named = &[("name", name)]; @@ -51,6 +51,8 @@ impl DeletionReport { #[derive(Debug, Error)] pub enum PurgeFailure { + #[error(transparent)] + Settings(crate::persistence::StateFileError), #[error(transparent)] Workspace(WorkspaceSaveError), #[error("could not delete the saved queries: {0}")] diff --git a/linux/crates/app/src/storage.rs b/linux/crates/app/src/storage.rs index 74effe3924..7f4662d419 100644 --- a/linux/crates/app/src/storage.rs +++ b/linux/crates/app/src/storage.rs @@ -4,7 +4,7 @@ use tablepro_core::credentials::SecretVault; use tablepro_session::runtime::Tasks; use tablepro_storage::{ConnectionStore, StoragePaths}; -use crate::persistence::{ColumnLayoutStore, FilterSettingsStore}; +use crate::persistence::{ColumnLayoutStore, FilterSettingsStore, HighlightRulesStore}; use crate::workspace::WorkspaceStore; /// Everything the app persists, resolved once at startup and handed to @@ -17,6 +17,7 @@ pub struct AppStorage { secrets: std::sync::Arc, column_layouts: ColumnLayoutStore, filter_settings: FilterSettingsStore, + highlight_rules: HighlightRulesStore, workspaces: WorkspaceStore, } @@ -27,6 +28,7 @@ impl AppStorage { let connections = ConnectionStore::new(&paths); let column_layouts = ColumnLayoutStore::load(paths.column_layouts_file(), tasks); let filter_settings = FilterSettingsStore::load(paths.filter_settings_file(), tasks); + let highlight_rules = HighlightRulesStore::load(paths.highlight_rules_file(), tasks); let workspaces = WorkspaceStore::start( paths.workspaces_dir(), tablepro_storage::DraftStore::new(paths.drafts_dir()), @@ -38,6 +40,7 @@ impl AppStorage { secrets, column_layouts, filter_settings, + highlight_rules, workspaces, } } @@ -62,6 +65,10 @@ impl AppStorage { &self.filter_settings } + pub fn highlight_rules(&self) -> &HighlightRulesStore { + &self.highlight_rules + } + pub fn workspaces(&self) -> &WorkspaceStore { &self.workspaces } diff --git a/linux/crates/app/src/ui/browse_tab/data_handlers.rs b/linux/crates/app/src/ui/browse_tab/data_handlers.rs index cd4362927a..08321ab643 100644 --- a/linux/crates/app/src/ui/browse_tab/data_handlers.rs +++ b/linux/crates/app/src/ui/browse_tab/data_handlers.rs @@ -35,13 +35,19 @@ impl BrowseModel { self.grid.commit_edit(); self.sequencer.cancel_all(); self.table = name; - if let Some(connection) = self.connection_id { + if let Some(state_key) = self.state_key.as_ref() { self.filter_settings.set_filters( - connection, + state_key, self.schema.as_deref(), &self.table, self.current_filter.clone(), ); + self.highlight_rules.set_rules( + state_key, + self.schema.as_deref(), + &self.table, + self.grid.highlight_rules(), + ); } self.grid.rename_table(&self.table); let delta = self.tracker.set_target(SaveTarget { diff --git a/linux/crates/app/src/ui/browse_tab/layout.rs b/linux/crates/app/src/ui/browse_tab/layout.rs index ebd8c986d1..f13b5dc67d 100644 --- a/linux/crates/app/src/ui/browse_tab/layout.rs +++ b/linux/crates/app/src/ui/browse_tab/layout.rs @@ -142,7 +142,7 @@ pub(super) fn filter_strip(tab: &BrowseTab, initial: &FilterSet, dialect: Option pub(super) struct GridInit { pub(super) table: TableRef, pub(super) label: String, - pub(super) connection_id: Option, + pub(super) state_key: Option, pub(super) sorting: GridSorting, pub(super) tracker: TabTrackerHandle, pub(super) settings: Rc, @@ -160,9 +160,9 @@ pub(super) struct GridInit { pub(super) fn grid(tab: &BrowseTab, init: GridInit) -> DataGrid { let session = init.session; let dialect: DialectSource = Rc::new(move || session.upgrade()?.current_dialect()); - let layout = init.connection_id.map(|connection_id| LayoutBinding { + let layout = init.state_key.map(|state_key| LayoutBinding { store: init.layouts, - connection_id, + state_key, schema: init.table.schema.clone(), table: init.table.name.clone(), }); diff --git a/linux/crates/app/src/ui/browse_tab/mod.rs b/linux/crates/app/src/ui/browse_tab/mod.rs index bf262b3d9a..c27763e1e3 100644 --- a/linux/crates/app/src/ui/browse_tab/mod.rs +++ b/linux/crates/app/src/ui/browse_tab/mod.rs @@ -103,7 +103,7 @@ pub struct BrowseTabInit { pub tracker: TabTrackerHandle, pub schema: Option, pub table: String, - pub connection_id: Option, + pub state_key: Option, pub read_only: bool, pub page_size: u64, pub initial_offset: u64, @@ -111,6 +111,7 @@ pub struct BrowseTabInit { pub filter: TableFilter, pub column_layouts: crate::persistence::ColumnLayoutStore, pub filter_settings: crate::persistence::FilterSettingsStore, + pub highlight_rules: crate::persistence::HighlightRulesStore, /// The session the tab reads through. The tab belongs to it and is /// closed before it shuts down. pub session: Arc, @@ -128,7 +129,8 @@ impl BrowseTab { let tab: Self = glib::Object::new(); // The model and the inline strip start with the same FilterSet. let (initial_filter, reveal_filter) = init.filter.opening(|| { - init.connection_id + init.state_key + .as_ref() .map(|id| init.filter_settings.filters(id, init.schema.as_deref(), &init.table)) .unwrap_or_default() }); @@ -165,7 +167,7 @@ impl BrowseTab { layout::GridInit { table, label: table_label, - connection_id: init.connection_id, + state_key: init.state_key.clone(), sorting: grid_sorting(dialect.as_deref(), initial_sort), tracker: init.tracker.clone(), settings: init.settings, @@ -175,6 +177,7 @@ impl BrowseTab { tasks: init.tasks.clone(), }, ); + restore_highlights(&grid, &init.highlight_rules, init.state_key.clone()); let link_changes = init.session.link_changes(); let model = BrowseModel { this: tab.downgrade(), @@ -192,9 +195,10 @@ impl BrowseTab { tracker: init.tracker, schema: init.schema, table: init.table, - connection_id: init.connection_id, + state_key: init.state_key, read_only: init.read_only, filter_settings: init.filter_settings, + highlight_rules: init.highlight_rules, pager: BrowsePager::new(init.page_size, init.initial_offset), deadlines: init.deadlines, current_sort: initial_sort, @@ -596,9 +600,10 @@ pub(crate) struct BrowseModel { tracker: TabTrackerHandle, schema: Option, table: String, - connection_id: Option, + state_key: Option, read_only: bool, filter_settings: crate::persistence::FilterSettingsStore, + highlight_rules: crate::persistence::HighlightRulesStore, /// Where the page the tab reads starts, and how many rows it holds. pager: BrowsePager, @@ -707,6 +712,31 @@ impl BrowseModel { } } +fn restore_highlights( + grid: &DataGrid, + store: &crate::persistence::HighlightRulesStore, + state_key: Option, +) { + let Some(state_key) = state_key else { + return; + }; + let opened = grid.config().and_then(|config| config.table.clone()); + if let Some(table) = opened { + let rules = store.rules(&state_key, table.schema.as_deref(), &table.name); + if !rules.is_empty() { + grid.set_highlight_rules(rules); + } + } + let store = store.clone(); + // The grid's table, not the opening one, so a rename moves the rules with it. + grid.connect_highlight_rules_changed(move |grid| { + let Some(table) = grid.config().and_then(|config| config.table.clone()) else { + return; + }; + store.set_rules(&state_key, table.schema.as_deref(), &table.name, grid.highlight_rules()); + }); +} + /// Whether a page of a table can be sorted on its engine. Without a /// connection yet, a sort is kept for when one opens. fn sorts_rows(dialect: Option<&dyn SqlDialect>) -> bool { diff --git a/linux/crates/app/src/ui/browse_tab/paging_handlers.rs b/linux/crates/app/src/ui/browse_tab/paging_handlers.rs index 1416dd15a4..f7b14df4a6 100644 --- a/linux/crates/app/src/ui/browse_tab/paging_handlers.rs +++ b/linux/crates/app/src/ui/browse_tab/paging_handlers.rs @@ -51,9 +51,9 @@ impl BrowseModel { return; } self.current_filter = set.clone(); - if let Some(conn_id) = self.connection_id { + if let Some(state_key) = self.state_key.as_ref() { self.filter_settings - .set_filters(conn_id, self.schema.as_deref(), &self.table, set.clone()); + .set_filters(state_key, self.schema.as_deref(), &self.table, set.clone()); } // Filtered counts shift, so back to page 1. self.pager.restart(); diff --git a/linux/crates/app/src/ui/browse_tab/tests/mod.rs b/linux/crates/app/src/ui/browse_tab/tests/mod.rs index a18735d900..525d664907 100644 --- a/linux/crates/app/src/ui/browse_tab/tests/mod.rs +++ b/linux/crates/app/src/ui/browse_tab/tests/mod.rs @@ -423,7 +423,7 @@ pub(super) fn table_tab( tracker, schema: None, table: table.to_owned(), - connection_id: Some(connection_id), + state_key: Some(connection_id.into()), read_only, page_size: 100, initial_offset: 0, @@ -431,6 +431,7 @@ pub(super) fn table_tab( filter, column_layouts: crate::persistence::ColumnLayoutStore::load(root.join("column_layouts.json"), &tasks), filter_settings, + highlight_rules: crate::persistence::HighlightRulesStore::load(root.join("highlight_rules.json"), &tasks), session: session.clone(), tasks, deadlines: Arc::new(tablepro_session::operation::DefaultDeadlines::new( @@ -1958,6 +1959,34 @@ fn the_bottom_bar_offers_the_column_chooser() { window.visible_dialog().expect("the chooser").close(); } +#[gtk4::test] +fn a_renamed_table_keeps_its_highlight_rules_under_the_new_name() { + use crate::services::grid::HighlightRule; + use gtk4::subclass::prelude::ObjectSubclassIsExt; + + let mut script = serving_table(); + script.query_bound.always(FakeReply::ok(rows(&["Ada"]))); + let harness = Harness::open(script, []); + wait_until(WAIT, || harness.shown() == text(&["Ada"])).expect("the page"); + let saved = |table: &str| { + let model = harness.tab().imp().model.borrow(); + let model = model.as_ref().expect("the tab is open"); + let state_key = model.state_key.as_ref().expect("a saved connection"); + model.highlight_rules.rules(state_key, None, table) + }; + let rule = HighlightRule::new("name"); + harness.grid().set_highlight_rules(vec![rule.clone()]); + + harness.tab().rename_table("people"); + + assert_eq!(saved("people"), [rule]); + harness.grid().set_highlight_rules(Vec::new()); + assert!( + saved("people").is_empty(), + "a change after the rename was kept under the old name" + ); +} + fn switch_labels(tab: &BrowseTab) -> Vec { use gtk4::subclass::prelude::ObjectSubclassIsExt; crate::test_support::descendants(&tab.imp().view_toggle.get()) diff --git a/linux/crates/app/src/ui/browse_tab/tests/references.rs b/linux/crates/app/src/ui/browse_tab/tests/references.rs index f4969445c7..1f9ddc06dd 100644 --- a/linux/crates/app/src/ui/browse_tab/tests/references.rs +++ b/linux/crates/app/src/ui/browse_tab/tests/references.rs @@ -73,8 +73,8 @@ fn text_columns() -> Vec { fn remembered(harness: &Harness) -> FilterSet { let model = harness.tab().imp().model.borrow(); let model = model.as_ref().expect("the tab is open"); - let connection_id = model.connection_id.expect("a saved connection"); - model.filter_settings.filters(connection_id, None, &model.table) + let state_key = model.state_key.as_ref().expect("a saved connection"); + model.filter_settings.filters(state_key, None, &model.table) } #[gtk4::test] diff --git a/linux/crates/app/src/ui/data_grid/column_chooser.rs b/linux/crates/app/src/ui/data_grid/column_chooser.rs index 4088ff0fa8..acc573e2f8 100644 --- a/linux/crates/app/src/ui/data_grid/column_chooser.rs +++ b/linux/crates/app/src/ui/data_grid/column_chooser.rs @@ -519,7 +519,7 @@ mod tests { let spec = GridSpec { layout: Some(LayoutBinding { store: store.clone(), - connection_id, + state_key: connection_id.into(), schema: Some("public".to_owned()), table: "users".to_owned(), }), diff --git a/linux/crates/app/src/ui/data_grid/highlight.rs b/linux/crates/app/src/ui/data_grid/highlight.rs index 1fca6c305c..5d7cc5542d 100644 --- a/linux/crates/app/src/ui/data_grid/highlight.rs +++ b/linux/crates/app/src/ui/data_grid/highlight.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use std::rc::Rc; +use gtk4::glib; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use tablepro_core::Value; @@ -21,6 +22,14 @@ impl DataGrid { self.emit_by_name::<()>("highlight-rules-changed", &[]); } + pub fn connect_highlight_rules_changed(&self, f: F) -> glib::SignalHandlerId { + self.connect_closure( + "highlight-rules-changed", + false, + glib::closure_local!(move |grid: &Self| f(grid)), + ) + } + pub fn highlight_rules(&self) -> Vec { self.imp().highlight_source.borrow().clone() } diff --git a/linux/crates/app/src/ui/data_grid/layout_binding.rs b/linux/crates/app/src/ui/data_grid/layout_binding.rs index a58e877812..9e4df19aa0 100644 --- a/linux/crates/app/src/ui/data_grid/layout_binding.rs +++ b/linux/crates/app/src/ui/data_grid/layout_binding.rs @@ -1,26 +1,21 @@ -use uuid::Uuid; - -use crate::persistence::ColumnLayoutStore; +use crate::persistence::{ColumnLayoutStore, TableStateKey}; use crate::services::grid::ColumnLayout; -/// Where a table's column layout is kept. The schema is part of it, so -/// `public.users` and `audit.users` keep their own. #[derive(Debug, Clone)] pub struct LayoutBinding { pub store: ColumnLayoutStore, - pub connection_id: Uuid, + pub state_key: TableStateKey, pub schema: Option, pub table: String, } impl LayoutBinding { pub fn load(&self) -> ColumnLayout { - self.store - .layout(self.connection_id, self.schema.as_deref(), &self.table) + self.store.layout(&self.state_key, self.schema.as_deref(), &self.table) } pub fn save(&self, layout: &ColumnLayout) { self.store - .save(self.connection_id, self.schema.as_deref(), &self.table, layout); + .save(&self.state_key, self.schema.as_deref(), &self.table, layout); } } diff --git a/linux/crates/app/src/ui/data_grid/tests/cells.rs b/linux/crates/app/src/ui/data_grid/tests/cells.rs index 52fefbb93d..11348e366f 100644 --- a/linux/crates/app/src/ui/data_grid/tests/cells.rs +++ b/linux/crates/app/src/ui/data_grid/tests/cells.rs @@ -585,6 +585,25 @@ fn highlights_announce_their_colour_and_keep_modified_text_readable() { assert!(has_class(&cell, "warning")); } +/// A host that keeps the rules hears of each change, and only of changes. +#[gtk4::test] +fn the_grid_tells_its_host_when_the_highlight_rules_change() { + use crate::services::grid::HighlightRule; + + let grid = Grid::open(two_text_columns(), two_text_values()); + let heard = Rc::new(Cell::new(0)); + let count = heard.clone(); + grid.grid + .connect_highlight_rules_changed(move |_| count.set(count.get() + 1)); + + grid.grid.set_highlight_rules(vec![HighlightRule::new("name")]); + assert_eq!(heard.get(), 1); + assert_eq!(grid.grid.highlight_rules().len(), 1); + + grid.grid.set_highlight_rules(Vec::new()); + assert_eq!(heard.get(), 2); +} + #[gtk4::test] fn restored_highlights_apply_to_the_first_rows_without_a_refresh() { use crate::services::grid::{HighlightColor, HighlightRule, HighlightTarget}; diff --git a/linux/crates/app/src/ui/data_grid/tests/mod.rs b/linux/crates/app/src/ui/data_grid/tests/mod.rs index ed5d8a536a..f00508e9b3 100644 --- a/linux/crates/app/src/ui/data_grid/tests/mod.rs +++ b/linux/crates/app/src/ui/data_grid/tests/mod.rs @@ -319,7 +319,7 @@ impl Layouts { fn binding(&self, table: &str) -> LayoutBinding { LayoutBinding { store: self.store.clone(), - connection_id: self.connection_id, + state_key: self.connection_id.into(), schema: Some("public".to_owned()), table: table.to_owned(), } diff --git a/linux/crates/app/src/ui/session_window/imp.rs b/linux/crates/app/src/ui/session_window/imp.rs index 2de9293473..8d5d76a60e 100644 --- a/linux/crates/app/src/ui/session_window/imp.rs +++ b/linux/crates/app/src/ui/session_window/imp.rs @@ -35,6 +35,8 @@ pub(super) type FailureBeside = (ConnectSource, ConnectFailure, Vec); #[template(resource = "/app/tablepro/TablePro/ui/session-window.ui")] #[properties(wrapper_type = super::SessionWindow)] pub struct SessionWindow { + pub(super) state_file_watches: RefCell>, + pub(super) state_file_toasts: RefCell>, /// The toasts of the page on screen. Each page has its own, under /// its header bar, so the shell's sit inside the tab overview. pub(super) page_toasts: RefCell>, diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index 0380a7072c..972836dce2 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -31,6 +31,7 @@ mod session_restore; mod session_teardown; mod session_window_command; mod session_window_input; +mod state_file_status; #[cfg(test)] pub(crate) mod tests; mod window_actions; @@ -191,6 +192,7 @@ impl SessionWindow { self.render(); self.show_connection_list_banner(); + self.watch_state_files(); input.deliver(SessionWindowInput::LoadConnections); self.open_launch(launch, &input); } @@ -311,6 +313,7 @@ impl SessionWindow { if let Some(presenter) = imp.prompt_presenter.take() { presenter.abort(); } + self.stop_state_file_watches(); self.release_session(); drop(imp.dialog.take()); if let Some(welcome) = imp.welcome.take() { @@ -500,14 +503,7 @@ impl SessionWindow { SessionWindowInput::CloseCurrent => self.on_close_current(), SessionWindowInput::Reconnect => self.on_reconnect(), SessionWindowInput::ResetWorkspaceLayout => self.on_reset_workspace_layout(), - SessionWindowInput::CloseRequested(CloseReason::Quit(reply)) => { - if let Some(guards) = self.close_guards() { - guards.request_window_close(self, move |decision: CloseDecision| { - // A quit that went away no longer needs the answer. - let _ = reply.send(decision); - }); - } - } + SessionWindowInput::CloseRequested(reason) => self.on_close_requested(reason), SessionWindowInput::DirtyChanged | SessionWindowInput::SavesInFlightChanged => self.refresh_dirty(), SessionWindowInput::WorkCompleted(work) => self.on_work_completed(work), SessionWindowInput::TabsChanged => self.on_tabs_changed(), @@ -537,6 +533,8 @@ impl SessionWindow { SessionWindowInput::ConnectionsFileRead { path, bytes } => self.on_connections_file_read(path, bytes), SessionWindowInput::ImportPreviewed(preview) => self.on_import_previewed(*preview), SessionWindowInput::ImportFromApp => self.on_import_from_app(), + SessionWindowInput::StateFileStatus(kind) => self.show_state_file_status(kind), + SessionWindowInput::RetryStateFile(kind) => self.retry_state_file(kind), SessionWindowInput::ShowToast(message) => self.show_toast(&message), SessionWindowInput::SecretSave(outcome) => self.on_secret_save(outcome), } @@ -549,6 +547,16 @@ impl SessionWindow { } } + fn on_close_requested(&self, reason: CloseReason) { + let CloseReason::Quit(reply) = reason; + if let Some(guards) = self.close_guards() { + guards.request_window_close(self, move |decision: CloseDecision| { + // A quit that went away no longer needs the answer. + let _ = reply.send(decision); + }); + } + } + fn on_reset_workspace_layout(&self) { if let Some(input) = self.workspace().and_then(|workspace| workspace.sender()) { input.deliver(WorkspaceInput::ResetWorkspaceLayout); @@ -1796,12 +1804,24 @@ impl SessionWindow { } } - /// The window closes through one pass of the close prompts over every - /// session: one Save Changes for all of them, and Cancel keeps them - /// all. fn close_guards_for_window(&self) -> Rc { let guards = CloseGuards::new(); let window = self.downgrade(); + guards.set_window_preflight(move || { + let window = window.clone(); + let discarded = Rc::new(std::cell::Cell::new(None)); + Rc::new(move |_| { + let window = window.upgrade(); + let discarded = discarded.clone(); + Box::pin(async move { + match window { + Some(window) => window.confirm_state_files_before_close(&discarded).await, + None => CloseDecision::Approved, + } + }) + }) + }); + let window = self.downgrade(); guards.connect_window( self, move || { diff --git a/linux/crates/app/src/ui/session_window/session_window_input.rs b/linux/crates/app/src/ui/session_window/session_window_input.rs index a0fc4d2f11..81fcbeab54 100644 --- a/linux/crates/app/src/ui/session_window/session_window_input.rs +++ b/linux/crates/app/src/ui/session_window/session_window_input.rs @@ -22,6 +22,8 @@ use crate::workspace::{CloseReason, CompletedWork}; #[derive(Debug)] pub(crate) enum SessionWindowInput { + StateFileStatus(super::state_file_status::StateFileKind), + RetryStateFile(super::state_file_status::StateFileKind), /// Open a saved connection, beside the sessions the window holds. OpenSaved(Box), /// Open a database file, as the file manager or the command line diff --git a/linux/crates/app/src/ui/session_window/state_file_status.rs b/linux/crates/app/src/ui/session_window/state_file_status.rs new file mode 100644 index 0000000000..3fc9ccf29f --- /dev/null +++ b/linux/crates/app/src/ui/session_window/state_file_status.rs @@ -0,0 +1,455 @@ +use std::cell::Cell; + +use crate::workspace::CloseDecision; +use gtk4::glib; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use libadwaita as adw; +use libadwaita::prelude::*; +use tablepro_session::runtime::WatchForwarder; + +use super::{SessionWindow, SessionWindowInput}; +use crate::i18n::gettext; +use crate::persistence::StateFileError; +use crate::runtime::DeliverInput; +use crate::storage::AppStorage; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) enum StateFileKind { + Layouts, + Filters, + Highlights, +} + +impl StateFileKind { + const ALL: [Self; 3] = [Self::Layouts, Self::Filters, Self::Highlights]; + + fn failing(self, storage: &AppStorage) -> tokio::sync::watch::Receiver { + match self { + Self::Layouts => storage.column_layouts().failing(), + Self::Filters => storage.filter_settings().failing(), + Self::Highlights => storage.highlight_rules().failing(), + } + } + + fn error(self, storage: &AppStorage) -> Option { + match self { + Self::Layouts => storage.column_layouts().current_error(), + Self::Filters => storage.filter_settings().current_error(), + Self::Highlights => storage.highlight_rules().current_error(), + } + } + + fn unsaved(self, storage: &AppStorage) -> bool { + match self { + Self::Layouts => storage.column_layouts().has_unsaved_changes(), + Self::Filters => storage.filter_settings().has_unsaved_changes(), + Self::Highlights => storage.highlight_rules().has_unsaved_changes(), + } + } + + fn revision(self, storage: &AppStorage) -> u64 { + match self { + Self::Layouts => storage.column_layouts().revision(), + Self::Filters => storage.filter_settings().revision(), + Self::Highlights => storage.highlight_rules().revision(), + } + } + + fn retry(self, storage: &AppStorage) -> bool { + match self { + Self::Layouts => storage.column_layouts().retry(), + Self::Filters => storage.filter_settings().retry(), + Self::Highlights => storage.highlight_rules().retry(), + } + } + + async fn flush(self, storage: &AppStorage) -> Result<(), StateFileError> { + match self { + Self::Layouts => storage.column_layouts().flush().await, + Self::Filters => storage.filter_settings().flush().await, + Self::Highlights => storage.highlight_rules().flush().await, + } + } + + fn message(self, retryable: bool) -> String { + match (self, retryable) { + (Self::Layouts, true) => gettext("Column layouts could not be saved"), + (Self::Filters, true) => gettext("Table filters could not be saved"), + (Self::Highlights, true) => gettext("Highlight rules could not be saved"), + (Self::Layouts, false) => { + gettext("Saved column layouts are unavailable. Their file has been kept unchanged.") + } + (Self::Filters, false) => { + gettext("Saved table filters are unavailable. Their file has been kept unchanged.") + } + (Self::Highlights, false) => { + gettext("Saved highlight rules are unavailable. Their file has been kept unchanged.") + } + } + } +} + +impl SessionWindow { + pub(super) async fn confirm_state_files_before_close(&self, discarded: &Cell>) -> CloseDecision { + if self.application().is_some_and(|application| { + application + .windows() + .iter() + .any(|window| window != self.upcast_ref::()) + }) { + return CloseDecision::Approved; + } + let Some(state) = self.state() else { + return CloseDecision::Approved; + }; + let storage = state.services.storage.clone(); + let _hold = state.services.hold().acquire(); + loop { + let revisions = StateFileKind::ALL.map(|kind| kind.revision(&storage)); + if discarded.get() == Some(revisions) { + return CloseDecision::Approved; + } + let mut failed = Vec::new(); + for kind in StateFileKind::ALL { + if kind.flush(&storage).await.is_err() && kind.unsaved(&storage) { + failed.push(kind); + } + } + if failed.is_empty() { + if StateFileKind::ALL.into_iter().any(|kind| kind.unsaved(&storage)) { + continue; + } + return CloseDecision::Approved; + } + let revisions = StateFileKind::ALL.map(|kind| kind.revision(&storage)); + let retryable = failed + .iter() + .any(|kind| kind.error(&storage).is_some_and(|error| error.is_retryable())); + let dialog = adw::AlertDialog::builder() + .heading(gettext("Table Settings Could Not Be Saved")) + .body(gettext( + "Closing now will lose changes to highlight rules, filters or column layouts.", + )) + .build(); + dialog.add_responses(&[ + ("cancel", &gettext("Keep Open")), + ("discard", &gettext("Close Without Saving")), + ]); + if retryable { + dialog.add_response("retry", &gettext("Retry")); + dialog.set_response_appearance("retry", adw::ResponseAppearance::Suggested); + } + dialog.set_response_appearance("discard", adw::ResponseAppearance::Destructive); + dialog.set_default_response(Some("cancel")); + dialog.set_close_response("cancel"); + match crate::workspace::dialog_response::respond(&dialog, self.upcast_ref()) + .await + .as_str() + { + "discard" => { + discarded.set(Some(revisions)); + } + "retry" => { + for kind in failed { + if kind.error(&storage).is_some_and(|error| error.is_retryable()) { + kind.retry(&storage); + } + } + } + _ => return CloseDecision::Cancelled, + } + } + } + + pub(super) fn watch_state_files(&self) { + let (Some(state), Some(input)) = (self.state(), self.input()) else { + return; + }; + let watches = StateFileKind::ALL + .into_iter() + .map(|kind| { + let input = input.clone(); + WatchForwarder::new( + kind.failing(&state.services.storage), + move |_| input.deliver(SessionWindowInput::StateFileStatus(kind)), + |future| { + glib::spawn_future_local(future); + }, + ) + }) + .collect(); + self.imp().state_file_watches.replace(watches); + } + + pub(super) fn stop_state_file_watches(&self) { + self.imp().state_file_watches.borrow_mut().clear(); + for (_, toast) in self.imp().state_file_toasts.borrow_mut().drain() { + toast.dismiss(); + } + } + + pub(super) fn show_state_file_status(&self, kind: StateFileKind) { + if let Some(toast) = self.imp().state_file_toasts.borrow_mut().remove(&kind) { + toast.dismiss(); + } + let Some(state) = self.state() else { + return; + }; + let Some(error) = kind.error(&state.services.storage) else { + return; + }; + let retryable = error.is_retryable(); + let toast = crate::ui::plain_toast(&kind.message(retryable)); + toast.set_timeout(0); + if retryable && let Some(input) = self.input() { + toast.set_button_label(Some(&gettext("Retry"))); + toast.connect_button_clicked(move |_| input.deliver(SessionWindowInput::RetryStateFile(kind))); + } + self.imp().state_file_toasts.borrow_mut().insert(kind, toast.clone()); + self.add_toast(toast); + } + + pub(super) fn retry_state_file(&self, kind: StateFileKind) { + let (Some(state), Some(input)) = (self.state(), self.input()) else { + return; + }; + let storage = state.services.storage.clone(); + if !kind.retry(&storage) { + return; + } + let hold = state.services.hold().acquire(); + glib::spawn_future_local(async move { + if let Err(error) = kind.flush(&storage).await { + tracing::warn!(%error, "table settings retry failed"); + } + // A repeated failure leaves the watch unchanged, but still needs its Retry button back. + input.deliver(SessionWindowInput::StateFileStatus(kind)); + drop(hold); + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::application::WindowLaunch; + use crate::services::change_tracker::RowKey; + use crate::services::grid::HighlightRule; + use crate::services::save_batch::SaveTarget; + use crate::services::tracker_edit::TrackerEdit; + use crate::test_support::{drain_main_context, wait_until}; + use crate::ui::session_window::tests::{TestWindow, WAIT}; + use crate::ui::workspace::{OpenMode, Workspace, WorkspaceInput}; + use crate::workspace::dialog_response::{answer, visible_alert}; + use crate::workspace::test_workspace::integer; + use tablepro_core::Value; + use tablepro_core::meta::TableRef; + + const SETTINGS_PROMPT: &str = "Table Settings Could Not Be Saved"; + + fn wait_for_prompt(window: &SessionWindow, heading: &str) { + wait_until(WAIT, || { + visible_alert(window).is_some_and(|alert| alert.heading().as_deref() == Some(heading)) + }) + .expect("the expected close prompt"); + } + + fn fail_rule_write(test: &TestWindow) { + let store = test.services.storage.highlight_rules(); + std::fs::create_dir(store.path()).expect("block file replacement"); + store.set_rules(test.saved.id, None, "orders", vec![HighlightRule::new("status")]); + wait_until(WAIT, || store.current_error().is_some()).expect("the failed write"); + } + + fn edit_table(workspace: &Workspace, name: &str) { + let before = workspace.open_tabs().unwrap_or_default(); + workspace + .sender() + .expect("workspace input") + .deliver(WorkspaceInput::SelectTable { + schema: None, + name: name.to_owned(), + open_mode: OpenMode::NewTab, + }); + let mut opened = None; + wait_until(WAIT, || { + opened = workspace + .open_tabs() + .and_then(|tabs| tabs.into_iter().find(|tab| !before.contains(tab))); + opened.is_some() + }) + .expect("the table tab"); + let tracker = crate::ui::workspace::tests::row_tracker(workspace, opened.expect("opened tab")) + .expect("the table tracker"); + let _ = tracker.set_target(SaveTarget { + table: TableRef::unqualified(name), + columns: vec![integer("id", true), integer("n", false)], + }); + let _ = tracker.apply(TrackerEdit::Cell { + row_key: RowKey::from_pk_values(&[Value::Int(1)]).expect("row key"), + col: 1, + original: Value::Int(1), + value: tablepro_core::edit::CellInput::Value(Value::Int(2)), + }); + } + + fn second_connected_window(test: &TestWindow) -> SessionWindow { + let saved = tablepro_storage::SavedConnection { + id: uuid::Uuid::new_v4(), + name: "Second".to_owned(), + database: test.dir().join("second.db").display().to_string(), + ..test.saved.clone() + }; + test.services + .storage + .connections() + .upsert_blocking(saved.clone()) + .expect("save second connection"); + wait_until(WAIT, || test.services.saved_connections.get(saved.id).is_some()).expect("saved list updated"); + let window = test + .app + .open_window(WindowLaunch::Connect(Box::new(saved))) + .and_then(|window| window.downcast::().ok()) + .expect("second window"); + wait_until(WAIT, || window.page_name() == "workspace").expect("second window connected"); + window + } + + #[gtk4::test] + fn a_failed_rule_write_offers_retry_and_success_dismisses_the_notice() { + let test = TestWindow::picker("HighlightWriteFailure"); + let store = test.services.storage.highlight_rules(); + let path = store.path().to_owned(); + std::fs::create_dir(&path).expect("block file replacement"); + store.set_rules(test.saved.id, None, "orders", vec![HighlightRule::new("status")]); + wait_until(WAIT, || { + test.window + .imp() + .state_file_toasts + .borrow() + .contains_key(&StateFileKind::Highlights) + }) + .expect("write failure notice"); + let toast = test + .window + .imp() + .state_file_toasts + .borrow() + .get(&StateFileKind::Highlights) + .cloned() + .expect("toast"); + assert_eq!(toast.button_label().as_deref(), Some("Retry")); + assert_eq!(test.window.imp().state_file_toasts.borrow().len(), 1); + std::fs::remove_dir(&path).expect("allow writes again"); + toast.emit_by_name::<()>("button-clicked", &[]); + wait_until(WAIT, || { + path.is_file() && test.window.imp().state_file_toasts.borrow().is_empty() + }) + .expect("retry saves and dismisses"); + assert!( + std::fs::read_to_string(&path) + .expect("saved document") + .contains("status") + ); + drain_main_context(); + } + #[gtk4::test] + fn closing_waits_for_a_failed_write_and_can_retry_without_losing_rules() { + let test = TestWindow::picker("HighlightCloseFailure"); + let store = test.services.storage.highlight_rules(); + let path = store.path().to_owned(); + std::fs::create_dir(&path).expect("block write"); + store.set_rules(test.saved.id, None, "orders", vec![HighlightRule::new("status")]); + test.window.close(); + wait_until(WAIT, || { + test.window + .visible_dialog() + .and_downcast::() + .is_some() + }) + .expect("failed close asks before losing state"); + assert!(test.window.is_visible()); + std::fs::remove_dir(&path).expect("allow retry"); + assert!(crate::workspace::dialog_response::answer(&test.window, "retry")); + wait_until(WAIT, || !test.window.is_visible()).expect("close after saved"); + assert!( + std::fs::read_to_string(path) + .expect("saved on close") + .contains("status") + ); + } + + #[gtk4::test] + fn the_last_of_two_pending_window_closes_checks_shared_failed_settings_again() { + let test = TestWindow::connected("TwoWindowSettingsClose"); + let second = second_connected_window(&test); + edit_table(&test.workspace(), "first_edits"); + edit_table(&second.workspace().expect("second workspace"), "second_edits"); + fail_rule_write(&test); + + test.window.close(); + second.close(); + wait_for_prompt(&test.window, "Save Changes?"); + wait_for_prompt(&second, "Save Changes?"); + assert_eq!(test.app.windows().len(), 2); + assert!(answer(&test.window, "discard")); + wait_until(WAIT, || !test.window.is_visible() && test.app.windows().len() == 1) + .expect("the first close completes while the second waits"); + assert!(answer(&second, "discard")); + + wait_for_prompt(&second, SETTINGS_PROMPT); + assert!(second.is_visible()); + assert!(test.services.storage.highlight_rules().has_unsaved_changes()); + assert!(answer(&second, "cancel")); + wait_until(WAIT, || visible_alert(&second).is_none()).expect("keep the last window open"); + assert!(second.is_visible()); + } + + #[gtk4::test] + fn unchanged_settings_discard_is_asked_once_across_workspace_close_prompts() { + let test = TestWindow::connected("SettingsDiscardOnce"); + edit_table(&test.workspace(), "orders"); + fail_rule_write(&test); + + test.window.close(); + wait_for_prompt(&test.window, SETTINGS_PROMPT); + assert!(answer(&test.window, "discard")); + wait_for_prompt(&test.window, "Save Changes?"); + assert!(answer(&test.window, "discard")); + + wait_until(WAIT, || !test.window.is_visible()).expect("unchanged settings consent remains valid"); + assert!(test.services.storage.highlight_rules().has_unsaved_changes()); + } + + #[gtk4::test] + fn settings_changed_during_the_workspace_prompt_need_new_discard_consent() { + let test = TestWindow::connected("SettingsDiscardRevision"); + edit_table(&test.workspace(), "orders"); + fail_rule_write(&test); + + test.window.close(); + wait_for_prompt(&test.window, SETTINGS_PROMPT); + assert!(answer(&test.window, "discard")); + wait_for_prompt(&test.window, "Save Changes?"); + let changed = HighlightRule::new("new_status"); + test.services + .storage + .highlight_rules() + .set_rules(test.saved.id, None, "orders", vec![changed.clone()]); + assert!(answer(&test.window, "discard")); + + wait_for_prompt(&test.window, SETTINGS_PROMPT); + assert!(test.window.is_visible()); + assert_eq!( + test.services + .storage + .highlight_rules() + .rules(test.saved.id, None, "orders"), + [changed] + ); + assert!(answer(&test.window, "cancel")); + wait_until(WAIT, || visible_alert(&test.window).is_none()).expect("keep the changed settings open"); + assert!(test.window.is_visible()); + } +} diff --git a/linux/crates/app/src/ui/workspace/close_flow.rs b/linux/crates/app/src/ui/workspace/close_flow.rs index 2257f256c2..6e0ecc2962 100644 --- a/linux/crates/app/src/ui/workspace/close_flow.rs +++ b/linux/crates/app/src/ui/workspace/close_flow.rs @@ -41,6 +41,7 @@ impl WorkspaceModel { let hold = self.context.hold.acquire(); let column_layouts = self.storage.column_layouts().flush(); let filter_settings = self.storage.filter_settings().flush(); + let highlight_rules = self.storage.highlight_rules().flush(); // The key is free once the window lets go of it, which is before // these land; a delete of the connection waits for them. let drafts_landing = self.storage.workspaces().writes_in_flight(&self.key); @@ -48,8 +49,11 @@ impl WorkspaceModel { let writer = self.drafts.clone(); let tabs = self.workspace.detach(); glib::spawn_future_local(async move { - column_layouts.await; - filter_settings.await; + for result in [column_layouts.await, filter_settings.await, highlight_rules.await] { + if let Err(error) = result { + tracing::warn!(%error, "table settings were not saved"); + } + } for failure in editor_drafts.await { tracing::warn!(failure, "an editor draft was not saved"); } diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index df3e160158..4b423600cf 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -7,7 +7,6 @@ use gtk4::{gio, glib}; use libadwaita as adw; use libadwaita::prelude::*; use tablepro_core::meta::TableRef; -use tablepro_session::SessionKey; use tablepro_storage::{DraftId, DraftScope}; use uuid::Uuid; @@ -242,14 +241,7 @@ impl TabFactory { tracker: parts.change_trackers.register(id), schema: schema.clone(), table: name.clone(), - // Column widths and filters are kept per connection and table; - // another database's table of the same name is not that one. - connection_id: match parts.label.key() { - SessionKey::SavedIn { .. } => None, - SessionKey::Saved(_) | SessionKey::File(_) => { - parts.context.active.metadata().map(|metadata| metadata.id) - } - }, + state_key: Some(crate::persistence::TableStateKey::for_session(parts.label.key())), read_only: parts.read_only, page_size: page.page_size, initial_offset: page.offset, @@ -257,6 +249,7 @@ impl TabFactory { filter, column_layouts: parts.storage.column_layouts().clone(), filter_settings: parts.storage.filter_settings().clone(), + highlight_rules: parts.storage.highlight_rules().clone(), session, tasks: parts.context.tasks.clone(), deadlines: parts.context.deadlines.clone(), diff --git a/linux/crates/app/src/workspace/close_guards.rs b/linux/crates/app/src/workspace/close_guards.rs index 2821fea983..8941b0bee0 100644 --- a/linux/crates/app/src/workspace/close_guards.rs +++ b/linux/crates/app/src/workspace/close_guards.rs @@ -2,6 +2,8 @@ use std::cell::{Cell, RefCell}; use std::collections::HashSet; use std::rc::Rc; +use futures::FutureExt; +use futures::future::LocalBoxFuture; use gtk4 as gtk; use gtk4::glib; use gtk4::prelude::*; @@ -9,33 +11,23 @@ use uuid::Uuid; use super::{CloseDecision, CloseGuard, CloseScope, CloseStep, WorkspaceHandle}; -/// Hears how the window's close ended. type CloseWaiter = Box; +pub(crate) type WindowPreflight = Rc LocalBoxFuture<'static, CloseDecision>>; +type WindowHandles = Rc Vec>; -/// Where the window's own close stands. #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum WindowClose { Open, - /// The prompts are deciding. Asking, - /// Nothing is left to lose, so the next close request goes through. Approved, } -/// A window's close prompts. One runs at a time, so a tab's close and -/// the window's never ask about the same tab at once: a second waits for -/// the first, then reads the workspace as the first left it. pub(crate) struct CloseGuards { turn: futures::lock::Mutex<()>, window: Cell, - /// Close requests that reached the window's handler, so a caller can - /// tell when a dialog over the window took one instead. requests: Cell, - /// Callers waiting to hear how the window's close ended. waiting: RefCell>, - /// Open transactions the user agreed to lose in the current - /// disconnect or switch. A switch asks again once its connection is - /// ready, about what changed while it connected, but not about these. + preflight: RefCell WindowPreflight>>>, teardown_acknowledged: RefCell>, } @@ -46,10 +38,15 @@ impl CloseGuards { window: Cell::new(WindowClose::Open), requests: Cell::new(0), waiting: RefCell::new(Vec::new()), + preflight: RefCell::new(None), teardown_acknowledged: RefCell::new(HashSet::new()), }) } + pub(crate) fn set_window_preflight(&self, preflight: impl Fn() -> WindowPreflight + 'static) { + self.preflight.replace(Some(Rc::new(preflight))); + } + async fn run( &self, parent: >k::Widget, @@ -61,12 +58,6 @@ impl CloseGuards { CloseGuard::run(parent, handles, scope, acknowledged).await } - /// The window closes only once its prompts approve; `on_close` runs - /// as it does. A close request while they decide is dropped, since - /// their dialog is already on screen. `handles` gives the workspace - /// of each session the window holds at the time, all asked in one - /// pass, so nothing is saved or discarded until every one agreed, and - /// a window with none closes at once. pub(crate) fn connect_window>( self: &Rc, window: &W, @@ -74,9 +65,10 @@ impl CloseGuards { on_close: impl Fn(&W) + 'static, ) { let guards = self.clone(); + let handles: WindowHandles = Rc::new(handles); window.connect_close_request(move |window| { guards.requests.set(guards.requests.get() + 1); - if !guards.window_may_close(window.upcast_ref(), handles()) { + if !guards.window_may_close(window.upcast_ref(), handles.clone()) { return glib::Propagation::Stop; } on_close(window); @@ -85,8 +77,6 @@ impl CloseGuards { }); } - /// Close the window through its prompts and tell `decided` how it - /// ended. A prompt already on screen answers for this request too. pub(crate) fn request_window_close( &self, window: &impl IsA, @@ -112,25 +102,68 @@ impl CloseGuards { } } - fn window_may_close(self: &Rc, window: >k::Window, handles: Vec) -> bool { + fn window_may_close(self: &Rc, window: >k::Window, handles: WindowHandles) -> bool { match self.window.get() { WindowClose::Asking => return false, WindowClose::Approved => return true, WindowClose::Open => {} } - if handles - .iter() - .all(|handle| handle.close_step(CloseScope::Window, &HashSet::new()) == CloseStep::Approve) + let preflight = self.preflight.borrow().clone(); + if preflight.is_none() + && handles() + .iter() + .all(|handle| handle.close_step(CloseScope::Window, &HashSet::new()) == CloseStep::Approve) { return true; } self.window.set(WindowClose::Asking); + let (guards, parent) = (self.clone(), window.clone()); + let mut deciding = Box::pin(async move { + let _turn = guards.turn.lock().await; + let preflight = preflight.map(|make| make()); + let mut acknowledged = HashSet::new(); + let ready = match &preflight { + Some(preflight) => preflight(&parent).await, + None => CloseDecision::Approved, + }; + if ready == CloseDecision::Cancelled { + return CloseDecision::Cancelled; + } + loop { + if CloseGuard::run(parent.upcast_ref(), &handles(), CloseScope::Window, &mut acknowledged).await + == CloseDecision::Cancelled + { + return CloseDecision::Cancelled; + } + if let Some(preflight) = &preflight + && preflight(&parent).await == CloseDecision::Cancelled + { + return CloseDecision::Cancelled; + } + if handles() + .iter() + .all(|handle| handle.close_step(CloseScope::Window, &acknowledged) == CloseStep::Approve) + { + return CloseDecision::Approved; + } + } + }); + // An already approved close must release restore claims before queued lookups can run. + match deciding.as_mut().now_or_never() { + Some(CloseDecision::Approved) => { + self.window.set(WindowClose::Approved); + return true; + } + Some(CloseDecision::Cancelled) => { + self.window.set(WindowClose::Open); + self.settle(CloseDecision::Cancelled); + return false; + } + None => {} + } let (guards, window) = (self.clone(), window.clone()); glib::spawn_future_local(async move { - let decision = guards - .run(window.upcast_ref(), &handles, CloseScope::Window, &mut HashSet::new()) - .await; - match decision { + match deciding.await { CloseDecision::Approved => { guards.window.set(WindowClose::Approved); window.close(); @@ -169,13 +202,10 @@ impl CloseGuards { }); } - /// A new disconnect or switch asks about every open transaction. pub(crate) fn begin_teardown(&self) { self.teardown_acknowledged.borrow_mut().clear(); } - /// Whether every tab of `handles` may go with their connection - /// without asking. pub(crate) fn may_tear_down(&self, handles: &[WorkspaceHandle]) -> bool { let acknowledged = self.teardown_acknowledged.borrow(); handles @@ -183,8 +213,6 @@ impl CloseGuards { .all(|handle| handle.close_step(CloseScope::Window, &acknowledged) == CloseStep::Approve) } - /// Ask whether every tab may go with the connection; `decided` hears - /// the answer. pub(crate) fn ask_tear_down( self: &Rc, parent: >k::Widget, @@ -228,8 +256,6 @@ mod tests { OwnedWindow::presented(adw::Window::new()) } - /// A window whose close goes through `guards`, and how many times it - /// closed. fn guarded_window(guards: &Rc, workspace: &Workspace) -> (OwnedWindow, Rc>) { let window = window(); let closes = Rc::new(Cell::new(0)); @@ -243,8 +269,6 @@ mod tests { (window, closes) } - /// Wait for an alert with `heading` other than `seen`, and answer it - /// with `response`. Returns the alert answered. fn answer_next( window: &adw::Window, heading: &str, @@ -290,8 +314,6 @@ mod tests { assert!(visible_alert(&window).is_none()); } - /// A window of two sessions asks once about both, and saves or - /// discards nothing until that answer. #[gtk4::test] fn one_save_changes_covers_every_workspace_and_cancel_keeps_them_all() { let (first, second) = ( @@ -342,8 +364,270 @@ mod tests { assert!(visible_alert(&window).is_none()); } - /// libadwaita takes a close request over a dialog as dismissing the - /// dialog, which keeps the window open. + #[gtk4::test] + fn an_immediately_ready_preflight_closes_without_yielding() { + let guards = CloseGuards::new(); + let window = window(); + let closes = Rc::new(Cell::new(0)); + let sink = closes.clone(); + guards.connect_window(&*window, Vec::new, move |_| sink.set(sink.get() + 1)); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + guards.set_window_preflight(move || { + let observed = observed.clone(); + Rc::new(move |_| { + observed.set(observed.get() + 1); + Box::pin(std::future::ready(CloseDecision::Approved)) + }) + }); + let (decision, sink) = decision_sink(); + + guards.request_window_close(&*window, sink); + + assert_eq!(decision.get(), Some(CloseDecision::Approved)); + assert_eq!(closes.get(), 1); + assert_eq!(calls.get(), 2); + assert!(!window.is_visible()); + } + + #[gtk4::test] + fn a_preflight_holds_an_empty_window_and_joins_close_requests() { + let guards = CloseGuards::new(); + let window = window(); + let closes = Rc::new(Cell::new(0)); + let sink = closes.clone(); + guards.connect_window(&*window, Vec::new, move |_| sink.set(sink.get() + 1)); + let (send, receive) = futures::channel::oneshot::channel(); + let receive = Rc::new(RefCell::new(Some(receive))); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + let attempts = Rc::new(Cell::new(0)); + let made = attempts.clone(); + guards.set_window_preflight(move || { + made.set(made.get() + 1); + let (observed, receive) = (observed.clone(), receive.clone()); + Rc::new(move |_| { + observed.set(observed.get() + 1); + let receive = receive.borrow_mut().take(); + Box::pin(async move { + match receive { + Some(receive) => receive.await.expect("preflight answer"), + None => CloseDecision::Approved, + } + }) + }) + }); + let (first, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + let (second, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + drain_main_context(); + assert_eq!(calls.get(), 1); + assert_eq!(closes.get(), 0); + assert_eq!(first.get(), None); + assert_eq!(second.get(), None); + assert!(window.is_visible()); + + send.send(CloseDecision::Approved).expect("waiting preflight"); + wait_until(WAIT, || closes.get() == 1).expect("approved close"); + assert_eq!(first.get(), Some(CloseDecision::Approved)); + assert_eq!(second.get(), Some(CloseDecision::Approved)); + assert_eq!(calls.get(), 2); + assert_eq!(attempts.get(), 1); + } + + #[gtk4::test] + fn a_cancelled_preflight_keeps_the_window_and_can_be_asked_again() { + let guards = CloseGuards::new(); + let window = window(); + let closes = Rc::new(Cell::new(0)); + let sink = closes.clone(); + guards.connect_window(&*window, Vec::new, move |_| sink.set(sink.get() + 1)); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + guards.set_window_preflight(move || { + let observed = observed.clone(); + Rc::new(move |_| { + let call = observed.get(); + observed.set(call + 1); + Box::pin(std::future::ready(if call == 0 { + CloseDecision::Cancelled + } else { + CloseDecision::Approved + })) + }) + }); + let (cancelled, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + wait_until(WAIT, || cancelled.get().is_some()).expect("cancelled close"); + assert_eq!(cancelled.get(), Some(CloseDecision::Cancelled)); + assert_eq!(guards.window.get(), WindowClose::Open); + assert_eq!(closes.get(), 0); + assert!(window.is_visible()); + + let (approved, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + wait_until(WAIT, || approved.get().is_some()).expect("second close"); + assert_eq!(approved.get(), Some(CloseDecision::Approved)); + assert_eq!(calls.get(), 3); + assert_eq!(closes.get(), 1); + } + + #[gtk4::test] + fn a_cancelled_preflight_does_not_discard_workspace_changes() { + let workspace = Workspace::open(ConnectionScript::default()); + let guards = CloseGuards::new(); + let (window, closes) = guarded_window(&guards, &workspace); + guards.set_window_preflight(|| Rc::new(|_| Box::pin(std::future::ready(CloseDecision::Cancelled)))); + let (decision, sink) = decision_sink(); + + guards.request_window_close(&*window, sink); + wait_until(WAIT, || decision.get().is_some()).expect("preflight decision"); + + assert_eq!(decision.get(), Some(CloseDecision::Cancelled)); + assert_eq!(closes.get(), 0); + assert_eq!(workspace.tracker.pending_state(), PendingState::Unsaved); + assert!(visible_alert(&window).is_none()); + } + + #[gtk4::test] + fn workspaces_added_while_preflight_waits_are_asked_before_close() { + let workspace = Workspace::open(ConnectionScript::default()); + let guards = CloseGuards::new(); + let window = window(); + let handles = Rc::new(RefCell::new(Vec::new())); + let current = handles.clone(); + let closes = Rc::new(Cell::new(0)); + let sink = closes.clone(); + guards.connect_window( + &*window, + move || current.borrow().clone(), + move |_| sink.set(sink.get() + 1), + ); + let (send, receive) = futures::channel::oneshot::channel(); + let receive = Rc::new(RefCell::new(Some(receive))); + guards.set_window_preflight(move || { + let receive = receive.clone(); + Rc::new(move |_| { + let receive = receive.borrow_mut().take().expect("one preflight"); + Box::pin(async move { receive.await.expect("preflight answer") }) + }) + }); + window.close(); + drain_main_context(); + handles.borrow_mut().push(workspace.handle.clone()); + send.send(CloseDecision::Approved).expect("waiting preflight"); + + answer_next(&window, "Save Changes?", "cancel", None); + wait_until(WAIT, || guards.window.get() == WindowClose::Open).expect("cancelled close"); + assert_eq!(closes.get(), 0); + assert_eq!(workspace.tracker.pending_state(), PendingState::Unsaved); + } + + #[gtk4::test] + fn a_final_preflight_holds_the_close_and_cancellation_allows_a_new_attempt() { + let workspace = Workspace::open(ConnectionScript::default()); + let guards = CloseGuards::new(); + let (window, closes) = guarded_window(&guards, &workspace); + let (send, receive) = futures::channel::oneshot::channel(); + let receive = Rc::new(RefCell::new(Some(receive))); + let attempts = Rc::new(Cell::new(0)); + let made = attempts.clone(); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + guards.set_window_preflight(move || { + made.set(made.get() + 1); + let (observed, receive) = (observed.clone(), receive.clone()); + Rc::new(move |_| { + let call = observed.get() + 1; + observed.set(call); + let receive = (call == 2).then(|| receive.borrow_mut().take().expect("final gate")); + Box::pin(async move { + match receive { + Some(receive) => receive.await.expect("preflight answer"), + None => CloseDecision::Approved, + } + }) + }) + }); + let (first, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + answer_next(&window, "Save Changes?", "discard", None); + wait_until(WAIT, || calls.get() == 2).expect("final preflight"); + let (second, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + assert_eq!(closes.get(), 0); + assert_eq!(first.get(), None); + assert_eq!(second.get(), None); + assert_eq!(attempts.get(), 1); + + send.send(CloseDecision::Cancelled).expect("waiting final gate"); + wait_until(WAIT, || first.get().is_some()).expect("cancelled close"); + assert_eq!(first.get(), Some(CloseDecision::Cancelled)); + assert_eq!(second.get(), Some(CloseDecision::Cancelled)); + assert_eq!(guards.window.get(), WindowClose::Open); + assert!(window.is_visible()); + assert_eq!(closes.get(), 0); + + let (approved, sink) = decision_sink(); + guards.request_window_close(&*window, sink); + wait_until(WAIT, || approved.get().is_some()).expect("new close attempt"); + assert_eq!(approved.get(), Some(CloseDecision::Approved)); + assert_eq!(attempts.get(), 2); + assert_eq!(calls.get(), 4); + assert_eq!(closes.get(), 1); + } + + #[gtk4::test] + fn edits_and_sessions_added_during_the_final_preflight_are_asked_without_repeating_transactions() { + let workspace = Workspace::open(ConnectionScript::default()); + workspace.open_transaction_tab(); + let added = Workspace::open(ConnectionScript::default()); + let guards = CloseGuards::new(); + let window = window(); + let handles = Rc::new(RefCell::new(vec![workspace.handle.clone()])); + let current = handles.clone(); + let closes = Rc::new(Cell::new(0)); + let sink = closes.clone(); + guards.connect_window( + &*window, + move || current.borrow().clone(), + move |_| sink.set(sink.get() + 1), + ); + let (send, receive) = futures::channel::oneshot::channel(); + let receive = Rc::new(RefCell::new(Some(receive))); + let calls = Rc::new(Cell::new(0)); + let observed = calls.clone(); + guards.set_window_preflight(move || { + let (observed, receive) = (observed.clone(), receive.clone()); + Rc::new(move |_| { + let call = observed.get() + 1; + observed.set(call); + let receive = (call == 2).then(|| receive.borrow_mut().take().expect("final gate")); + Box::pin(async move { + match receive { + Some(receive) => receive.await.expect("preflight answer"), + None => CloseDecision::Approved, + } + }) + }) + }); + window.close(); + answer_next(&window, "Close With Open Transactions?", "close", None); + let first = answer_next(&window, "Save Changes?", "discard", None); + wait_until(WAIT, || calls.get() == 2).expect("final preflight"); + edit_again(&workspace); + handles.borrow_mut().push(added.handle.clone()); + send.send(CloseDecision::Approved).expect("waiting final gate"); + + let second = answer_next(&window, "Save Changes?", "discard", Some(&first)); + assert_eq!(second.response_label("discard").as_str(), "_Discard All"); + wait_until(WAIT, || closes.get() == 1).expect("close after fresh workspace prompts"); + assert_eq!(calls.get(), 3); + assert_eq!(workspace.tracker.pending_state(), PendingState::Clean); + assert_eq!(added.tracker.pending_state(), PendingState::Clean); + } + #[gtk4::test] fn a_close_request_over_the_prompt_keeps_the_window() { let workspace = Workspace::open(ConnectionScript::default()); @@ -416,8 +700,6 @@ mod tests { assert_eq!(closes.get(), 1); } - /// A request that arrives while the user's own close is asking is - /// answered by that prompt, rather than dismissing it. #[gtk4::test] fn a_request_during_the_prompt_waits_for_its_answer() { let workspace = Workspace::open(ConnectionScript::default()); @@ -481,8 +763,6 @@ mod tests { assert!(guards.may_tear_down(std::slice::from_ref(&workspace.handle))); } - /// A switch asks once before it connects and again before the new - /// connection replaces the tabs, about what changed in between. #[gtk4::test] fn a_switch_asks_again_only_about_what_changed_while_connecting() { let workspace = Workspace::open(ConnectionScript::default()); diff --git a/linux/crates/app/src/workspace/workspace_file_name.rs b/linux/crates/app/src/workspace/workspace_file_name.rs index c0d37a4ae4..f79671a780 100644 --- a/linux/crates/app/src/workspace/workspace_file_name.rs +++ b/linux/crates/app/src/workspace/workspace_file_name.rs @@ -39,6 +39,10 @@ impl WorkspaceFileName { format!("{id}-database-") } + pub fn storage_key(&self) -> &str { + &self.stem + } + pub fn file_name(&self) -> String { format!("{}.json", self.stem) } diff --git a/linux/docs/storage.md b/linux/docs/storage.md index f190f695be..56e6f732c2 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -12,7 +12,7 @@ The app's data goes to the store its shape calls for. The core's `storage` crate | Preferences and window geometry | `gio::Settings` | `AppSettings` | | Query history, saved queries with their folders and keywords, and favourite tables and databases | SQLite, one database ([core ADR 0017](../../core/docs/decisions/0017-favourites.md)) | `QueryHistory`, `SavedQueries`, `SavedQueryFolders`, `Favourites` | | Editor tab text | one file per tab | `DraftStore` | -| Open tabs, open sessions, column layouts, filters | JSON files the app crate writes | `WorkspaceStore`, `OpenSessions`, `crates/app/src/persistence/` | +| Open tabs, open sessions, column layouts, filters, highlight rules | JSON files the app crate writes | `WorkspaceStore`, `OpenSessions`, `crates/app/src/persistence/` | [Where files go](#where-files-go) has the paths. @@ -108,7 +108,7 @@ leaves nothing unreachable behind when a step fails: entry stays: an entry with no secrets only asks for them again, while a secret with no entry is orphaned in the keyring. 2. The entry in `connections.json`. -3. Everything kept for it: column layouts, filters, the saved tabs with any +3. Everything kept for it: column layouts, filters, highlight rules, the saved tabs with any copy Reset moved aside or an upgrade kept, the editor drafts (`WorkspaceStore::purge`), the saved queries, and the query history when the user turned that on. A @@ -137,6 +137,7 @@ nothing reaches outside it. | `connections_file()` | `$XDG_CONFIG_HOME//connections.json` | | `column_layouts_file()` | `$XDG_CONFIG_HOME//column_layouts.json`: each table's column layout (order, hidden columns, widths the user set) | | `filter_settings_file()` | `$XDG_CONFIG_HOME//filter_settings.json` | +| `highlight_rules_file()` | `$XDG_CONFIG_HOME//highlight_rules.json`: ordered rules for each table | | `history_database()` | `$XDG_STATE_HOME//history.db` | | `workspaces_dir()` | `$XDG_STATE_HOME//workspaces`: one file per session, and `open-sessions.json`: the sessions open at the last quit as `windows`, one list of keys per window, windows in the order of their first recorded session and keys in the order each window adopted them | | `drafts_dir()` | `$XDG_DATA_HOME//drafts` | @@ -404,7 +405,15 @@ Three points worth knowing: Preference rows bind straight to the schema with `settings.gio().bind(key, &row, prop)`, so a change reaches every open view through `changed::` without a save step. -High-churn keyed data stays in files: column layouts, per-table filters and workspace tabs are unbounded maps, which is not what GSettings is for. +High-churn keyed data stays in files: column layouts, per-table filters, highlight rules and workspace tabs are unbounded maps, which is not what GSettings is for. + +## Table state + +Column layouts, filters and highlight rules use the session identity from `WorkspaceFileName`: a saved connection UUID, that UUID plus a digest of the sibling database name, or a digest of the canonical file URI. Existing saved UUID keys keep their spelling. A schema and table name complete the key. Opening a file again uses the same key; tables with the same name in different databases stay separate. Deleting a saved connection also removes its sibling database keys. + +`StateFile` holds these small versioned documents in memory and writes the latest snapshot through the core's `LatestWinsWriter` and `write_document_blocking`. `flush` waits for a write attempt and returns its failure. A failed write keeps the latest state in memory. Each window shows a notice with Retry, which writes that latest state again. A successful write dismisses the notice. Closing the last window waits until every pending settings edit is saved. It checks again after workspace prompts, so concurrent window closes cannot skip the check. If local changes cannot be saved, it offers Retry, Keep Open or Close Without Saving. Consent to close without saving applies only to the settings revision shown. Newer or unreadable files stay protected and offer no write retry. An untouched protected file does not prevent closing. + +Highlight rules are restored before the table grid starts reporting edits. A query result has no table identity, so its rules live only until the next run. The grid behavior is in `../ARCHITECTURE.md`, Highlight rules. ## Errors @@ -452,8 +461,8 @@ that replaces it with a newer format ([core ADR 0012](../../core/docs/decisions/ - Every write of the connection list first takes the connections it no longer has out of each copy of `connections.json`, and deletes a copy it cannot read that way; a failed trim fails the write, so the next one tries again. Deleting - a connection deletes the copies of the column layout and filter files, which - name its tables and filter values. + a connection deletes the copies of the column layout, filter and highlight rule files, which + name its tables and values. - A copy that cannot be made fails the write or the open, and the file is left as it was. The error names the copy's path. - Reading never makes a copy. Nothing restores one on its own: diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index fc4d7a404a..f11592eb4f 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -39,8 +39,11 @@ crates/app/src/persistence/column_layout_store.rs crates/app/src/persistence/column_layouts_document.rs crates/app/src/persistence/filter_settings_document.rs crates/app/src/persistence/filter_settings_store.rs +crates/app/src/persistence/highlight_rules_document.rs +crates/app/src/persistence/highlight_rules_store.rs crates/app/src/persistence/mod.rs crates/app/src/persistence/state_file.rs +crates/app/src/persistence/table_state_key.rs crates/app/src/runtime/app_runtime.rs crates/app/src/runtime/clock_sample.rs crates/app/src/runtime/clock_source.rs @@ -559,6 +562,7 @@ crates/app/src/ui/session_window/session_restore.rs crates/app/src/ui/session_window/session_teardown.rs crates/app/src/ui/session_window/session_window_command.rs crates/app/src/ui/session_window/session_window_input.rs +crates/app/src/ui/session_window/state_file_status.rs crates/app/src/ui/session_window/tests/mod.rs crates/app/src/ui/session_window/tests/notification.rs crates/app/src/ui/session_window/tests/open_file.rs