diff --git a/Cargo.lock b/Cargo.lock index 97916c2ace..e1b9b55dd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8149,6 +8149,7 @@ dependencies = [ "tempfile", "thiserror 2.0.20", "tokio", + "tracing", "unicode-normalization", "uuid", ] diff --git a/core/ARCHITECTURE.md b/core/ARCHITECTURE.md index 4d1731ca24..c5d67fbddb 100644 --- a/core/ARCHITECTURE.md +++ b/core/ARCHITECTURE.md @@ -111,7 +111,7 @@ Each boundary error has a stable `code()`, such as `driver.timeout` or `ssh.host `ffi` is the library an app outside Rust loads: the Windows app as a DLL, the Apple apps as a static library in an XCFramework, built without the `engines` feature by `apple/scripts/build-core.sh`. Core ADRs 0002, on the bindings, 0003, on result windows, and 0009, on the macOS slices, hold the reasons. -- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), reports and waits on the session's health, and opens a `QueryEditor`. `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. +- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, and opens a `QueryEditor`. `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. - The grid reads a `ResultSet` through the C ABI in `src/window_abi.rs`: `tablepro_cursor_fetch` packs the display text of one visible range, and `tablepro_cursor_free` ends the cursor. - The library owns one tokio runtime. Each async export runs on it and is aborted when the foreign side drops the call; a `CancelToken` stops it sooner, since UniFFI cannot cancel. - A result set takes rows as the driver reads them. Each column keeps a kind byte, a raw `i64` and the exact text `tablepro_core::export::value_text` writes, or a binary value's bytes, in chunks of 65,536 rows. A window only cuts and copies. diff --git a/core/crates/drivers/clickhouse/src/call_client.rs b/core/crates/drivers/clickhouse/src/call_client.rs index 5ef1b7518f..6589595f0f 100644 --- a/core/crates/drivers/clickhouse/src/call_client.rs +++ b/core/crates/drivers/clickhouse/src/call_client.rs @@ -55,25 +55,6 @@ impl CallClient { } } -/// A read as the server runs it: never a write, and stopped by the -/// server itself once its bound passes. `None` leaves it unbounded. -/// `readonly` is the level `ClickHouseEndpoint::read_level` chose; none -/// leaves the login's own. -pub(crate) fn reading( - client: &clickhouse::Client, - bound: Option, - readonly: Option<&str>, -) -> clickhouse::Client { - let client = client - .clone() - .with_setting("timeout_overflow_mode", "throw") - .with_setting("max_execution_time", whole_seconds(bound).to_string()); - match readonly { - Some(level) => client.with_setting("readonly", level), - None => client, - } -} - /// The nearer of the call's deadline and the configured timeout. pub(crate) fn read_bound(deadline: Option, configured: Option) -> Option { let remaining = deadline.map(|deadline| deadline.saturating_duration_since(Instant::now())); diff --git a/core/crates/drivers/clickhouse/src/cell_seed.rs b/core/crates/drivers/clickhouse/src/cell_seed.rs index a3d88276ae..afaacdca62 100644 --- a/core/crates/drivers/clickhouse/src/cell_seed.rs +++ b/core/crates/drivers/clickhouse/src/cell_seed.rs @@ -21,7 +21,9 @@ pub(crate) enum CellPlan { Integer, Float32, Float64, - Decimal, + Decimal { + scale: u32, + }, Bool, Date, DateTime, @@ -58,7 +60,9 @@ impl CellPlan { | DataTypeNode::Int256 => Self::Integer, DataTypeNode::Float32 | DataTypeNode::BFloat16 => Self::Float32, DataTypeNode::Float64 => Self::Float64, - DataTypeNode::Decimal(..) => Self::Decimal, + DataTypeNode::Decimal(_, scale, _) => Self::Decimal { + scale: u32::from(*scale), + }, DataTypeNode::Bool => Self::Bool, DataTypeNode::Date | DataTypeNode::Date32 => Self::Date, DataTypeNode::DateTime(_) | DataTypeNode::DateTime64(..) => Self::DateTime, @@ -103,7 +107,7 @@ impl CellPlan { Self::Integer => "Integer".to_owned(), Self::Float32 => "Float32".to_owned(), Self::Float64 => "Float64".to_owned(), - Self::Decimal => "Decimal".to_owned(), + Self::Decimal { .. } => "Decimal".to_owned(), Self::Bool => "Bool".to_owned(), Self::Date => "Date".to_owned(), Self::DateTime => "DateTime".to_owned(), @@ -153,7 +157,7 @@ impl<'de> DeserializeSeed<'de> for CellSeed<'_> { let text = Option::::deserialize(deserializer)?; Ok(text.map_or(Value::Null, |text| temporal_or_uuid(plan, &text))) } - CellPlan::Integer | CellPlan::Float32 | CellPlan::Float64 | CellPlan::Decimal | CellPlan::Bool => { + CellPlan::Integer | CellPlan::Float32 | CellPlan::Float64 | CellPlan::Decimal { .. } | CellPlan::Bool => { let raw = <&RawValue>::deserialize(deserializer)?; Ok(number(plan, raw.get())) } @@ -178,8 +182,13 @@ fn undecodable(plan: &CellPlan, reason: UndecodableReason) -> Value { /// A number token, quoted or bare: the server quotes 64-bit and wider /// integers, and the non-finite floats, so that JSON readers keep them. fn number(plan: &CellPlan, token: &str) -> Value { + // A Nullable column takes its null before this, so a float's null is a NaN or an infinity, which a login that may + // not set `output_format_json_quote_denormals` gets as null. if token == "null" { - return Value::Null; + return match plan { + CellPlan::Float32 | CellPlan::Float64 => undecodable(plan, UndecodableReason::OutOfRange), + _ => Value::Null, + }; } let text = token .strip_prefix('"') @@ -189,7 +198,9 @@ fn number(plan: &CellPlan, token: &str) -> Value { CellPlan::Integer => integer(text), CellPlan::Float32 => f32::from_str(text).ok().map(Value::Float32), CellPlan::Float64 => f64::from_str(text).ok().map(Value::Float64), - CellPlan::Decimal => SqlDecimal::from_str(text).ok().map(Value::Decimal), + CellPlan::Decimal { scale } => SqlDecimal::from_str(text) + .ok() + .map(|decimal| Value::Decimal(at_scale(decimal, *scale))), CellPlan::Bool => match text { "true" | "1" => Some(Value::Bool(true)), "false" | "0" => Some(Value::Bool(false)), @@ -200,6 +211,19 @@ fn number(plan: &CellPlan, token: &str) -> Value { parsed.unwrap_or_else(|| undecodable(plan, UndecodableReason::OutOfRange)) } +// A login that may not set `output_format_decimal_trailing_zeros` gets `1.5` for a `Decimal(10, 2)` holding 1.50, which +// is not equal to the 1.50 an edit of it parses. +fn at_scale(decimal: SqlDecimal, scale: u32) -> SqlDecimal { + let padded = match decimal.parts() { + Some((negative, coefficient, written)) if written < scale => { + let zeros = "0".repeat(usize::try_from(scale - written).unwrap_or(0)); + SqlDecimal::from_parts(negative, &format!("{coefficient}{zeros}"), scale).ok() + } + _ => None, + }; + padded.unwrap_or(decimal) +} + /// An integer of any width, in the smallest variant that holds it, the /// way an edit parses one, so a value read back compares equal to the /// same value typed in. @@ -377,11 +401,45 @@ mod tests { assert_eq!(wide.to_string(), widest); let decimal = "12345678901234567890123456789012345678901234567890123456.12345678901234567890"; assert_eq!( - read(&CellPlan::Decimal, decimal.as_bytes()), + read(&CellPlan::Decimal { scale: 20 }, decimal.as_bytes()), Value::Decimal(decimal.parse().expect("a decimal")) ); } + #[test] + fn a_decimal_written_without_its_trailing_zeros_reads_at_its_columns_scale() { + let plan = CellPlan::Decimal { scale: 2 }; + let at = |text: &str| Value::Decimal(text.parse().expect("a decimal")); + + assert_eq!(read(&plan, b"1.5"), at("1.50")); + assert_eq!(read(&plan, b"-0.1"), at("-0.10")); + assert_eq!(read(&plan, b"7"), at("7.00")); + assert_eq!(read(&plan, b"0"), at("0.00")); + assert_eq!(read(&plan, b"1.25"), at("1.25")); + assert_eq!(read(&CellPlan::Decimal { scale: 0 }, b"12"), at("12")); + } + + #[test] + fn a_float_the_server_wrote_as_null_is_out_of_range_unless_its_column_is_nullable() { + let out_of_range = |type_name: &str| { + Value::Undecodable(Box::new(UndecodedValue { + type_name: type_name.to_owned(), + reason: UndecodableReason::OutOfRange, + })) + }; + + assert_eq!(read(&CellPlan::Float64, b"null"), out_of_range("Float64")); + assert_eq!(read(&CellPlan::Float32, b"null"), out_of_range("Float32")); + assert_eq!( + read(&CellPlan::Array(Box::new(CellPlan::Float64)), b"[1.5,null]"), + Value::Array(vec![Value::Float64(1.5), out_of_range("Float64")]) + ); + assert_eq!( + read(&CellPlan::Nullable(Box::new(CellPlan::Float64)), b"null"), + Value::Null + ); + } + #[test] fn non_finite_floats_arrive_quoted() { let Value::Float32(nan) = read(&CellPlan::Float32, b"\"nan\"") else { diff --git a/core/crates/drivers/clickhouse/src/client_factory.rs b/core/crates/drivers/clickhouse/src/client_factory.rs index 591f45b6fa..2c68528b95 100644 --- a/core/crates/drivers/clickhouse/src/client_factory.rs +++ b/core/crates/drivers/clickhouse/src/client_factory.rs @@ -22,7 +22,6 @@ const POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(2); /// certificate's common name instead of a password. const CERTIFICATE_AUTH_HEADER: &str = "X-ClickHouse-SSL-Certificate-Auth"; -/// The settings every request carries. const COMMON_SETTINGS: [(&str, &str); 7] = [ // A Decimal is written at its column's scale, so a value read back // keeps the scale it was stored with and compares equal to it. @@ -47,16 +46,62 @@ const COMMON_SETTINGS: [(&str, &str); 7] = [ // `replication_wait_for_inactive_replica_timeout`, two minutes by // default, and then end with UNFINISHED although the live replicas // applied it. - ("mutations_sync", "1"), + (MUTATIONS_SYNC, "1"), // A read whose connection the app dropped stops on the server even // when no KILL could be sent. ("cancel_http_readonly_queries_on_client_close", "1"), ]; +pub(crate) const MAX_EXECUTION_TIME: &str = "max_execution_time"; +pub(crate) const TIMEOUT_OVERFLOW_MODE: &str = "timeout_overflow_mode"; +pub(crate) const MUTATIONS_SYNC: &str = "mutations_sync"; + +// A login at `readonly=1`, or one a profile constraint holds to a value, refuses the whole request that changes such a +// setting (164, 452), so the driver sends only what `system.settings` says this login may change. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ChangeableSettings(Vec<&'static str>); + +impl ChangeableSettings { + pub(crate) fn all() -> Self { + Self(Self::asked().collect()) + } + + pub(crate) fn asked() -> impl Iterator { + COMMON_SETTINGS + .into_iter() + .map(|(name, _)| name) + .chain([MAX_EXECUTION_TIME, TIMEOUT_OVERFLOW_MODE]) + } + + pub(crate) fn listed(names: &[String]) -> Self { + Self( + Self::asked() + .filter(|asked| names.iter().any(|name| name == asked)) + .collect(), + ) + } + + pub(crate) fn allows(&self, name: &str) -> bool { + self.0.contains(&name) + } + + pub(crate) fn common(&self) -> impl Iterator + '_ { + COMMON_SETTINGS.into_iter().filter(|(name, _)| self.allows(name)) + } + + // A bound without the mode would let a profile's `timeout_overflow_mode=break` end a read at it with the rows read + // so far, as if they were all. + pub(crate) fn bounds_reads(&self) -> bool { + self.allows(MAX_EXECUTION_TIME) && self.allows(TIMEOUT_OVERFLOW_MODE) + } +} + // `readonly=2` still lets `INSERT INTO FUNCTION file(...)` write a file (measured on 26.8); 1 refuses it and still -// takes the settings a request carries. A login already at 1 or 2 refuses any change of `readonly`, so it keeps its own. +// takes the settings a request carries. A login at 2 refuses any change of `readonly`. One at 1 takes the 1 as no +// change, and it puts back a session that ran `SET readonly = 0`, which a profile marking `readonly` +// `changeable_in_readonly` lets it run. pub(crate) fn strict_readonly(login_level: u8) -> Option<&'static str> { - (login_level == 0).then_some("1") + (login_level <= 1).then_some("1") } // User SQL and everything on a read-only connection is held strictly. The app's own reads keep 2, which still lets @@ -69,17 +114,17 @@ pub(crate) fn read_level(read_only: bool, user_sql: bool, login_level: u8) -> Op } pub(crate) fn read_only_settings(login_level: u8) -> Vec<(&'static str, &'static str)> { - let mut settings = vec![("allow_ddl", "0")]; + let mut settings = Vec::new(); + // A login at 1 refuses the change, and `readonly=1` refuses the DDL it would. + if login_level != 1 { + settings.push(("allow_ddl", "0")); + } settings.extend(strict_readonly(login_level).map(|level| ("readonly", level))); settings } -/// The client every request of one target goes through: hyper's pool -/// over the target's transport, the target's credentials and database, -/// and the settings every request carries. -/// -/// The clickhouse crate's own TLS stays unused: TLS is negotiated here, -/// over the transport's stream, against the trust the target names. +// No setting goes on here: the login's first answer says which of the driver's it may change. The crate's own TLS +// stays unused, since TLS is negotiated over the transport's stream against the trust the target names. pub(crate) fn base_client( target: &NetworkTarget, liveness: &LivenessPolicy, @@ -106,14 +151,11 @@ pub(crate) fn base_client( }; // A typed fetch then reads RowBinary, which names no column types, so no type name the server sends is parsed, // however deep it nests, nor a list sized by the column count the server claims. - let mut client = client + let client = client .with_url(service_url(scheme, target)) .with_validation(false) .with_product_info(APPLICATION_NAME, environment.client_version.clone()); - for (name, value) in COMMON_SETTINGS { - client = client.with_setting(name, value); - } - client = with_credentials(client, target)?; + let mut client = with_credentials(client, target)?; if let Some(database) = target.database.as_deref().filter(|name| !name.is_empty()) { client = client.with_database(database); } @@ -176,10 +218,46 @@ mod tests { #[test] fn a_login_with_a_readonly_level_of_its_own_keeps_it() { - assert_eq!(read_only_settings(1), vec![("allow_ddl", "0")]); assert_eq!(read_only_settings(2), vec![("allow_ddl", "0")]); } + #[test] + fn a_read_only_connection_holds_a_login_at_readonly_1_to_its_level_without_allow_ddl() { + assert_eq!(read_only_settings(1), vec![("readonly", "1")]); + } + + #[test] + fn only_the_drivers_own_settings_the_server_lists_are_changeable() { + let listed = ["mutations_sync", "max_threads", MAX_EXECUTION_TIME].map(str::to_owned); + + let changeable = ChangeableSettings::listed(&listed); + + assert!(changeable.allows("mutations_sync")); + assert!(changeable.allows(MAX_EXECUTION_TIME)); + assert!(!changeable.allows("max_threads")); + assert!(!changeable.allows("output_format_json_quote_denormals")); + assert_eq!(changeable.common().collect::>(), vec![("mutations_sync", "1")]); + } + + #[test] + fn a_read_is_bounded_on_the_server_only_with_its_overflow_mode() { + let bound_only = ChangeableSettings::listed(&[MAX_EXECUTION_TIME.to_owned()]); + let both = ChangeableSettings::listed(&[MAX_EXECUTION_TIME.to_owned(), TIMEOUT_OVERFLOW_MODE.to_owned()]); + + assert!(!bound_only.bounds_reads()); + assert!(both.bounds_reads()); + assert!(ChangeableSettings::all().bounds_reads()); + } + + #[test] + fn a_login_that_may_change_nothing_gets_no_common_setting() { + let changeable = ChangeableSettings::listed(&[]); + + assert_eq!(changeable.common().count(), 0); + assert!(!changeable.bounds_reads()); + assert_eq!(ChangeableSettings::all().common().count(), COMMON_SETTINGS.len()); + } + #[test] fn user_sql_is_held_to_readonly_1_on_any_connection() { assert_eq!(read_level(false, true, 0), Some("1")); @@ -193,7 +271,13 @@ mod tests { assert_eq!(read_level(false, true, 2), None); assert_eq!(read_level(false, false, 2), Some("2")); assert_eq!(read_level(false, false, 1), None); - assert_eq!(read_level(true, true, 1), None); + } + + #[test] + fn user_sql_and_a_read_only_connection_hold_a_login_at_readonly_1_to_its_level() { + assert_eq!(read_level(false, true, 1), Some("1")); + assert_eq!(read_level(true, true, 1), Some("1")); + assert_eq!(read_level(true, false, 1), Some("1")); } #[test] diff --git a/core/crates/drivers/clickhouse/src/connection.rs b/core/crates/drivers/clickhouse/src/connection.rs index 6b5a2aa42c..277bd2c97f 100644 --- a/core/crates/drivers/clickhouse/src/connection.rs +++ b/core/crates/drivers/clickhouse/src/connection.rs @@ -16,12 +16,12 @@ use tablepro_core::{ CallOptions, CommitResolution, CommitToken, Connection, DriverError, EditorSession, EditorSessionOptions, ErrorCategory, ForeignKeyInfo, LinkState, LinkWatch, LivenessPolicy, LossPhase, OwnedCalls, QueryResult, ReadStatement, ResultSink, RowLimit, ServerInfo, StreamSummary, TimeoutPhase, WorkspaceSettings, WriteAtomicity, - WriteBatch, WriteLedger, WriteReport, + WriteBatch, WriteKind, WriteLedger, WriteReport, }; use tokio::time::Instant; use tokio_util::sync::{CancellationToken, WaitForCancellationFutureOwned}; -use crate::call_client::{CallClient, read_bound, reading}; +use crate::call_client::{CallClient, read_bound}; use crate::cancel::{ClickHouseStop, StopProgress}; use crate::catalog::{self, CatalogTable}; use crate::commit_resolution::resolve; @@ -122,18 +122,14 @@ impl ClickHouseConnection { self.ensure_open()?; let stop = Arc::new(ClickHouseStop::new(self.endpoint.control().clone())); let base = match client { - Client::Reads => reading( - self.endpoint.workspace(), - read_bound(options.deadline(), bound), - self.endpoint.read_level(false), - ), - Client::UserReads => reading( - self.endpoint.workspace(), - read_bound(options.deadline(), bound), - self.endpoint.read_level(true), - ), + Client::Reads => self.endpoint.reading(read_bound(options.deadline(), bound), false), + Client::UserReads => self.endpoint.reading(read_bound(options.deadline(), bound), true), Client::Control => self.endpoint.control().clone(), }; + let options = match client { + Client::Reads | Client::UserReads if !self.endpoint.bounds_reads() => held_to(options, bound), + Client::Reads | Client::UserReads | Client::Control => options, + }; let work_stop = stop.clone(); let outcome = self .calls @@ -196,6 +192,26 @@ impl ClickHouseConnection { } } +// Where the login may not ask the server to bound a read, the call's own deadline holds it to its bound and stops it +// with a KILL. +fn held_to(options: CallOptions, bound: Option) -> CallOptions { + let Some(at) = bound.and_then(|bound| Instant::now().checked_add(bound)) else { + return options; + }; + let deadline = options.deadline().map_or(at, |deadline| deadline.min(at)); + CallOptions::new(Some(deadline), options.cancel().clone()) +} + +// A row change is an INSERT or a mutation, `ALTER TABLE ... UPDATE` or `... DELETE`. A schema change waits by +// `alter_sync`, not `mutations_sync`. +fn holds_mutation(batch: &WriteBatch) -> bool { + batch.kind == WriteKind::RowChanges + && batch.steps.iter().any(|step| { + let sql = step.statement.sql().trim_start(); + sql.get(..6).is_some_and(|word| word.eq_ignore_ascii_case("ALTER ")) + }) +} + /// A failure that may mean the route is gone rather than one statement. fn suspects_link(error: &DriverError) -> bool { match error { @@ -461,13 +477,8 @@ impl Connection for ClickHouseConnection { batch: batch.clone(), table: TableRef::new(Some(written.database), written.name), ledger: ledger.clone(), - probes: reading(self.endpoint.workspace(), probe_bound, self.endpoint.read_level(false)), - writes: self - .endpoint - .workspace() - .clone() - .with_setting("wait_end_of_query", "1") - .with_setting("max_execution_time", "0"), + probes: self.endpoint.reading(probe_bound, false), + writes: self.endpoint.writing(holds_mutation(batch)), }; let work_stop = stop.clone(); let outcome = self @@ -516,6 +527,9 @@ impl Connection for ClickHouseConnection { #[cfg(test)] mod tests { + use tablepro_core::ForeignKeyMode; + use tablepro_core::statement::{GuardedStatement, StatementBuilder}; + use super::*; #[test] @@ -529,4 +543,60 @@ mod tests { assert_eq!(known_lost(StopProgress::NotSent, false), None); assert_eq!(known_lost(StopProgress::NothingToStop, false), None); } + + fn batch(kind: WriteKind, statements: &[&str]) -> WriteBatch { + let steps = statements + .iter() + .map(|sql| { + let mut builder = StatementBuilder::new(); + builder.push_sql(sql); + GuardedStatement::unguarded(builder.finish()) + }) + .collect(); + WriteBatch::new(kind, TableRef::unqualified("t"), steps, ForeignKeyMode::Enforced) + } + + #[test] + fn only_a_row_change_holding_an_alter_waits_for_a_mutation() { + let mixed = ["INSERT INTO t VALUES (1)", "ALTER TABLE t UPDATE a = 1 WHERE id = 2"]; + + assert!(holds_mutation(&batch(WriteKind::RowChanges, &mixed))); + assert!(holds_mutation(&batch( + WriteKind::RowChanges, + &[" alter table t DELETE WHERE id = 1"] + ))); + assert!(!holds_mutation(&batch( + WriteKind::RowChanges, + &["INSERT INTO t VALUES (1)"] + ))); + assert!(!holds_mutation(&batch( + WriteKind::SchemaChanges, + &["ALTER TABLE t ADD COLUMN b Int32"] + ))); + } + + #[tokio::test(start_paused = true)] + async fn a_read_the_server_cannot_bound_ends_at_the_nearer_of_its_bound_and_the_call_deadline() { + let cancel = CancellationToken::new(); + let minute = Duration::from_secs(60); + let sooner = Instant::now() + Duration::from_secs(5); + let later = Instant::now() + Duration::from_secs(90); + + assert_eq!( + held_to(CallOptions::new(None, cancel.clone()), Some(minute)).deadline(), + Some(Instant::now() + minute) + ); + assert_eq!( + held_to(CallOptions::new(Some(later), cancel.clone()), Some(minute)).deadline(), + Some(Instant::now() + minute) + ); + assert_eq!( + held_to(CallOptions::new(Some(sooner), cancel.clone()), Some(minute)).deadline(), + Some(sooner) + ); + let unbounded = held_to(CallOptions::new(None, cancel.clone()), None); + assert_eq!(unbounded.deadline(), None); + cancel.cancel(); + assert!(unbounded.cancel().is_cancelled()); + } } diff --git a/core/crates/drivers/clickhouse/src/editor_run.rs b/core/crates/drivers/clickhouse/src/editor_run.rs index 255895468d..1cbe2a5a63 100644 --- a/core/crates/drivers/clickhouse/src/editor_run.rs +++ b/core/crates/drivers/clickhouse/src/editor_run.rs @@ -12,6 +12,7 @@ use uuid::Uuid; use crate::call_client::whole_seconds; use crate::cancel::{acknowledges_stop, kill_query, wait_until_ended}; +use crate::client_factory::{MAX_EXECUTION_TIME, TIMEOUT_OVERFLOW_MODE}; use crate::errors::{code, server_code}; use crate::lingering_query::LingeringQuery; use crate::result_origin::{TracedTable, source, traced_table, with_origins}; @@ -35,6 +36,8 @@ pub(crate) struct EditorRun { pub(crate) session: clickhouse::Client, pub(crate) control: clickhouse::Client, pub(crate) statement_timeout: Option, + // Whether the login may set the bound; the run's own deadline holds it to it either way. + pub(crate) bounds_on_server: bool, pub(crate) grace: Duration, pub(crate) shutdown: CancellationToken, pub(crate) lingering: Arc, @@ -191,16 +194,15 @@ impl EditorRun { } } - /// Send `sql` as `query_id`, which the server ends at `bound`. fn open(&self, sql: &UserSql, query_id: Uuid, bound: Option) -> Result { let mut query = self .session .query_raw(sql.as_str()) .with_setting("query_id", query_id.to_string()); - if bound.is_some() { + if bound.is_some() && self.bounds_on_server { query = query - .with_setting("max_execution_time", whole_seconds(bound).to_string()) - .with_setting("timeout_overflow_mode", "throw"); + .with_setting(MAX_EXECUTION_TIME, whole_seconds(bound).to_string()) + .with_setting(TIMEOUT_OVERFLOW_MODE, "throw"); } ResultReader::open(query) } @@ -438,12 +440,41 @@ impl Emitter { #[cfg(test)] mod tests { + use std::sync::{Mutex, PoisonError}; + use async_trait::async_trait; use tablepro_core::{ServerCode, ServerDiagnostics}; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::task::JoinHandle; use super::*; + // Answers each request with an empty result and keeps the request's head. + async fn recording_route() -> (String, Arc>>, JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a listener"); + let address = listener.local_addr().expect("an address"); + let heads = Arc::new(Mutex::new(Vec::new())); + let kept = heads.clone(); + let answering = tokio::spawn(async move { + while let Ok((mut socket, _)) = listener.accept().await { + let mut request = Vec::new(); + let mut chunk = [0; 4096]; + while !request.windows(4).any(|end| end == b"\r\n\r\n") { + match socket.read(&mut chunk).await { + Ok(0) | Err(_) => break, + Ok(read) => request.extend_from_slice(&chunk[..read]), + } + } + kept.lock() + .unwrap_or_else(PoisonError::into_inner) + .push(String::from_utf8_lossy(&request).into_owned()); + let answer = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"; + let _ = socket.write_all(answer).await; + } + }); + (format!("http://{address}"), heads, answering) + } + /// A route that takes every request and never answers one. async fn silent_route() -> (clickhouse::Client, JoinHandle<()>) { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a listener"); @@ -502,6 +533,7 @@ mod tests { session: closed_route().await, control, statement_timeout: None, + bounds_on_server: true, grace: Duration::from_millis(200), shutdown: CancellationToken::new(), lingering, @@ -524,6 +556,34 @@ mod tests { silent.abort(); } + #[tokio::test] + async fn a_run_bound_the_login_may_not_set_goes_out_without_it() { + let (url, heads, answering) = recording_route().await; + for bounds_on_server in [false, true] { + let run = EditorRun { + session: clickhouse::Client::default().with_url(url.clone()), + control: closed_route().await, + statement_timeout: None, + bounds_on_server, + grace: Duration::from_millis(200), + shutdown: CancellationToken::new(), + lingering: Arc::new(LingeringQuery::default()), + }; + let mut reader = run + .open(&UserSql::new("SELECT 1"), Uuid::new_v4(), Some(Duration::from_secs(3))) + .expect("a request"); + assert!(matches!(reader.next_event().await, Ok(ReadEvent::End))); + } + + let heads = heads.lock().unwrap_or_else(PoisonError::into_inner).clone(); + let bounded: Vec = heads + .iter() + .map(|head| head.contains("max_execution_time=") || head.contains("timeout_overflow_mode=")) + .collect(); + assert_eq!(bounded, vec![false, true]); + answering.abort(); + } + #[test] fn only_a_body_the_driver_could_not_read_is_killed_after_it_fails() { let refused = DriverError::reported(ServerDiagnostics::new( diff --git a/core/crates/drivers/clickhouse/src/editor_session.rs b/core/crates/drivers/clickhouse/src/editor_session.rs index 894506f6f3..5b4af247e8 100644 --- a/core/crates/drivers/clickhouse/src/editor_session.rs +++ b/core/crates/drivers/clickhouse/src/editor_session.rs @@ -29,6 +29,7 @@ pub(crate) struct ClickHouseEditorSession { session: Arc>, control: clickhouse::Client, statement_timeout: Option, + bounds_on_server: bool, grace: Duration, status: Arc, shutdown: CancellationToken, @@ -68,6 +69,7 @@ impl ClickHouseEditorSession { session: Arc::new(Mutex::new(session.with_setting("session_check", "1"))), control: endpoint.control().clone(), statement_timeout: options.statement_timeout, + bounds_on_server: endpoint.bounds_reads(), grace: liveness.cancel_grace, status: Arc::new(SessionStatus::new()), shutdown: CancellationToken::new(), @@ -125,6 +127,7 @@ impl EditorSession for ClickHouseEditorSession { session: self.client(), control: self.control.clone(), statement_timeout: self.statement_timeout, + bounds_on_server: self.bounds_on_server, grace: self.grace, shutdown: self.shutdown.clone(), lingering: self.lingering.clone(), diff --git a/core/crates/drivers/clickhouse/src/endpoint.rs b/core/crates/drivers/clickhouse/src/endpoint.rs index 8691fc5796..61a72122ae 100644 --- a/core/crates/drivers/clickhouse/src/endpoint.rs +++ b/core/crates/drivers/clickhouse/src/endpoint.rs @@ -7,25 +7,26 @@ use tablepro_core::{ }; use uuid::Uuid; -use crate::client_factory::{base_client, read_level, read_only_settings}; +use crate::call_client::whole_seconds; +use crate::client_factory::{ + ChangeableSettings, MAX_EXECUTION_TIME, MUTATIONS_SYNC, TIMEOUT_OVERFLOW_MODE, base_client, read_level, + read_only_settings, +}; use crate::errors::{ErrorContext, map_error_in, missing_at_login}; use crate::result_reader::drain; /// How long the server keeps an editor session's state between runs. const SESSION_TIMEOUT: Duration = Duration::from_secs(3600); -// The level goes out as text, which every JSON output setting writes as a string; a login at a readonly level of -// its own cannot change those settings for this query. -const IDENTITY_SQL: &str = "SELECT version(), currentDatabase(), toString(toUInt8(getSetting('readonly')))"; - -/// The login's answer is two short strings and a byte. A longer body is -/// not a ClickHouse server's, and is never held in memory whole. +// The login's answer is a few short strings. A longer body is not a ClickHouse server's, and is never held whole. const IDENTITY_LIMIT: usize = 64 << 10; /// Everything needed to reach one server: the workspace's client, the /// sessionless control client that KILLs and resolves, and the /// transport behind both. pub(crate) struct ClickHouseEndpoint { + // Carries no setting, so the login cannot refuse it for one. + login: clickhouse::Client, workspace: clickhouse::Client, control: clickhouse::Client, transport: Arc, @@ -36,10 +37,22 @@ pub(crate) struct ClickHouseEndpoint { read_only: bool, // The login's own `readonly` level, learned on connect. login_readonly: u8, + changeable: ChangeableSettings, + // Whether the server waits for a save's mutation, by the driver's `mutations_sync` or the login's own. + waits_for_mutations: bool, /// The database every request names, as the client was built with it. database: Option, } +// What the login's first answer says. +struct Identity { + version: String, + database: String, + readonly: u8, + changeable: ChangeableSettings, + mutations_sync: u64, +} + impl ClickHouseEndpoint { pub(crate) fn new( target: &NetworkTarget, @@ -47,10 +60,11 @@ impl ClickHouseEndpoint { environment: &ConnectEnvironment, read_only: bool, ) -> Result { - let control = base_client(target, &liveness, environment)?; + let login = base_client(target, &liveness, environment)?; let mut endpoint = Self { - workspace: control.clone(), - control, + workspace: login.clone(), + control: login.clone(), + login, transport: target.transport.clone(), liveness, tls_requested: target.tls.mode != TlsMode::Disable, @@ -58,22 +72,31 @@ impl ClickHouseEndpoint { session_timeout: SESSION_TIMEOUT, read_only, login_readonly: 0, + changeable: ChangeableSettings::all(), + waits_for_mutations: true, database: target.database.clone().filter(|name| !name.is_empty()), }; - endpoint.hold(0); + endpoint.hold(0, ChangeableSettings::all(), 0); Ok(endpoint) } - // A login at a `readonly` level of its own refuses any request that changes it, so the workspace waits for - // the level before it asks for one. - fn hold(&mut self, login_readonly: u8) { - self.login_readonly = login_readonly; - let mut workspace = self.control.clone(); + // A login refuses a whole request that changes a setting it may not, so the clients wait for its answer before + // they ask for any. + fn hold(&mut self, login_readonly: u8, changeable: ChangeableSettings, login_mutations_sync: u64) { + let mut control = self.login.clone(); + for (name, value) in changeable.common() { + control = control.with_setting(name, value); + } + let mut workspace = control.clone(); if self.read_only { for (name, value) in read_only_settings(login_readonly) { workspace = workspace.with_setting(name, value); } } + self.login_readonly = login_readonly; + self.waits_for_mutations = changeable.allows(MUTATIONS_SYNC) || login_mutations_sync > 0; + self.changeable = changeable; + self.control = control; self.workspace = workspace; } @@ -81,6 +104,39 @@ impl ClickHouseEndpoint { read_level(self.read_only, user_sql, self.login_readonly) } + pub(crate) fn bounds_reads(&self) -> bool { + self.changeable.bounds_reads() + } + + // Never a write, and ended by the server at `bound` where the login may set one; `None` leaves it unbounded. + pub(crate) fn reading(&self, bound: Option, user_sql: bool) -> clickhouse::Client { + let mut client = self.workspace.clone(); + if self.bounds_reads() { + client = client + .with_setting(TIMEOUT_OVERFLOW_MODE, "throw") + .with_setting(MAX_EXECUTION_TIME, whole_seconds(bound).to_string()); + } + match self.read_level(user_sql) { + Some(level) => client.with_setting("readonly", level), + None => client, + } + } + + // Each statement of a save runs to its end: a mutation the server stops waiting for keeps running anyway. + pub(crate) fn writing(&self, mutates: bool) -> clickhouse::Client { + let mut writes = self.workspace.clone().with_setting("wait_end_of_query", "1"); + if self.changeable.allows(MAX_EXECUTION_TIME) { + writes = writes.with_setting(MAX_EXECUTION_TIME, "0"); + } + // A server that does not wait answers a mutation once it is queued, so a save would read as done while its + // mutation may still fail. The setting this login may not change has the server refuse the save before any of + // it runs. + if mutates && !self.waits_for_mutations { + writes = writes.with_setting(MUTATIONS_SYNC, "1"); + } + writes + } + #[cfg(test)] pub(crate) fn for_fixture(fixture: &tablepro_test_fixtures::ClickHouseFixture, liveness: LivenessPolicy) -> Self { use secrecy::SecretString; @@ -110,6 +166,7 @@ impl ClickHouseEndpoint { /// The client of the workspace's own requests, read-only when the /// connection is. + #[cfg(test)] pub(crate) fn workspace(&self) -> &clickhouse::Client { &self.workspace } @@ -139,7 +196,7 @@ impl ClickHouseEndpoint { /// refused password or a failed handshake shows up. pub(crate) async fn identify(&mut self) -> Result<(ServerInfo, String), DriverError> { let bound = self.liveness.connect_timeout + self.liveness.login_timeout; - let asking = read_identity(&self.control); + let asking = read_identity(&self.login); let body = match tokio::time::timeout(bound, asking).await { Err(_) => { return Err(DriverError::Timeout { @@ -157,10 +214,13 @@ impl ClickHouseEndpoint { } Ok(Ok(body)) => body, }; - let (version, database, readonly) = identity_of(body)?; - self.hold(readonly); - let (major, minor, patch) = version_numbers(&version); - Ok((ServerInfo::new("ClickHouse", version, major, minor, patch), database)) + let identity = identity_of(body)?; + self.hold(identity.readonly, identity.changeable, identity.mutations_sync); + let (major, minor, patch) = version_numbers(&identity.version); + Ok(( + ServerInfo::new("ClickHouse", identity.version, major, minor, patch), + identity.database, + )) } // A server that takes only TLS drops a login in the clear unanswered, which a handshake of its own tells apart. @@ -207,8 +267,8 @@ impl ClickHouseEndpoint { /// The login answer's body, or None once it runs past `IDENTITY_LIMIT`. /// A RowBinary read would buffer a string of whatever length the server /// declares before it could refuse it. -async fn read_identity(control: &clickhouse::Client) -> Result>, clickhouse::error::Error> { - let mut cursor = control.query_raw(IDENTITY_SQL).fetch_bytes("JSONCompactEachRow")?; +async fn read_identity(login: &clickhouse::Client) -> Result>, clickhouse::error::Error> { + let mut cursor = login.query_raw(&identity_sql()).fetch_bytes("JSONCompactEachRow")?; let mut body = Vec::new(); while let Some(chunk) = cursor.next().await? { if body.len() + chunk.len() > IDENTITY_LIMIT { @@ -219,8 +279,19 @@ async fn read_identity(control: &clickhouse::Client) -> Result>, Ok(Some(body)) } -/// The version, the current database and the login's `readonly` level. -fn identity_of(body: Option>) -> Result<(String, String, u8), DriverError> { +// Each column is text or an array of it, which reads the same under whatever JSON output settings the login's +// profile holds, since this request sends none. +fn identity_sql() -> String { + let asked: Vec = ChangeableSettings::asked().map(|name| format!("'{name}'")).collect(); + format!( + "SELECT version(), currentDatabase(), toString(toUInt8(getSetting('readonly'))), \ + (SELECT groupArray(name) FROM system.settings WHERE readonly = 0 AND name IN ({})), \ + toString(getSetting('{MUTATIONS_SYNC}'))", + asked.join(", ") + ) +} + +fn identity_of(body: Option>) -> Result { let body = body.ok_or_else(|| { DriverError::Protocol(format!( "the server's answer to the login ran past {} KiB", @@ -229,12 +300,21 @@ fn identity_of(body: Option>) -> Result<(String, String, u8), DriverErro })?; let unreadable = |detail: String| DriverError::Protocol(format!("the server's answer to the login could not be read: {detail}")); - let (version, database, readonly): (String, String, String) = + let (version, database, readonly, changeable, mutations_sync): (String, String, String, Vec, String) = serde_json::from_slice(&body).map_err(|error| unreadable(error.to_string()))?; let readonly = readonly .parse() .map_err(|_| unreadable(format!("readonly level {readonly:?}")))?; - Ok((version, database, readonly)) + let mutations_sync = mutations_sync + .parse() + .map_err(|_| unreadable(format!("mutations_sync {mutations_sync:?}")))?; + Ok(Identity { + version, + database, + readonly, + changeable: ChangeableSettings::listed(&changeable), + mutations_sync, + }) } /// `24.8.4.13` is major 24, minor 8. @@ -246,6 +326,8 @@ fn version_numbers(version: &str) -> (u32, u32, u32) { #[cfg(test)] mod tests { + use std::sync::{Mutex, PoisonError}; + use secrecy::SecretString; use tablepro_core::{Credentials, NetworkEndpoint, TlsConfig}; use tablepro_net::DirectTcpTransport; @@ -254,9 +336,9 @@ mod tests { use super::*; - /// Answers every request with HTTP 200 and `body`, and keeps the socket - /// open, so the client reads the body to its stated length. - async fn answer_every_request(listener: TcpListener, body: Vec) { + // Answers every request with HTTP 200 and `body` and keeps the socket open, so the client reads the body to its + // stated length. + async fn answer_every_request(listener: TcpListener, body: Vec, heads: Arc>>) { let mut held = Vec::new(); while let Ok((mut socket, _)) = listener.accept().await { let mut request = Vec::new(); @@ -267,6 +349,10 @@ mod tests { Ok(read) => request.extend_from_slice(&chunk[..read]), } } + heads + .lock() + .unwrap_or_else(PoisonError::into_inner) + .push(String::from_utf8_lossy(&request).into_owned()); let head = format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len()); if socket.write_all(head.as_bytes()).await.is_ok() && socket.write_all(&body).await.is_ok() { held.push(socket); @@ -274,9 +360,14 @@ mod tests { } } - /// The login against a loopback server whose every answer is `body`, - /// run where a runtime worker would run it, on a 2 MiB stack. - fn identify_against(body: Vec) -> Result<(ServerInfo, String, u8), DriverError> { + struct Login { + identity: Result<(ServerInfo, String), DriverError>, + endpoint: ClickHouseEndpoint, + heads: Vec, + } + + // Run where a runtime worker would run it, on a 2 MiB stack. + fn log_in_against(body: Vec, read_only: bool) -> Login { let worker = std::thread::Builder::new().stack_size(2 << 20); let handle = worker .spawn(move || { @@ -287,7 +378,8 @@ mod tests { runtime.block_on(async move { let listener = TcpListener::bind("127.0.0.1:0").await.expect("a listener"); let port = listener.local_addr().expect("an address").port(); - let answering = tokio::spawn(answer_every_request(listener, body)); + let heads = Arc::new(Mutex::new(Vec::new())); + let answering = tokio::spawn(answer_every_request(listener, body, heads.clone())); let liveness = LivenessPolicy::DESKTOP; let address = NetworkEndpoint::new("127.0.0.1", port).expect("an endpoint"); let target = NetworkTarget { @@ -301,20 +393,56 @@ mod tests { options: Default::default(), }; let mut endpoint = - ClickHouseEndpoint::new(&target, liveness, &ConnectEnvironment::new("test"), false) + ClickHouseEndpoint::new(&target, liveness, &ConnectEnvironment::new("test"), read_only) .expect("an endpoint"); - let identity = endpoint - .identify() - .await - .map(|(info, database)| (info, database, endpoint.login_readonly)); + let identity = endpoint.identify().await; answering.abort(); - identity + let heads = heads.lock().unwrap_or_else(PoisonError::into_inner).clone(); + Login { + identity, + endpoint, + heads, + } }) }) .expect("a worker"); handle.join().expect("the login ran to an outcome") } + fn identify_against(body: Vec) -> Result<(ServerInfo, String, u8), DriverError> { + let login = log_in_against(body, false); + let readonly = login.endpoint.login_readonly; + login.identity.map(|(info, database)| (info, database, readonly)) + } + + fn answer(readonly: &str, changeable: &[&str]) -> Vec { + answer_with(readonly, changeable, "0") + } + + fn answer_with(readonly: &str, changeable: &[&str], mutations_sync: &str) -> Vec { + let names: Vec = changeable.iter().map(|name| format!("\"{name}\"")).collect(); + let names = names.join(","); + format!("[\"26.8.1.1\",\"tablepro\",\"{readonly}\",[{names}],\"{mutations_sync}\"]\n").into_bytes() + } + + // The server takes each of these as a setting, apart from the few names it reserves. + fn url_names(head: &str) -> Vec { + let target = head.split_whitespace().nth(1).unwrap_or_default(); + let query = target.split_once('?').map_or("", |(_, query)| query); + query + .split('&') + .filter(|pair| !pair.is_empty()) + .map(|pair| pair.split('=').next().unwrap_or_default().to_owned()) + .collect() + } + + fn settings_on(client: &clickhouse::Client) -> Vec<&'static str> { + ChangeableSettings::asked() + .chain(["readonly", "allow_ddl"]) + .filter(|name| client.get_setting(name).is_some()) + .collect() + } + /// RowBinaryWithNamesAndTypes header: the column count, the names, /// then the types, each string after its LEB128 length. fn header(name: &str, type_name: &str) -> Vec { @@ -350,13 +478,82 @@ mod tests { #[test] fn a_login_answer_reads_as_the_version_the_database_and_the_readonly_level() { let (info, database, readonly) = - identify_against(b"[\"24.8.4.13\",\"tablepro\",\"2\"]\n".to_vec()).expect("a login"); + identify_against(b"[\"24.8.4.13\",\"tablepro\",\"2\",[],\"0\"]\n".to_vec()).expect("a login"); assert_eq!((info.major, info.minor, info.patch), (24, 8, 4)); assert_eq!(database, "tablepro"); assert_eq!(readonly, 2); } + #[test] + fn the_login_request_sends_no_setting() { + let login = log_in_against(answer("1", &[]), false); + + assert!(login.identity.is_ok(), "{:?}", login.identity.err()); + let sent: Vec> = login.heads.iter().map(String::as_str).map(url_names).collect(); + assert_eq!(sent, vec![vec!["default_format".to_owned()]]); + } + + #[test] + fn a_login_at_readonly_1_that_may_change_nothing_is_sent_only_its_own_level() { + let login = log_in_against(answer("1", &[]), true); + let endpoint = login.endpoint; + let user_read = endpoint.reading(Some(Duration::from_secs(5)), true); + let app_read = endpoint.reading(Some(Duration::from_secs(5)), false); + let writes = endpoint.writing(false); + + assert_eq!(endpoint.login_readonly, 1); + assert!(!endpoint.bounds_reads()); + assert_eq!(settings_on(endpoint.control()), Vec::<&str>::new()); + for client in [endpoint.workspace(), &user_read, &app_read, &writes] { + assert_eq!(settings_on(client), vec!["readonly"]); + assert_eq!(client.get_setting("readonly"), Some("1")); + } + } + + #[test] + fn a_save_with_a_mutation_the_server_will_not_wait_for_asks_for_the_wait_so_it_is_refused() { + let pinned = log_in_against(answer_with("0", &[MAX_EXECUTION_TIME], "0"), false).endpoint; + let waits_itself = log_in_against(answer_with("0", &[MAX_EXECUTION_TIME], "2"), false).endpoint; + + assert_eq!(pinned.writing(true).get_setting(MUTATIONS_SYNC), Some("1")); + assert_eq!(pinned.writing(false).get_setting(MUTATIONS_SYNC), None); + assert_eq!(waits_itself.writing(true).get_setting(MUTATIONS_SYNC), None); + } + + #[test] + fn a_login_is_sent_only_the_settings_it_may_change() { + let changeable = [ + "output_format_json_quote_denormals", + MAX_EXECUTION_TIME, + TIMEOUT_OVERFLOW_MODE, + ]; + let login = log_in_against(answer("1", &changeable), false); + let endpoint = login.endpoint; + let read = endpoint.reading(Some(Duration::from_millis(1_500)), false); + + assert_eq!( + settings_on(endpoint.control()), + vec!["output_format_json_quote_denormals"] + ); + assert_eq!(read.get_setting(MAX_EXECUTION_TIME), Some("2")); + assert_eq!(read.get_setting(TIMEOUT_OVERFLOW_MODE), Some("throw")); + assert_eq!(endpoint.writing(false).get_setting(MAX_EXECUTION_TIME), Some("0")); + } + + #[test] + fn a_login_free_to_change_every_setting_gets_them_all() { + let all: Vec<&str> = ChangeableSettings::asked().collect(); + let login = log_in_against(answer("0", &all), true); + let endpoint = login.endpoint; + + assert!(endpoint.bounds_reads()); + assert_eq!(endpoint.control().get_setting("mutations_sync"), Some("1")); + assert_eq!(endpoint.workspace().get_setting("readonly"), Some("1")); + assert_eq!(endpoint.workspace().get_setting("allow_ddl"), Some("0")); + assert_eq!(endpoint.reading(None, false).get_setting(MAX_EXECUTION_TIME), Some("0")); + } + #[test] fn a_login_answer_past_64_kib_fails_the_login() { let body = format!("[\"{}\",\"default\",0]\n", "9".repeat(1 << 20)); diff --git a/core/crates/drivers/clickhouse/tests/integration/main.rs b/core/crates/drivers/clickhouse/tests/integration/main.rs index 28ed0cd691..c9d85a30cc 100644 --- a/core/crates/drivers/clickhouse/tests/integration/main.rs +++ b/core/crates/drivers/clickhouse/tests/integration/main.rs @@ -12,6 +12,7 @@ mod ddl; mod dump; mod keys_and_decimals; mod metadata; +mod restricted_logins; mod result_origins; mod round_trip; mod sessions; diff --git a/core/crates/drivers/clickhouse/tests/integration/restricted_logins.rs b/core/crates/drivers/clickhouse/tests/integration/restricted_logins.rs new file mode 100644 index 0000000000..fc423d280f --- /dev/null +++ b/core/crates/drivers/clickhouse/tests/integration/restricted_logins.rs @@ -0,0 +1,249 @@ +// Logins whose profile holds settings the driver would change: the server refuses a whole request that changes one. + +use std::time::Duration; + +use tablepro_core::value::{SqlDecimal, UndecodableReason, UndecodedValue}; +use tablepro_core::{ + CallOptions, ConnectOptions, Connection, DriverError, ReadOnlyRefusal, RowLimit, RunEnd, RunOptions, ServerCode, + TimeoutPhase, TlsConfig, Value, WorkspaceSettings, WriteKind, +}; +use tablepro_test_fixtures::{ClickHouseFixture, FixtureCredentials}; +use tablepro_test_support::conformance::test_liveness; +use tokio::time::Instant; +use tokio_util::sync::CancellationToken; + +use crate::support::{ + apply, connect, connect_with, count, direct, options_over, password, quoted, rows, run, run_ok, run_with, session, + setup, start, statement, unique, +}; + +fn options_for( + fixture: &ClickHouseFixture, + credentials: &FixtureCredentials, + settings: WorkspaceSettings, +) -> ConnectOptions { + let liveness = test_liveness(); + options_over( + direct(fixture.host(), fixture.http_port(), &liveness), + password(credentials), + TlsConfig::disabled(), + settings, + liveness, + ) +} + +fn decimal(text: &str) -> Value { + Value::Decimal(text.parse::().expect("a decimal")) +} + +// Long enough that only a stop ends it; the marker finds it in `system.processes`. +fn endless(marker: &str) -> String { + format!("SELECT count() FROM numbers(100000000000000) WHERE toString(number) != '{marker}'") +} + +async fn wait_until_gone(owner: &dyn Connection, marker: &str) { + let running = format!( + "SELECT count() FROM system.processes WHERE query LIKE '%{marker}%' AND query NOT LIKE '%system.processes%'" + ); + let give_up = Instant::now() + Duration::from_secs(10); + while count(owner, &running).await > 0 { + assert!( + Instant::now() < give_up, + "the stopped query {marker} still runs on the server" + ); + tokio::time::sleep(Duration::from_millis(100)).await; + } +} + +#[tokio::test] +#[ignore = "requires docker"] +async fn a_login_at_readonly_1_connects_and_reads_no_value_as_null_that_is_not() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + let qualified = quoted(&unique("readonly")); + setup( + owner.as_ref(), + &[ + &format!( + "CREATE TABLE {qualified} (id Int32, amount Decimal(10, 2), ratio Float64) \ + ENGINE = MergeTree ORDER BY id" + ), + &format!("INSERT INTO {qualified} VALUES (1, 1.5, nan)"), + ], + ) + .await; + let out_of_range = Value::Undecodable(Box::new(UndecodedValue { + type_name: "Float64".to_owned(), + reason: UndecodableReason::OutOfRange, + })); + + let logins = [ + (fixture.readonly_credentials(), false), + (fixture.lenient_readonly_credentials(), true), + ]; + for (credentials, quotes_nan) in logins { + for read_only in [false, true] { + let settings = WorkspaceSettings::workspace(read_only); + let reader = connect_with(options_for(&fixture, &credentials, settings)).await; + let context = format!("{}, read_only {read_only}", credentials.username); + + let read = rows(reader.as_ref(), &format!("SELECT id, amount, ratio FROM {qualified}")).await; + + let [row] = read.as_slice() else { + panic!("{context} read {read:?}"); + }; + assert_eq!(row[..2], [Value::Int(1), decimal("1.50")], "{context}"); + if quotes_nan { + assert!( + matches!(row[2], Value::Float64(ratio) if ratio.is_nan()), + "{context}: {row:?}" + ); + } else { + assert_eq!(row[2], out_of_range, "{context}"); + } + reader.close().await.expect("a close"); + } + } + owner.close().await.expect("a close"); +} + +#[tokio::test] +#[ignore = "requires docker"] +async fn a_login_at_readonly_1_runs_sql_within_its_bounds_and_cannot_write() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + let qualified = quoted(&unique("readonly")); + setup( + owner.as_ref(), + &[&format!( + "CREATE TABLE {qualified} (id Int32) ENGINE = MergeTree ORDER BY id" + )], + ) + .await; + let mut settings = WorkspaceSettings::workspace(false); + settings.statement_timeout = Duration::from_secs(1); + let reader = connect_with(options_for(&fixture, &fixture.readonly_credentials(), settings)).await; + + let editor = session(reader.as_ref()).await; + let level = run_ok(editor.as_ref(), "SELECT getSetting('readonly')").await; + let in_editor = unique("editor"); + let options = RunOptions::new(CancellationToken::new()).with_deadline(Some(Duration::from_secs(1))); + let (stopped, _) = run_with(editor.as_ref(), &endless(&in_editor), options).await; + wait_until_gone(owner.as_ref(), &in_editor).await; + editor.close().await; + let in_grid = unique("grid"); + let unbounded = CallOptions::new(None, CancellationToken::new()); + let timed_out = reader + .query_bound(&statement(&endless(&in_grid)), RowLimit::EDITOR_DEFAULT, unbounded) + .await; + wait_until_gone(owner.as_ref(), &in_grid).await; + let refused = apply( + reader.as_ref(), + WriteKind::RowChanges, + &[&format!("INSERT INTO {qualified} VALUES (1)")], + ) + .await; + + assert_eq!(level.rows(), vec![vec![Value::Int(1)]]); + assert!(matches!(stopped.end, RunEnd::TimedOut { .. }), "{:?}", stopped.end); + assert!( + matches!( + timed_out, + Err(DriverError::Timeout { + phase: TimeoutPhase::Statement, + .. + }) + ), + "the statement timeout let the read run: {timed_out:?}" + ); + assert!( + matches!(&refused, Err(DriverError::RolledBack { statement_index: 0, source }) + if matches!(&**source, DriverError::ReadOnly(ReadOnlyRefusal::ServerRejected(_)))), + "{refused:?}" + ); + reader.close().await.expect("a close"); + owner.close().await.expect("a close"); +} + +// The lenient profile lets the session run `SET readonly = 0`; the next request carries `readonly=1` again. +#[tokio::test] +#[ignore = "requires docker"] +async fn a_read_only_connection_puts_back_the_readonly_level_a_session_lifted() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + let qualified = quoted(&unique("lifted")); + setup( + owner.as_ref(), + &[&format!( + "CREATE TABLE {qualified} (id Int32) ENGINE = MergeTree ORDER BY id" + )], + ) + .await; + let settings = WorkspaceSettings::workspace(true); + let reader = connect_with(options_for(&fixture, &fixture.lenient_readonly_credentials(), settings)).await; + let editor = session(reader.as_ref()).await; + + run_ok(editor.as_ref(), "SET readonly = 0").await; + let (inserted, _) = run(editor.as_ref(), &format!("INSERT INTO {qualified} VALUES (1)")).await; + + assert!( + matches!( + &inserted.end, + RunEnd::Failed(DriverError::ReadOnly(ReadOnlyRefusal::ServerRejected(_))) + ), + "{:?}", + inserted.end + ); + assert_eq!( + count(owner.as_ref(), &format!("SELECT count() FROM {qualified}")).await, + 0 + ); + editor.close().await; + reader.close().await.expect("a close"); + owner.close().await.expect("a close"); +} + +#[tokio::test] +#[ignore = "requires docker"] +async fn a_login_whose_profile_holds_mutations_sync_at_0_is_refused_a_mutation_rather_than_told_it_saved() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + let qualified = quoted(&unique("unsynced")); + setup( + owner.as_ref(), + &[ + &format!("CREATE TABLE {qualified} (id Int32, n Int32) ENGINE = MergeTree ORDER BY id"), + &format!("INSERT INTO {qualified} VALUES (1, 0)"), + ], + ) + .await; + let settings = WorkspaceSettings::workspace(false); + let writer = connect_with(options_for(&fixture, &fixture.unsynced_credentials(), settings)).await; + + let inserted = apply( + writer.as_ref(), + WriteKind::RowChanges, + &[&format!("INSERT INTO {qualified} VALUES (2, 0)")], + ) + .await; + let updated = apply( + writer.as_ref(), + WriteKind::RowChanges, + &[&format!("ALTER TABLE {qualified} UPDATE n = 1 WHERE id = 1")], + ) + .await; + + assert!(inserted.is_ok(), "{inserted:?}"); + assert!( + matches!(&updated, Err(DriverError::RolledBack { statement_index: 0, source }) + if matches!(&**source, DriverError::Server(diagnostics) + if diagnostics.code == Some(ServerCode::ClickHouse { code: 452 }))), + "{updated:?}" + ); + assert_eq!( + count(owner.as_ref(), &format!("SELECT count() FROM {qualified} WHERE n = 1")).await, + 0 + ); + writer.close().await.expect("a close"); + owner.close().await.expect("a close"); +} diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index 57a6ba76c2..e0372d600b 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -17,6 +17,7 @@ mod prompter; mod results; mod run; mod runtime; +mod safe_mode_gate; mod session; mod syntax; pub mod window_abi; @@ -37,6 +38,7 @@ pub use library::{ pub use prompter::{HostPrompter, PromptAnswer, PromptFieldInfo, PromptPurposeTag, PromptReasonTag, PromptRequest}; pub use results::{ResultColumnInfo, ResultSet, ValueKind}; pub use run::{QueryRun, RunEvent, RunListener, RunOutcome, TransactionStateTag, TruncationTag}; +pub use safe_mode_gate::RunGate; pub use session::{ColumnSort, DatabaseSession, FailureCauseTag, SessionHealthTag, TableQuery}; uniffi::setup_scaffolding!(); diff --git a/core/crates/ffi/src/library.rs b/core/crates/ffi/src/library.rs index 462b4b5ab1..dce95fe34d 100644 --- a/core/crates/ffi/src/library.rs +++ b/core/crates/ffi/src/library.rs @@ -55,6 +55,16 @@ pub enum SafeModeTag { ReadOnly, } +impl From for SafeModeLevel { + fn from(tag: SafeModeTag) -> Self { + match tag { + SafeModeTag::Off => Self::Off, + SafeModeTag::ConfirmWrites => Self::ConfirmWrites, + SafeModeTag::ReadOnly => Self::ReadOnly, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] pub struct TlsSettings { pub mode: TlsModeTag, @@ -543,11 +553,7 @@ pub(crate) fn apply(record: &ConnectionRecord, name: String, group_id: Option SafeModeLevel::Off, - SafeModeTag::ConfirmWrites => SafeModeLevel::ConfirmWrites, - SafeModeTag::ReadOnly => SafeModeLevel::ReadOnly, - }; + saved.safe_mode = record.safe_mode.into(); saved.query_timeout_secs = record.query_timeout_secs; saved.color = record.color.as_deref().and_then(ConnectionColor::from_id); saved.group_id = group_id; diff --git a/core/crates/ffi/src/safe_mode_gate.rs b/core/crates/ffi/src/safe_mode_gate.rs new file mode 100644 index 0000000000..730dbafb1a --- /dev/null +++ b/core/crates/ffi/src/safe_mode_gate.rs @@ -0,0 +1,185 @@ +use tablepro_core::sql_syntax::script::{LexicalSettings, ScriptPlan, ScriptTokenKind}; +use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, statement_access}; +use tablepro_core::{SafeModeLevel, WriteGate}; + +// What Safe Mode lets an editor run do before it is sent. The editor sends its text whole, so one refused statement +// keeps the whole run back. +#[derive(Debug, Clone, Default, PartialEq, Eq, uniffi::Record)] +pub struct RunGate { + pub refused: bool, + // The statements to ask about first, each as it would run. + pub confirm: Vec, + // One of them drops or empties a table, or changes every row of one. + pub destructive: bool, +} + +// Each batch is judged on its own, as the Linux app judges the batches it sends. +pub(crate) fn gate(sql: &str, grammar: SqlGrammar, level: SafeModeLevel) -> RunGate { + let mut gate = RunGate::default(); + let plan = ScriptPlan::build(sql, grammar, LexicalSettings::default_for(grammar)); + let mut strictest = StatementAccess::Read; + for batch in plan.batches() { + let Some(text) = sql.get(batch.range.clone()) else { + continue; + }; + // A Redis command's range is exact, and a blank Rust trims can be part of its last argument. + let text = if grammar.is_sql() { text.trim() } else { text }; + if text.is_empty() { + continue; + } + let access = statement_access(text, grammar); + strictest = strictest.max(access); + match level.gate(access) { + WriteGate::Run => {} + WriteGate::Refuse => gate.refused = true, + WriteGate::Confirm => { + gate.destructive |= access == StatementAccess::Destructive; + // `GO 100` runs the batch a hundred times, which the question has to show. + gate.confirm.push(match batch.repeat.get() { + 1 => text.to_owned(), + times => format!("{text}\nGO {times}"), + }); + } + } + } + if sent_in_batches(sql, grammar, &plan) { + return gate; + } + // The driver splits the text it gets with the session's own settings, so a reading of the whole that is stricter + // than every batch decides, and the question shows the text as it is sent. + let whole = statement_access(sql, grammar); + if whole > strictest { + match level.gate(whole) { + WriteGate::Run => {} + WriteGate::Refuse => gate.refused = true, + WriteGate::Confirm => { + gate.destructive |= whole == StatementAccess::Destructive; + gate.confirm = vec![sql.trim().to_owned()]; + } + } + } + gate +} + +// The MySQL driver sends a script with a DELIMITER line and nothing left open as these batches, one at a time, so +// no server reads the text whole, DELIMITER lines and all. +fn sent_in_batches(sql: &str, grammar: SqlGrammar, plan: &ScriptPlan) -> bool { + grammar == SqlGrammar::MySql + && plan.diagnostics().is_empty() + && plan + .tokens(0..sql.len()) + .any(|token| token.kind == ScriptTokenKind::Directive) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn confirm_writes_asks_about_an_update_and_runs_a_select_at_once() { + let update = gate( + "UPDATE t SET a = 1 WHERE id = 2", + SqlGrammar::PostgreSql, + SafeModeLevel::ConfirmWrites, + ); + let select = gate("SELECT * FROM t", SqlGrammar::PostgreSql, SafeModeLevel::ConfirmWrites); + + assert_eq!(update.confirm, ["UPDATE t SET a = 1 WHERE id = 2"]); + assert!(!update.destructive); + assert!(!update.refused); + assert_eq!(select, RunGate::default()); + } + + #[test] + fn each_statement_of_a_script_is_asked_about_on_its_own() { + let run = gate( + "SELECT 1; INSERT INTO t VALUES (1); SELECT 2; DELETE FROM t", + SqlGrammar::MySql, + SafeModeLevel::ConfirmWrites, + ); + + assert_eq!(run.confirm.len(), 2); + assert!(run.confirm[0].starts_with("INSERT")); + assert!(run.confirm[1].starts_with("DELETE")); + assert!(run.destructive); + } + + #[test] + fn off_asks_only_before_a_destructive_statement() { + let write = gate("INSERT INTO t VALUES (1)", SqlGrammar::Sqlite, SafeModeLevel::Off); + let drop = gate("DROP TABLE t", SqlGrammar::Sqlite, SafeModeLevel::Off); + + assert_eq!(write, RunGate::default()); + assert_eq!(drop.confirm, ["DROP TABLE t"]); + assert!(drop.destructive); + } + + #[test] + fn a_byte_order_mark_does_not_hide_a_sqlite_statement_from_the_question() { + let drop = gate("\u{feff}DROP TABLE t", SqlGrammar::Sqlite, SafeModeLevel::Off); + + assert_eq!(drop.confirm.len(), 1); + assert!(drop.destructive); + } + + #[test] + fn a_repeated_batch_shows_how_many_times_it_runs() { + let run = gate( + "UPDATE accounts SET balance = balance - 10 WHERE id = 1\nGO 100", + SqlGrammar::MsSql, + SafeModeLevel::ConfirmWrites, + ); + + assert_eq!( + run.confirm, + ["UPDATE accounts SET balance = balance - 10 WHERE id = 1\nGO 100"] + ); + } + + #[test] + fn read_only_keeps_back_a_script_with_one_write_and_asks_nothing() { + let script = gate( + "SELECT 1\nGO\nUPDATE t SET a = 1 WHERE id = 1\nGO\nSELECT 2", + SqlGrammar::MsSql, + SafeModeLevel::ReadOnly, + ); + let read = gate("SELECT 1\nGO\nSELECT 2", SqlGrammar::MsSql, SafeModeLevel::ReadOnly); + + assert!(script.refused); + assert!(script.confirm.is_empty()); + assert_eq!(read, RunGate::default()); + } + + #[test] + fn a_mysql_executable_comment_is_read_as_the_server_reads_it() { + // MySQL 8.0 skips the second comment past its nested `/* */` and runs the DROP after it. + let script = "/*!40103 SET TIME_ZONE='+00:00' */;\nSELECT 1 /*!99999 /* */ ' */ ; DROP TABLE orders; -- '"; + + let off = gate(script, SqlGrammar::MySql, SafeModeLevel::Off); + let read_only = gate(script, SqlGrammar::MySql, SafeModeLevel::ReadOnly); + + assert_eq!(off.confirm, ["SELECT 1 /*!99999 /* */ ' */ ; DROP TABLE orders; -- '"]); + assert!(off.destructive); + assert!(read_only.refused); + } + + #[test] + fn a_text_only_the_whole_reading_shows_a_drop_in_is_asked_about_as_it_is_sent() { + // A PostgreSQL session can turn backslash escapes on, and then the first string runs on and DROP is code. + let sql = "SELECT 'x\\'; SELECT '; DROP TABLE t; --'"; + + let run = gate(sql, SqlGrammar::PostgreSql, SafeModeLevel::ConfirmWrites); + + assert_eq!(run.confirm, [sql]); + assert!(run.destructive); + } + + #[test] + fn a_mysql_read_script_with_delimiter_lines_runs_at_every_level() { + let script = "DELIMITER //\nSELECT 1//\nDELIMITER ;\nSELECT 2;"; + + for level in SafeModeLevel::ALL { + assert_eq!(gate(script, SqlGrammar::MySql, level), RunGate::default(), "{level:?}"); + } + } +} diff --git a/core/crates/ffi/src/session.rs b/core/crates/ffi/src/session.rs index 79c00f9e2f..8fd5bd9c10 100644 --- a/core/crates/ffi/src/session.rs +++ b/core/crates/ffi/src/session.rs @@ -12,7 +12,9 @@ use crate::cancel_token::{CancelToken, token}; use crate::catalog::{ColumnEntry, ObjectEntry, ObjectScopeArg, SchemaEntry, TableName}; use crate::editor::QueryEditor; use crate::error::{BoxedErrorInfo, CoreError}; +use crate::library::SafeModeTag; use crate::runtime::on_runtime; +use crate::safe_mode_gate::{self, RunGate}; #[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] pub struct ColumnSort { @@ -309,6 +311,17 @@ impl DatabaseSession { Ok(with_order_by(&sql, grammar, &format!("{target} {direction}"))) } + // At the session's level now, read by the engine's own rules also while the session's link is down. + pub fn gate_run(&self, sql: String) -> RunGate { + safe_mode_gate::gate(&sql, self.session.grammar(), self.session.safe_mode()) + } + + // A level saved while the session is open applies to it at once. False when `level` is on the other side of + // Read-only, which takes a new connection; the session keeps its level until then. + pub fn set_safe_mode(&self, level: SafeModeTag) -> bool { + self.session.set_safe_mode(level.into()) + } + pub async fn open_editor(&self, cancel: Option>) -> Result, CoreError> { let session = self.session.clone(); let tasks = self.tasks.clone(); diff --git a/core/crates/ffi/tests/safe_mode_gate.rs b/core/crates/ffi/tests/safe_mode_gate.rs new file mode 100644 index 0000000000..1aac542fb9 --- /dev/null +++ b/core/crates/ffi/tests/safe_mode_gate.rs @@ -0,0 +1,221 @@ +#![cfg(feature = "engines")] +#![expect( + clippy::unwrap_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::path::Path; +use std::sync::Arc; + +use tablepro_ffi::{ + ConnectionRecord, ConnectionSecrets, Core, DatabaseSession, FileOpenRequest, HostConfig, HostSecretError, + HostSecrets, RunGate, RunListener, RunOutcome, RunSettings, SafeModeTag, SecretItemTag, SecretKindTag, TlsModeTag, + TlsSettings, +}; + +struct NoSecrets; + +impl HostSecrets for NoSecrets { + fn load(&self, _: String, _: SecretKindTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store(&self, _: String, _: SecretKindTag, _: String, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn relabel(&self, _: String, _: SecretKindTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete(&self, _: String, _: SecretKindTag) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_connection(&self, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn load_item(&self, _: SecretItemTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store_item(&self, _: SecretItemTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_item(&self, _: SecretItemTag) -> Result<(), HostSecretError> { + Ok(()) + } +} + +struct Quiet; + +impl RunListener for Quiet { + fn events_pending(&self) {} +} + +fn core(dir: &Path) -> Arc { + Core::new( + HostConfig { + client_version: "test".to_owned(), + storage_dir: Some(dir.display().to_string()), + }, + Box::new(NoSecrets), + ) + .unwrap() +} + +fn file(path: &Path, read_only: bool, create: bool) -> FileOpenRequest { + FileOpenRequest { + path: path.display().to_string(), + driver_id: None, + read_only, + create, + } +} + +async fn people(core: &Core, path: &Path) { + let session = core.open_file(file(path, false, true), None).await.unwrap(); + let editor = session.open_editor(None).await.unwrap(); + let run = editor.run( + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)".to_owned(), + RunSettings::default(), + Box::new(Quiet), + ); + assert!(matches!(run.outcome().await.unwrap(), RunOutcome::Completed { .. })); + editor.close().await.unwrap(); + session.close().await.unwrap(); +} + +fn gate(session: &DatabaseSession, sql: &str) -> RunGate { + session.gate_run(sql.to_owned()) +} + +async fn saved(core: &Core, path: &Path, safe_mode: SafeModeTag) -> Arc { + let record = ConnectionRecord { + id: String::new(), + name: "people".to_owned(), + driver_id: "sqlite".to_owned(), + host: String::new(), + port: 0, + database: path.display().to_string(), + username: String::new(), + integrated_auth: false, + tls: TlsSettings { + mode: TlsModeTag::Disable, + ca_file: None, + client_certificate: None, + client_key: None, + server_name: None, + }, + ssh: None, + safe_mode, + query_timeout_secs: None, + color: None, + group_id: None, + last_opened_ms: None, + tags: Vec::new(), + }; + let saved = core + .library() + .unwrap() + .save(record, ConnectionSecrets::default()) + .await + .unwrap(); + core.connect_saved(saved.id, ConnectionSecrets::default(), None, None) + .await + .unwrap() +} + +#[tokio::test] +async fn confirm_writes_on_a_saved_connection_asks_before_a_write_and_never_before_a_read() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("people.sqlite"); + let core = core(dir.path()); + people(&core, &path).await; + let session = saved(&core, &path, SafeModeTag::ConfirmWrites).await; + + let write = gate(&session, "SELECT 1; UPDATE people SET name = 'Ada' WHERE id = 1"); + let read = gate(&session, "SELECT * FROM people"); + session.close().await.unwrap(); + + assert_eq!(write.confirm, ["UPDATE people SET name = 'Ada' WHERE id = 1"]); + assert!(!write.destructive); + assert!(!write.refused); + assert_eq!(read, RunGate::default()); +} + +#[tokio::test] +async fn off_asks_only_before_a_statement_that_empties_a_table() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("people.sqlite"); + let core = core(dir.path()); + people(&core, &path).await; + let session = saved(&core, &path, SafeModeTag::Off).await; + + let insert = gate(&session, "INSERT INTO people (name) VALUES ('Ada')"); + let delete = gate(&session, "DELETE FROM people"); + session.close().await.unwrap(); + + assert_eq!(insert, RunGate::default()); + assert_eq!(delete.confirm, ["DELETE FROM people"]); + assert!(delete.destructive); +} + +#[tokio::test] +async fn a_file_opened_read_only_keeps_a_write_back_and_lets_a_read_run() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("people.sqlite"); + let core = core(dir.path()); + people(&core, &path).await; + let session = core.open_file(file(&path, true, false), None).await.unwrap(); + + let write = gate(&session, "SELECT 1; INSERT INTO people (name) VALUES ('Ada')"); + let read = gate(&session, "SELECT * FROM people"); + session.close().await.unwrap(); + + assert!(write.refused); + assert!(write.confirm.is_empty()); + assert_eq!(read, RunGate::default()); +} + +#[tokio::test] +async fn a_level_set_on_an_open_session_applies_at_once_but_never_across_read_only() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("people.sqlite"); + let core = core(dir.path()); + people(&core, &path).await; + let session = saved(&core, &path, SafeModeTag::Off).await; + let update = "UPDATE people SET name = 'Ada' WHERE id = 1"; + + let at_off = gate(&session, update); + let confirmed = session.set_safe_mode(SafeModeTag::ConfirmWrites); + let asked = gate(&session, update); + let read_only = session.set_safe_mode(SafeModeTag::ReadOnly); + let still_asked = gate(&session, update); + session.close().await.unwrap(); + + assert_eq!(at_off, RunGate::default()); + assert!(confirmed); + assert_eq!(asked.confirm, [update]); + assert!(!read_only); + assert!(!still_asked.refused); + assert_eq!(still_asked.confirm, [update]); +} + +#[tokio::test] +async fn a_session_whose_link_is_down_still_reads_sql_by_its_own_engine() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("people.sqlite"); + let core = core(dir.path()); + people(&core, &path).await; + let session = core.open_file(file(&path, false, false), None).await.unwrap(); + session.close().await.unwrap(); + + // SQLite ends a block comment at its first `*/`, so the DROP runs; PostgreSQL would read one nested comment. + let run = gate(&session, "SELECT 1 /* /* */; DROP TABLE people; -- */"); + + assert_eq!(run.confirm, ["DROP TABLE people"]); + assert!(run.destructive); +} diff --git a/core/crates/session/src/session/session.rs b/core/crates/session/src/session/session.rs index dff1692fb4..3f49f9a79e 100644 --- a/core/crates/session/src/session/session.rs +++ b/core/crates/session/src/session/session.rs @@ -5,6 +5,7 @@ use std::time::Duration; use tablepro_core::credentials::CredentialInteraction; use tablepro_core::dialect::SqlDialect; use tablepro_core::meta::{ObjectInfo, ObjectScope, SchemaInfo}; +use tablepro_core::sql_syntax::SqlGrammar; use tablepro_core::{ CallOptions, ColumnInfo, DriverError, EditorSession, EditorSessionOptions, SafeModeLevel, TableRef, UnsupportedFeature, WorkspaceSettings, @@ -35,6 +36,8 @@ pub struct Session { metadata: ConnectionMetadata, workspace_settings: WorkspaceSettings, safe_mode: Mutex, + // The engine's own, from the connection the session opened on, for SQL read while no connection is current. + grammar: SqlGrammar, reachability: Arc>, last_rejection: Arc>>>, trusted_host_key: Arc>>, @@ -81,6 +84,7 @@ impl Session { &parts.request, &parts.established, ))); + let grammar = parts.established.connection.dialect().grammar(); let link = Arc::new(ConnectionLink::new(parts.established)); let last_rejection = Arc::new(Mutex::new(None)); let trusted_host_key = Arc::new(Mutex::new(None)); @@ -128,6 +132,7 @@ impl Session { metadata: parts.request.metadata, workspace_settings: parts.request.workspace_settings, safe_mode: Mutex::new(parts.request.safe_mode), + grammar, reachability, last_rejection, trusted_host_key, @@ -200,6 +205,11 @@ impl Session { self.link.lease().map(|lease| lease.dialect()) } + // Also while the link is down, since an editor tab's own connection can still be up and run what it is sent. + pub fn grammar(&self) -> SqlGrammar { + self.current_dialect().map_or(self.grammar, |dialect| dialect.grammar()) + } + /// Which connection is current. A request issued under an older one /// may have failed because that connection went away. pub fn link_generation(&self) -> LinkGeneration { diff --git a/core/crates/storage/Cargo.toml b/core/crates/storage/Cargo.toml index 9b95084fb4..1a620948e1 100644 --- a/core/crates/storage/Cargo.toml +++ b/core/crates/storage/Cargo.toml @@ -18,6 +18,7 @@ uuid.workspace = true sqlx = { workspace = true, features = ["migrate", "macros"] } chrono.workspace = true tempfile.workspace = true +tracing.workspace = true # Linux links the distribution's SQLite, so its security fixes arrive # through the distribution; the other targets build the copy sqlx ships. diff --git a/core/crates/storage/src/connections/additions.rs b/core/crates/storage/src/connections/additions.rs index 60888a5542..6d29bced5b 100644 --- a/core/crates/storage/src/connections/additions.rs +++ b/core/crates/storage/src/connections/additions.rs @@ -23,8 +23,8 @@ impl ConnectionStore { expected: &ConnectionList, imported: Vec, ) -> Result, StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; if !document.lists_as(expected) { return Err(StorageError::ImportOutdated); } diff --git a/core/crates/storage/src/connections/mod.rs b/core/crates/storage/src/connections/mod.rs index 45b03da922..975f0f6f28 100644 --- a/core/crates/storage/src/connections/mod.rs +++ b/core/crates/storage/src/connections/mod.rs @@ -1,7 +1,8 @@ use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; +use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, MutexGuard}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -416,19 +417,18 @@ pub enum RemoveOutcome { NotPresent, } +// The file's bytes when the cache was read from it or written to it, `None` for no file. +type FileBytes = Option>; + #[derive(Debug)] enum StoreState { Unloaded, - Ready(ConnectionsDocument), - Unavailable(DocumentProblem), + Ready(ConnectionsDocument, FileBytes), + // `None` when the file could not be read at all. + Unavailable(DocumentProblem, Option), } -/// The saved-connection list on disk. One store per storage root, so a -/// development build and an installed build never share a file. -/// -/// Every mutation holds the mutex across read, apply, encode and write, -/// so two threads cannot interleave a read-modify-write and lose an -/// entry. +// A write holds the mutex and a lock file from its re-read of the list to its rename, so no writer loses an entry. #[derive(Clone)] pub struct ConnectionStore { inner: Arc, @@ -440,6 +440,25 @@ struct StoreInner { snapshot: watch::Sender, } +struct WriteGuard<'a> { + state: MutexGuard<'a, StoreState>, + _file: Option, +} + +impl Deref for WriteGuard<'_> { + type Target = StoreState; + + fn deref(&self) -> &StoreState { + &self.state + } +} + +impl DerefMut for WriteGuard<'_> { + fn deref_mut(&mut self) -> &mut StoreState { + &mut self.state + } +} + impl ConnectionStore { pub fn new(paths: &crate::StoragePaths) -> Self { let (snapshot, _) = watch::channel(ConnectionListSnapshot { @@ -479,8 +498,8 @@ impl ConnectionStore { /// A new entry, or one that changed group, goes last in its group. pub fn upsert_blocking(&self, connection: SavedConnection) -> Result<(), StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let mut connection = connection; let stored = document .connections @@ -495,8 +514,8 @@ impl ConnectionStore { // One write, so no publish shows a copy at the end of its group before it moves up. pub fn insert_after_blocking(&self, connection: SavedConnection, after: Uuid) -> Result<(), StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let mut connection = connection; let stored = document .connections @@ -528,8 +547,8 @@ impl ConnectionStore { } pub fn remove_blocking(&self, id: Uuid) -> Result { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let before = document.connections.len(); document.connections.retain(|saved| saved.id != id); if document.connections.len() == before { @@ -546,8 +565,8 @@ impl ConnectionStore { name: ConnectionTagName, color: crate::ConnectionColor, ) -> Result { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; if document.tags.iter().any(|tag| tag.name.same_name(name.as_str())) { return Err(StorageError::TagNameTaken); } @@ -560,8 +579,8 @@ impl ConnectionStore { /// Rename or recolour the tag with `changed`'s id, on every /// connection that carries it. pub fn update_tag_blocking(&self, changed: ConnectionTag) -> Result<(), StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; if document .tags .iter() @@ -581,8 +600,8 @@ impl ConnectionStore { /// Delete a tag and take it off every connection, in one write. pub fn delete_tag_blocking(&self, id: Uuid) -> Result { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let before = document.tags.len(); document.tags.retain(|tag| tag.id != id); if document.tags.len() == before { @@ -601,8 +620,8 @@ impl ConnectionStore { id: Uuid, change: impl FnOnce(&mut SavedConnection), ) -> Result, StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let Some(index) = document.connections.iter().position(|saved| saved.id == id) else { return Ok(None); }; @@ -624,8 +643,8 @@ impl ConnectionStore { // For the picker's Recent section. A connection opened without saving has no entry, so nothing is written. pub fn touch_last_opened_blocking(&self, id: Uuid) -> Result<(), StorageError> { - let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let mut document = self.ensure_current(&mut state)?; let Some(connection) = document.connections.iter_mut().find(|saved| saved.id == id) else { return Ok(()); }; @@ -733,8 +752,8 @@ impl ConnectionStore { /// refused, since the user agreed to that preview and not another. pub fn apply_import_blocking(&self, preview: &ImportPreview) -> Result { let report = preview.report(); - let mut state = self.lock(); - let current = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let current = self.ensure_current(&mut state)?; if current != preview.before { return Err(StorageError::ImportOutdated); } @@ -747,23 +766,25 @@ impl ConnectionStore { Ok(report) } - /// Move the unreadable file aside and start from an empty list, - /// returning the name the old file now has so the user can find it. - /// Only valid while the list is unavailable. + // Returns the name the unreadable file was moved to. Another process may have reset or rewritten it since. pub fn reset_unreadable_blocking(&self, now: std::time::SystemTime) -> Result { - let mut state = self.lock(); - if !matches!(*state, StoreState::Unavailable(_)) { - return Err(StorageError::Schema( - "the saved connections are readable; there is nothing to reset".to_owned(), - )); + let mut state = self.lock_to_write(); + match self.ensure_current(&mut state) { + Err(StorageError::DocumentUnavailable(_)) => {} + Ok(_) => { + return Err(StorageError::Schema( + "the saved connections are readable; there is nothing to reset".to_owned(), + )); + } + Err(error) => return Err(error), } let moved = crate::fs::move_aside_blocking(&self.inner.path, now)?; - *state = StoreState::Ready(ConnectionsDocument::default()); + *state = StoreState::Ready(ConnectionsDocument::default(), None); self.publish(&state); Ok(moved) } - fn lock(&self) -> std::sync::MutexGuard<'_, StoreState> { + fn lock(&self) -> MutexGuard<'_, StoreState> { match self.inner.state.lock() { Ok(guard) => guard, // A panic mid-mutation leaves the cache suspect, so drop it @@ -776,29 +797,74 @@ impl ConnectionStore { } } - /// A copy of the list to change, read from disk the first time. + fn lock_to_write(&self) -> WriteGuard<'_> { + let state = self.lock(); + let file = crate::fs::lock_beside_blocking(&self.inner.path); + WriteGuard { state, _file: file } + } + fn ensure_loaded(&self, state: &mut StoreState) -> Result { - if let StoreState::Unloaded = state { - *state = match read_document(&self.inner.path) { - Ok(document) => StoreState::Ready(document), - Err(problem) => StoreState::Unavailable(problem), - }; - self.publish(state); + self.refresh(state, false) + } + + fn ensure_current(&self, state: &mut WriteGuard<'_>) -> Result { + self.refresh(state, true) + } + + // A file that cannot be read keeps the list for a read and refuses a write, which would land over unseen bytes. + fn refresh(&self, state: &mut StoreState, writing: bool) -> Result { + let path = self.inner.path.as_path(); + match read_bytes(path) { + Ok(found) => { + let unchanged = match &*state { + StoreState::Ready(_, seen) | StoreState::Unavailable(_, Some(seen)) => *seen == found, + StoreState::Unloaded | StoreState::Unavailable(_, None) => false, + }; + if !unchanged { + let decoded = decode(path, found.as_deref()); + *state = match decoded { + Ok(document) => StoreState::Ready(document, found), + Err(problem) => StoreState::Unavailable(problem, Some(found)), + }; + self.publish(state); + } + } + Err(source) => { + if let StoreState::Ready(document, _) = &*state { + if writing { + return Err(StorageError::Io { + path: path.to_owned(), + source, + }); + } + return Ok(document.clone()); + } + let problem = DocumentProblem::new( + path, + DocumentProblemKind::Unreadable { + detail: source.to_string(), + }, + ); + if !matches!(&*state, StoreState::Unavailable(held, None) if *held == problem) { + *state = StoreState::Unavailable(problem, None); + self.publish(state); + } + } } match state { - StoreState::Ready(document) => Ok(document.clone()), - StoreState::Unavailable(problem) => Err(StorageError::DocumentUnavailable(problem.clone())), + StoreState::Ready(document, _) => Ok(document.clone()), + StoreState::Unavailable(problem, _) => Err(StorageError::DocumentUnavailable(problem.clone())), StoreState::Unloaded => unreachable!("just loaded"), } } - /// A change that changes nothing writes nothing. + // A change that changes nothing writes nothing. fn edit( &self, change: impl FnOnce(&mut ConnectionsDocument) -> Result, ) -> Result { - let mut state = self.lock(); - let before = self.ensure_loaded(&mut state)?; + let mut state = self.lock_to_write(); + let before = self.ensure_current(&mut state)?; let mut document = before.clone(); let outcome = change(&mut document)?; document.tidy(); @@ -808,9 +874,8 @@ impl ConnectionStore { Ok(outcome) } - /// Write first, then adopt. A failed write leaves both the file and - /// the cached list exactly as they were. - fn commit(&self, state: &mut StoreState, mut document: ConnectionsDocument) -> Result<(), StorageError> { + // Write first, then adopt, so a failed write leaves the file and the cache as they were. + fn commit(&self, state: &mut WriteGuard<'_>, mut document: ConnectionsDocument) -> Result<(), StorageError> { // A connection saved with a tag deleted meanwhile drops it here. document.tidy(); let bytes = crate::document::encode_document(&document)?; @@ -820,7 +885,7 @@ impl ConnectionStore { let kept = document.connections.iter().map(|saved| saved.id.to_string()).collect(); crate::upgrade_copies::trim_connection_copies_blocking(&self.inner.path, &kept)?; crate::fs::write_private_blocking(&self.inner.path, &bytes)?; - *state = StoreState::Ready(document); + **state = StoreState::Ready(document, Some(bytes)); self.publish(state); Ok(()) } @@ -828,8 +893,8 @@ impl ConnectionStore { fn publish(&self, state: &StoreState) { let next = match state { StoreState::Unloaded => ConnectionListState::Loading, - StoreState::Ready(document) => ConnectionListState::Ready(document.list()), - StoreState::Unavailable(problem) => ConnectionListState::Unavailable(Arc::new(problem.clone())), + StoreState::Ready(document, _) => ConnectionListState::Ready(document.list()), + StoreState::Unavailable(problem, _) => ConnectionListState::Unavailable(Arc::new(problem.clone())), }; let revision = self.inner.snapshot.borrow().revision + 1; // send() drops the value when no receiver is alive, which would @@ -848,24 +913,20 @@ impl std::fmt::Debug for ConnectionStore { } } -fn read_document(path: &Path) -> Result { - let bytes = match std::fs::read(path) { - Ok(bytes) => bytes, - // No file is an empty list, not a problem: a first run has none. - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(ConnectionsDocument::default()), - Err(error) => { - return Err(DocumentProblem::new( - path, - DocumentProblemKind::Unreadable { - detail: error.to_string(), - }, - )); - } - }; - if bytes.iter().all(u8::is_ascii_whitespace) { - return Ok(ConnectionsDocument::default()); +fn read_bytes(path: &Path) -> std::io::Result { + match std::fs::read(path) { + Ok(bytes) => Ok(Some(bytes)), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(error) => Err(error), } - let mut document = crate::document::decode_document::(path, &bytes)?; +} + +// No file is an empty list, not a problem: a first run has none. +fn decode(path: &Path, bytes: Option<&[u8]>) -> Result { + let Some(bytes) = bytes.filter(|bytes| !bytes.iter().all(u8::is_ascii_whitespace)) else { + return Ok(ConnectionsDocument::default()); + }; + let mut document = crate::document::decode_document::(path, bytes)?; document.tidy(); Ok(document) } @@ -1292,6 +1353,123 @@ mod tests { assert_eq!(list.len(), 16, "an interleaved read-modify-write lost entries"); } + // Two stores on one root stand for two app processes. + #[test] + fn a_connect_keeps_a_connection_another_process_saved_after_this_one_read_the_list() { + let root = TempDir::new().expect("tempdir"); + let mine = store(&root); + let theirs = store(&root); + let opened = sample_connection(); + mine.upsert_blocking(opened.clone()).expect("upsert"); + let added = SavedConnection { + name: "Added elsewhere".to_owned(), + ..sample_connection() + }; + theirs.upsert_blocking(added.clone()).expect("their save"); + + mine.touch_last_opened_blocking(opened.id).expect("touch"); + + let saved = store(&root).load_blocking().expect("load"); + assert!( + saved.iter().any(|connection| connection.id == added.id), + "the connect wrote over the other process's save" + ); + let ConnectionListState::Ready(list) = mine.snapshot().state else { + panic!("expected Ready"); + }; + assert_eq!(list.connections.len(), 2, "the list read again was not published"); + } + + #[test] + fn two_stores_on_one_file_writing_at_once_lose_nothing() { + let root = TempDir::new().expect("tempdir"); + let stores = [store(&root), store(&root)]; + + let handles: Vec<_> = (0..16) + .map(|index| { + let store = stores[index % 2].clone(); + std::thread::spawn(move || { + let mut connection = sample_connection(); + connection.name = format!("connection {index}"); + store.upsert_blocking(connection).expect("upsert"); + }) + }) + .collect(); + for handle in handles { + handle.join().expect("join"); + } + + assert_eq!( + store(&root).load_blocking().expect("load").len(), + 16, + "one store's write went over the other's" + ); + } + + #[test] + fn a_save_goes_ahead_when_the_lock_file_cannot_be_opened() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + let mut lock = store.path().as_os_str().to_owned(); + lock.push(".lock"); + std::fs::create_dir_all(PathBuf::from(lock)).expect("a directory in its place"); + + store.upsert_blocking(sample_connection()).expect("the save"); + + assert_eq!(store.load_blocking().expect("load").len(), 1); + } + + #[test] + fn a_file_broken_by_hand_after_the_list_was_read_is_never_written_over() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + store.upsert_blocking(sample_connection()).expect("upsert"); + std::fs::write(store.path(), b"{ half an edit").expect("the hand edit"); + + let refused = store.upsert_blocking(sample_connection()); + + assert!( + matches!(refused, Err(StorageError::DocumentUnavailable(_))), + "{refused:?}" + ); + assert_eq!(std::fs::read(store.path()).expect("read"), b"{ half an edit"); + assert!(matches!(store.snapshot().state, ConnectionListState::Unavailable(_))); + } + + #[test] + fn a_list_that_cannot_be_read_again_still_shows_and_refuses_writes() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + store.upsert_blocking(sample_connection()).expect("upsert"); + let revision = store.snapshot().revision; + std::fs::remove_file(store.path()).expect("remove"); + std::fs::create_dir(store.path()).expect("a directory in its place"); + + assert_eq!(store.load_blocking().expect("the list read before").len(), 1); + let refused = store.upsert_blocking(sample_connection()); + + assert!(matches!(refused, Err(StorageError::Io { .. })), "{refused:?}"); + assert_eq!(store.snapshot().revision, revision); + } + + #[test] + fn a_reset_another_process_already_made_moves_nothing_aside() { + let root = TempDir::new().expect("tempdir"); + seed(&root, "not json at all"); + let mine = store(&root); + let theirs = store(&root); + mine.load_blocking().expect_err("unavailable"); + theirs + .reset_unreadable_blocking(std::time::SystemTime::now()) + .expect("their reset"); + theirs.upsert_blocking(sample_connection()).expect("their save"); + + let refused = mine.reset_unreadable_blocking(std::time::SystemTime::now()); + + assert!(refused.is_err(), "{refused:?}"); + assert_eq!(mine.load_blocking().expect("the list").len(), 1); + } + #[test] fn touch_missing_id_does_not_write() { let root = TempDir::new().expect("tempdir"); diff --git a/core/crates/storage/src/fs.rs b/core/crates/storage/src/fs.rs index 9d0b8c75cc..49299acbd0 100644 --- a/core/crates/storage/src/fs.rs +++ b/core/crates/storage/src/fs.rs @@ -2,7 +2,7 @@ use std::fs::{DirBuilder, File, OpenOptions}; use std::io::{ErrorKind, Write}; #[cfg(unix)] use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt}; -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::error::StorageError; @@ -131,6 +131,37 @@ pub fn append_private_blocking(path: &Path, bytes: &[u8]) -> Result<(), StorageE file.sync_all().map_err(io_error) } +// A lock file the file system refuses (NFS without lockd, SMB) only warns: the caller's re-read still guards it. +pub(crate) fn lock_beside_blocking(path: &Path) -> Option { + let mut name = path.as_os_str().to_owned(); + name.push(".lock"); + let lock = PathBuf::from(name); + let opened = path + .parent() + .map_or(Ok(()), ensure_private_dir) + .map_err(|error| error.to_string()) + .and_then(|()| { + private_file_options() + .write(true) + .create(true) + .truncate(false) + .open(&lock) + .map_err(|error| error.to_string()) + }); + let file = match opened { + Ok(file) => file, + Err(error) => { + tracing::warn!(path = %lock.display(), %error, "no lock file; writing after a re-read alone"); + return None; + } + }; + if let Err(error) = file.lock() { + tracing::warn!(path = %lock.display(), %error, "the lock was refused; writing after a re-read alone"); + return None; + } + Some(file) +} + /// Rename a file out of the way, returning the name it now has. /// /// The caller shows that name, so the user can find the file again. The @@ -471,6 +502,43 @@ mod tests { assert_eq!(std::fs::read(&file).expect("read"), b"first\nsecond\n"); } + #[test] + fn a_held_lock_makes_a_second_holder_wait_until_it_is_dropped() { + let root = tempfile::tempdir().expect("tempdir"); + let list = root.path().join("config").join("list.json"); + let held = lock_beside_blocking(&list).expect("lock"); + let (taken, waited) = std::sync::mpsc::channel(); + + let second = { + let list = list.clone(); + std::thread::spawn(move || { + let lock = lock_beside_blocking(&list).expect("second lock"); + taken.send(()).expect("send"); + drop(lock); + }) + }; + + assert!( + waited.recv_timeout(std::time::Duration::from_millis(200)).is_err(), + "a second holder took the lock while it was held" + ); + drop(held); + waited + .recv_timeout(std::time::Duration::from_secs(10)) + .expect("the lock once it was dropped"); + second.join().expect("join"); + assert_eq!(mode_of(&root.path().join("config").join("list.json.lock")), 0o600); + } + + #[test] + fn a_lock_file_that_cannot_be_opened_is_no_lock_rather_than_an_error() { + let root = tempfile::tempdir().expect("tempdir"); + let list = root.path().join("list.json"); + std::fs::create_dir(root.path().join("list.json.lock")).expect("a directory in its place"); + + assert!(lock_beside_blocking(&list).is_none()); + } + #[test] fn create_private_file_if_missing_keeps_existing_contents() { let root = tempfile::tempdir().expect("tempdir"); diff --git a/core/crates/test-fixtures/src/clickhouse_fixture.rs b/core/crates/test-fixtures/src/clickhouse_fixture.rs index 5a9f7c7289..7c6784af8c 100644 --- a/core/crates/test-fixtures/src/clickhouse_fixture.rs +++ b/core/crates/test-fixtures/src/clickhouse_fixture.rs @@ -18,6 +18,9 @@ const HTTP_PORT: u16 = ClickHouseFixture::HTTP_PORT; const HTTPS_PORT: u16 = ClickHouseFixture::HTTPS_PORT; const PASSWORD_USER: &str = "tablepro"; const LIMITED_USER: &str = "tablepro_limited"; +const READONLY_USER: &str = "tablepro_readonly"; +const LENIENT_READONLY_USER: &str = "tablepro_readonly_lenient"; +const UNSYNCED_USER: &str = "tablepro_unsynced"; const DATABASE: &str = "tablepro"; const SERVICE: &str = "clickhouse"; const READY_TIMEOUT: Duration = Duration::from_secs(60); @@ -118,8 +121,30 @@ impl ClickHouseFixture { \n\ \n" ); + // The stock config turns on `settings_constraints_replace_previous`, without which a profile cannot name a + // setting `changeable_in_readonly`. + let profiles = format!( + "\n\ + <{READONLY_USER}>default1\n\ + <{LENIENT_READONLY_USER}>default1\ + \ + \ + \n\ + <{UNSYNCED_USER}>default0\ + \ + \n\ + \n" + ); + let own_profile = [READONLY_USER, LENIENT_READONLY_USER, UNSYNCED_USER].map(|name| { + format!( + "<{name}>{password}::/0\ + {name}default\n" + ) + }); + let own_profile = own_profile.concat(); let users = format!( - "\n\ + "{profiles}\n\ + {own_profile}\ <{PASSWORD_USER}>\n\ {password}\n\ ::/0\n\ @@ -191,6 +216,21 @@ impl ClickHouseFixture { FixtureCredentials::new(LIMITED_USER, &self.password) } + // A login whose profile sets `readonly=1` and lets it change no setting. + pub fn readonly_credentials(&self) -> FixtureCredentials { + FixtureCredentials::new(READONLY_USER, &self.password) + } + + // A login at `readonly=1` whose profile lets it change `output_format_json_quote_denormals` and `readonly` itself. + pub fn lenient_readonly_credentials(&self) -> FixtureCredentials { + FixtureCredentials::new(LENIENT_READONLY_USER, &self.password) + } + + // A login that may write, whose profile holds `mutations_sync` at 0. + pub fn unsynced_credentials(&self) -> FixtureCredentials { + FixtureCredentials::new(UNSYNCED_USER, &self.password) + } + pub fn certificate_credentials(&self) -> FixtureCredentials { FixtureCredentials::new(TestPki::CLIENT_COMMON_NAME, "") } diff --git a/linux/changelog.d/4176.fixed.1.md b/linux/changelog.d/4176.fixed.1.md new file mode 100644 index 0000000000..f6ab90f480 --- /dev/null +++ b/linux/changelog.d/4176.fixed.1.md @@ -0,0 +1 @@ +A change to connections.json made outside TablePro while it ran was undone on the next connect. diff --git a/linux/changelog.d/4176.fixed.md b/linux/changelog.d/4176.fixed.md new file mode 100644 index 0000000000..b2ba14ec10 --- /dev/null +++ b/linux/changelog.d/4176.fixed.md @@ -0,0 +1 @@ +A ClickHouse login whose profile sets readonly=1 could not connect. diff --git a/linux/changelog.d/4176.security.md b/linux/changelog.d/4176.security.md new file mode 100644 index 0000000000..3fa8c7390d --- /dev/null +++ b/linux/changelog.d/4176.security.md @@ -0,0 +1 @@ +While a connection was reconnecting, Safe Mode read SQL by PostgreSQL's rules and could miss a write to ask about. diff --git a/linux/crates/app/src/ui/editor/mod.rs b/linux/crates/app/src/ui/editor/mod.rs index fd70597bfe..40e27ec2d1 100644 --- a/linux/crates/app/src/ui/editor/mod.rs +++ b/linux/crates/app/src/ui/editor/mod.rs @@ -402,13 +402,13 @@ impl SqlEditor { } /// The grammar of the server the editor is pointed at, which decides - /// where statements begin and end. With no connection the split - /// still has to work, and PostgreSQL's is the strictest. + /// where statements begin and end, also while the window's link is + /// down and the tab's own connection still runs. With no session the + /// split still has to work, and PostgreSQL's is the strictest. fn grammar(&self) -> SqlGrammar { self.deps() .and_then(|deps| deps.session.get()) - .and_then(|session| session.current_dialect()) - .map_or(SqlGrammar::PostgreSql, |dialect| dialect.grammar()) + .map_or(SqlGrammar::PostgreSql, |session| session.grammar()) } /// A one-line message where the run summary goes. diff --git a/linux/docs/drivers/clickhouse.md b/linux/docs/drivers/clickhouse.md index 03d8631b09..92d9c6366b 100644 --- a/linux/docs/drivers/clickhouse.md +++ b/linux/docs/drivers/clickhouse.md @@ -10,7 +10,7 @@ HTTP has no login and no long-lived session, so there is no pool of logins: - hyper keeps idle HTTP connections for 2 s, shorter than the server's keep-alive (3 s before 23.11, 10 s after), so a reused connection is never one the server is about to close. - Every connection is a stream from `Transport::open`. The URL names only the service, for the `Host` header and the TLS name, so an SSH forward never needs a DNS lookup of a name only the far side can resolve. -- The connect is the first request, `SELECT version(), currentDatabase()` and the login's own `readonly` level from `system.settings`, on the control client, within the connect and login timeouts together. A refused password or a failed handshake shows up there. +- The connect is the first request, within the connect and login timeouts together. It carries no setting and asks for `version()`, `currentDatabase()`, the login's own `readonly` and `mutations_sync`, and which of the driver's settings `system.settings` lets this login change (`readonly = 0` there). A refused password or a failed handshake shows up there. - `probe` is one more request, `SELECT 1`, on the control client. Three clients share that connection pool: @@ -18,10 +18,10 @@ Three clients share that connection pool: | Client | Used for | Settings beyond the common ones | |---|---|---| | Workspace | Catalogue reads, pages, counts, exports, write probes, editor sessions | `readonly=1` and `allow_ddl=0` on a read-only connection | -| Writes | The statements of a save | `wait_end_of_query=1`, `max_execution_time=0` | +| Writes | The statements of a save | `wait_end_of_query=1`, and `max_execution_time=0` where the login may change it | | Control | `KILL QUERY`, `SYSTEM FLUSH LOGS`, commit resolution | None. It is not held read-only, because a `KILL` is not a read, and it runs only SQL the driver wrote. | -Every request carries these settings: +Every request carries these settings, each only where the login may change it: | Setting | Why | |---|---| @@ -33,6 +33,13 @@ Every request carries these settings: | `mutations_sync=1` | A save waits for its mutation, so the grid refetches what the save wrote. | | `cancel_http_readonly_queries_on_client_close=1` | A read whose HTTP connection the app dropped stops on the server even when no `KILL` got through. | +The server refuses a whole request that changes a setting the login may not change: any setting at `readonly=1` that the profile does not mark `changeable_in_readonly` (error 164), or one a profile constraint holds `const` (452). So a request carries only the settings the connect found changeable. An unchanged value is never checked, so a setting whose server default is already the driver's value (`date_time_output_format`, `http_write_exception_in_output_format`, `output_format_json_validate_utf8` on 26.x) holds either way. Without the others: + +- A Decimal is padded to its column's scale as it is read, so it reads the same as with `output_format_decimal_trailing_zeros`, except inside a Map, Tuple, Variant, Dynamic or JSON value, which keeps the server's text. +- The server writes NaN and infinity as null. In a Float column that is not Nullable, the cell reads as a value out of range, never as NULL. In a Nullable one it reads as NULL, since the server writes NULL the same way. +- A save with a mutation still carries `mutations_sync=1` when the login's own value is 0. The server then refuses the whole save (452, or 164 at `readonly=1`) before any of it runs. Without the setting, the server answers a mutation as soon as it is queued, and the save would read as done even if the mutation then failed. A login whose own value is 1 or 2 already waits, and an `INSERT` never needs to. +- A dropped read runs on until it ends or is killed; a stop still sends `KILL`. + The client names itself to the server as TablePro with the app's version (`with_product_info`). Results stream as `JSONCompactEachRowWithNamesAndTypes`, and each cell is read by the type the server named for it. Values go to the server as typed `{pN:Type}` parameters, never spliced into the SQL. SQL the user typed goes through `query_raw`, so a `?` in it stays a `?`. @@ -47,11 +54,11 @@ Results stream as `JSONCompactEachRowWithNamesAndTypes`, and each cell is read b ## Read-only -`ReadOnlyEnforcement::EngineEnforced`. A read-only connection sends `readonly=1` and `allow_ddl=0` with every workspace and editor request, and the server refuses a write whatever the SQL says (error 164, or 392 for DDL). `readonly=2` is not enough: it still lets `INSERT INTO FUNCTION file(...)` write a file on the server (measured on 26.8). Under `readonly=1` a `SET`, a `SETTINGS` clause or a temporary table is refused too; the settings the driver sends with a request still apply. A read of user SQL, such as an MCP read or an export of a query, carries `readonly=1` on any connection; the app's own reads carry `readonly=2`. A login whose profile already sets `readonly` refuses any change of it, so such a login keeps its own level and nothing is sent. Saves on a read-only connection are refused before anything is sent. +`ReadOnlyEnforcement::EngineEnforced`. A read-only connection sends `readonly=1` and `allow_ddl=0` with every workspace and editor request, and the server refuses a write whatever the SQL says (error 164, or 392 for DDL). `readonly=2` is not enough: it still lets `INSERT INTO FUNCTION file(...)` write a file on the server (measured on 26.8). Under `readonly=1` a `SET`, a `SETTINGS` clause or a temporary table is refused too; the settings the driver sends with a request still apply. A read of user SQL, such as an MCP read or an export of a query, carries `readonly=1` on any connection; the app's own reads carry `readonly=2`. A login whose profile sets `readonly=2` refuses any change of it, so such a login keeps its own level and no `readonly` is sent. A login at 1 gets `readonly=1` anyway: the server takes an unchanged value as no change, and the value puts back a session that ran `SET readonly = 0`, which a profile marking `readonly` `changeable_in_readonly` allows. It gets no `allow_ddl`, a change that level refuses and whose DDL `readonly=1` refuses already. A `SETTINGS readonly = 0` clause in user SQL is outside the driver's reach on such a profile. Saves on a read-only connection are refused before anything is sent. ## Stops and timeouts -A read carries `max_execution_time` (the nearer of the call's deadline and the workspace's 60 s, in whole seconds rounded up) with `timeout_overflow_mode=throw`, so the server stops it on time on its own (error 159). +A read carries `max_execution_time` (the nearer of the call's deadline and the workspace's 60 s, in whole seconds rounded up) with `timeout_overflow_mode=throw`, so the server stops it on time on its own (error 159). A login that may not change both gets neither, since a bound with a profile's `timeout_overflow_mode=break` would end a read early with partial rows; the workspace's bound then becomes the call's deadline, which stops the read with a `KILL`. A stop is `KILL QUERY WHERE query_id = ? ASYNC` on the control client. Each query the driver sends carries a query id of its own, and the id is bound by the client, never written into the SQL. The `KILL` names that one query, so a late one cannot hit the next: `StopDelivery::OutOfBand(StopScope::Statement)`. The server answers the stopped query with error 394, which reads as `Cancelled`, or as `Timeout { server_cancelled: true }` when the deadline asked for it. @@ -107,7 +114,7 @@ The query log and the process list are each node's own, and the driver asks the - An editor session is the workspace client with a `session_id` of its own and `session_timeout=3600`, so the server keeps the session's settings and temporary tables between runs. Opening it asks `SELECT 1` in the session, and every run after that sends `session_check=1`, so a session the server let expire fails with 372 instead of starting an empty one. The server runs one query at a time per session, so a second run is `EditorSessionBusy`. - One statement per run (`ScriptExecution::SingleStatement`), each with a new query id. -- The run's deadline, or the session's statement timeout, goes to the server as `max_execution_time` with `timeout_overflow_mode=throw`. A stop sends `KILL QUERY` and keeps reading, because the server answers the `KILL` by ending the response; a server that has not answered within the cancel grace is cut off. +- The run's deadline, or the session's statement timeout, goes to the server as `max_execution_time` with `timeout_overflow_mode=throw` where the login may change both; the run stops itself at it either way. A stop sends `KILL QUERY` and keeps reading, because the server answers the `KILL` by ending the response; a server that has not answered within the cancel grace is cut off. - Past the row limit the driver sends `KILL QUERY` for the rest of the result rather than reading it. - The server keeps a session locked until a query it was told to stop has ended, which can be after the run gave up its response, and refuses the session's next query with 373 until then. So when a stopped run gave up its response before the end, as one cut off at the cancel grace, past the row limit or at a row it could not read does, the next run first sends `KILL QUERY ... SYNC` for that query, which answers once it is gone, and waits under its own cancel and deadline, or for the cancel grace at most when it has neither a deadline nor a statement timeout. A stop the server answered by ending the response leaves nothing to wait for. A 373 that still comes is `EditorSessionBusy`, and the session stays ready. - ClickHouse has no transaction an editor statement can leave open, so the transaction state is always `Idle`. diff --git a/linux/docs/storage.md b/linux/docs/storage.md index 55ab40d06d..f190f695be 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -20,11 +20,11 @@ The app's data goes to the store its shape calls for. The core's `storage` crate `ConnectionStore` holds the saved-connection list, one store per storage root: -- `load_blocking`, `list_blocking`, `upsert_blocking`, `insert_after_blocking`, `update_blocking`, `remove_blocking`, `touch_last_opened_blocking`, the group and favourite operations below, `preview_import_blocking`, `apply_import_blocking` and `export_blocking` run off the GTK thread. A change holds the store's lock across read, change and write, so two writers never lose an entry. `update_blocking` changes one entry as the list holds it at the write, so a last-opened stamp from another window in between is kept. +- `load_blocking`, `list_blocking`, `upsert_blocking`, `insert_after_blocking`, `update_blocking`, `remove_blocking`, `touch_last_opened_blocking`, the group and favourite operations below, `preview_import_blocking`, `apply_import_blocking` and `export_blocking` run off the GTK thread. A change holds the store's lock and an exclusive lock on `connections.json.lock` beside the list across read, change and write, and reads the file again first, so neither another thread nor another process on the same root (a second app, a hand edit, a sync tool) loses an entry to a stale copy. A lock file that cannot be made or locked (NFS without lockd, SMB) is logged and the write goes ahead on the re-read alone. A read also reads the file again, keeps the list it has when the file cannot be read, and publishes a list it finds changed. `update_blocking` changes one entry as the list holds it at the write, so a last-opened stamp from another window in between is kept. - A write goes through `write_document_blocking` first and the cached list changes only once it landed, so a failed write leaves both as they were. The first write over a file an older build wrote keeps a copy of it first ([Copies before a format upgrade](#copies-before-a-format-upgrade)). - A file that another version wrote in a newer format is refused; one in the same format is read and written whichever build wrote it. - `subscribe` hands out a watch of `ConnectionListSnapshot`, so every picker sees every writer's change. -- A file that cannot be read, or that holds a format newer than this build's, leaves the store `Unavailable`, and it refuses every write until the user picks Reset (`reset_unreadable_blocking`), which moves the file aside. +- A file that cannot be read, or that holds a format newer than this build's, leaves the store `Unavailable`, and it refuses every write until the user picks Reset (`reset_unreadable_blocking`), which moves the file aside. A file broken after the list was read is found by the next change, which refuses it the same way; a file that cannot be read at all then fails the change with `Io` and leaves the list shown. Reset reads the file first and refuses when another process already reset or rewrote it. - A `SavedConnection` does **not** carry a password. Secrets live in the keyring, keyed by the connection's UUID, so a rename or a new host keeps them. - `safe_mode` is the connection's Safe Mode level: `off`, `confirm_writes` or `read_only`. It has no default, since guessing Off would drop a guard. - `external_access` is what MCP clients may do on the connection: `blocked`, `read_only` or `read_write` ([ADR 0022](decisions/0022-ai-assistant-and-mcp-server.md)). A new connection starts at `read_only`. It has no default either. diff --git a/windows/docs/decisions/0004-app-shell.md b/windows/docs/decisions/0004-app-shell.md index 2c9b6abeab..d98cc64c58 100644 --- a/windows/docs/decisions/0004-app-shell.md +++ b/windows/docs/decisions/0004-app-shell.md @@ -117,11 +117,12 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - A header click on a table tab runs the table's query again with the sort: `build_page` writes the ORDER BY, with the row key as the tiebreak, so it orders the whole table, not the rows already read. A row address the engine adds for edits (SQLite's rowid, PostgreSQL's ctid) follows the visible columns and is not shown. An engine that pages in its own order refuses the sort, and the grid keeps the shown one. - A header click on a query result does what the macOS app does: the result's statement runs again with the column's ORDER BY in place of its own and before its row limit (`DatabaseSession.SortedQuery`, the core's `with_order_by`). The ORDER BY names the column, or gives its position where a name is not unique or not a plain column (core ADR 0007). Only one statement that reads runs again. For a script, a write, SHOW or EXPLAIN, and where the engine takes no position, the core sorts the rows the result holds (`ResultSet.SortedAsync`), as macOS sorts them where they are, and the status says so. - The object pane resizes with the Windows Community Toolkit's `GridSplitter` (Sizers), set up as its [page](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/windows/sizers/gridsplitter) lays it out, between 180 and 640 effective pixels; WinUI's `SplitView` has no user resize. Every workspace shares the width, which `session.json` keeps. -- Reopen closed tab (File menu, Ctrl+Shift+T, which the [Tab view](https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/tab-view) page asks for): a workspace keeps its last 20 closed tabs, newest first, each with its text or its table and its place in the strip. Disconnecting forgets them. +- Reopen closed tab (File menu, Ctrl+Shift+T, which the [Tab view](https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/tab-view) page asks for): a workspace keeps its last 20 closed tabs, newest first, each with its text or its table and its place in the strip. Disconnecting forgets them; Reconnect and connecting an edited connection keep them. - Table tabs page as macOS pages a table: a page is the default page size long (Settings, Pagination: 100, 500, 1,000, 5,000 or 10,000 rows, 1,000 unless changed, as macOS); First, Previous, Next and Last sit in the tab's `CommandBar`, and its one status words the page as the macOS status bar does, with the run's time: "1,001-2,000 of 3,503 rows · 15 ms", or "Rows 1,001-2,000 · 15 ms" until the count lands. A table that fits one page keeps "Rows: 347 · 12 ms". The core writes each page (`table_query` takes an offset, over `build_page`'s `PagePosition::Offset`) and counts the table (`table_row_count`, `build_count` as the Linux pager counts), in the background after the first full page; until the count lands, Next stays while pages come back full. A sort or a refresh goes back to the first page, and a refresh counts again. An engine that pages on the server (Cassandra) cannot seek by offset, so it has only its first page, as on macOS, and the core refuses another (`ffi.page_unsupported`). - Rows per page: a "1,000 rows" button before First page opens the macOS sizes, 5, 10, 20, 100, 500 and 1,000, as `RadioMenuFlyoutItem`s, then All rows..., which asks first and needs the count, and Custom..., up to 1,000,000. A new size keeps the page that holds the first row shown, as macOS does. The size belongs to the tab: `session.json` keeps it with the tab's page, and Reopen closed tab brings both back. - Go to page: between Previous and Next, "Page 3 of 36" opens a flyout with a `NumberBox` from 1 to the page count, as macOS asks in a popover. It is offered once the table is counted, as on macOS, and the box keeps a typed number inside that range (`InvalidInputOverwritten`). -- An open transaction: each run's `Finished` event carries the transaction the editor's connection is left in (the core's `RunSummary.transaction_state`). The tab that holds one shows a Warning `InfoBar` above its results, "Transaction open", with Roll back, which asks first, and Commit, hidden on a read-only connection; a failed one is an Error bar with Roll back only. Both send COMMIT or ROLLBACK as typed SQL, EXEC or DISCARD for a key-value engine, as the Linux app does. Over the tabs, a bar names another tab that holds one, with Show; macOS shows its bar above every tab of the connection, and here the owning tab carries the actions, since the transaction lives on that tab's own connection. Closing a tab that holds one asks first, since closing rolls it back. Window close and Exit confirm every affected held transaction before closing any tabs; cancelling keeps them open. A window with a dialog already open waits for it to be dismissed before accepting a close request. +- An open transaction: each run's `Finished` event carries the transaction the editor's connection is left in (the core's `RunSummary.transaction_state`). The tab that holds one shows a Warning `InfoBar` above its results, "Transaction open", with Roll back, which asks first, and Commit, hidden on a read-only connection; a failed one is an Error bar with Roll back only. Both send COMMIT or ROLLBACK as typed SQL, EXEC or DISCARD for a key-value engine, as the Linux app does. Over the tabs, a bar names another tab that holds one, with Show; macOS shows its bar above every tab of the connection, and here the owning tab carries the actions, since the transaction lives on that tab's own connection. Closing a tab that holds one asks first, since closing rolls it back. Window close and Exit confirm every affected held transaction before closing any tabs; cancelling keeps them open. Disconnect, Reconnect and connecting an edited connection ask the same way first, and Reconnect and an edited connection bring the tabs back with their text on the new session. Cancelling that connect keeps the connection with Retry, so the tabs still come back. A window with a dialog already open waits for it to be dismissed before accepting a close request. WinUI opens one `ContentDialog` per thread, even across windows, so a question asked while another window shows one is declined, as Cancel would decline it (`WindowDialogs`). +- Safe Mode: before a typed run the core judges each statement at the connection's level (`DatabaseSession.gate_run`, over `SafeModeLevel::gate`, as the Linux editor judges its batches). Confirm writes asks before any write, and Off before a DROP, a TRUNCATE, or a DELETE or UPDATE with no WHERE. The question is a `ContentDialog` that lists each statement in full, with Cancel as the default. Read only keeps the whole run back, since the editor sends its text whole. The driver splits that text with the session's own settings, so when the whole text reads stricter than every statement, the question shows the whole text. A MySQL script with DELIMITER lines is judged statement by statement, since the driver sends its statements apart. A level saved while the connection is open applies at once to its sessions in every window (`set_safe_mode`), as on Linux; a change to or from Read only takes a new connection, so those sessions reconnect once their held transactions are confirmed. A session that keeps its connection shows "Changes apply the next time you connect", as Linux says it, and one that cannot take Read only asks before every write until then. Statements are judged by the connection's own engine, also while its link is down and a tab's own connection still runs (`Session::grammar`). Commit, Roll back and a sort's rerun are not asked about. Closing a tab stops its run and waits for it to end before its connection closes. - Reconnecting: the workspace waits on the core's session health (`health_change`) and shows a bar over the tabs while it is not online: Warning while it reconnects, with the attempt, or while the server cannot be reached, and Error once the session parks with the reason the core gives, with Reconnect (`reconnect`, the monitor's retry). An editor connection is never opened again by itself, since its transaction would not come back with it: a query tab whose connection was lost opens a new one on its next run and says that an open transaction went with it. ### Settings @@ -139,6 +140,8 @@ A page in the window, built from `SettingsCard` and `SettingsExpander`: Appearan The data folder is the package's `ApplicationData.Current.LocalFolder` (`%LOCALAPPDATA%\Packages\\LocalState`), which Windows removes with the app ([Store and retrieve settings and other app data](https://learn.microsoft.com/en-us/windows/apps/design/app-settings/store-and-retrieve-app-data)). An unpackaged development build uses `%LOCALAPPDATA%\TablePro`, and `TABLEPRO_DATA_DIR` overrides both for the UI tests. The session holds each open connection (a saved one by id, a file by path), its tabs (a query tab's text, a table tab's table with its page size and page), the selected tab, the front connection, and each tab window with its connection, its tabs and its bounds. +One process owns a data folder. `Program.Main` registers it with `AppInstance.FindOrRegisterForKey` under a hash of its path (`InstanceKey`) before XAML starts, so a second launch on the same folder redirects its activation to the running app and ends, and the running app brings its primary window to the front ([Create a single-instanced app](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/applifecycle/applifecycle-single-instance)). `RedirectActivationToAsync` waits for the owner without a limit, so the launch also stops waiting when the owner's process ends or after 5 seconds, then looks again or starts its own window (`LaunchClaim`); a launch always ends with a window. The primary window gives the key up (`UnregisterKey`) when it closes, after the session is saved, and a failure anywhere in the guard is written to `crash.log` and the app starts as usual. Two processes would otherwise each write their copy of `session.json` and `settings.json` over the other's. The core also locks `connections.json` for each write and reads it again first, which covers a writer outside the app. + ### Strings and accessibility - Every string is in `Strings/en-US/Resources.resw`: XAML through `x:Uid`, code through `ResourceLoader`. The core's error codes are worded as `Error_` with dots as underscores; the innermost code with wording wins, and the server's own message follows it. @@ -177,7 +180,7 @@ Facts the CI runs on `windows-2025` and `windows-11-arm` established, which the ## Consequences - The core gained the library and connecting through it: `Core::library`, `connect_saved`, `test_connection`, `parse_connection_url`, `DatabaseSession::table_query` and `sorted_query` with `sql_syntax::with_order_by` and `sort_reference`, `ResultSet::sorted` over `value::order`, and `Core::with_system_secrets`, which keeps secrets in Credential Manager on Windows (`core/ARCHITECTURE.md`, Platforms). -- Then what the shell's second pass needed: `DatabaseDriver::category` (`EngineCategory`) and `DriverInfo.category`; in the library, tags (`create_tag`, `update_tag`, `delete_tag`, `tag_connections`, and the tags in the snapshot), `rename_connection`, `add_favorites` and `remove_favorites` with a place, `move_to_group` with a place and `move_groups`; `ErrorInfo.secret`; `table_query` with an offset and `can_seek`, `table_row_count`; `health`, `health_change` and `reconnect` on the session; `QueryEditor::is_lost`; and the transaction in `RunEvent::Finished`. Each has its test in `core/crates/ffi/tests`. +- Then what the shell's second pass needed: `DatabaseDriver::category` (`EngineCategory`) and `DriverInfo.category`; in the library, tags (`create_tag`, `update_tag`, `delete_tag`, `tag_connections`, and the tags in the snapshot), `rename_connection`, `add_favorites` and `remove_favorites` with a place, `move_to_group` with a place and `move_groups`; `ErrorInfo.secret`; `table_query` with an offset and `can_seek`, `table_row_count`; `health`, `health_change` and `reconnect` on the session; `QueryEditor::is_lost`; the transaction in `RunEvent::Finished`; and `gate_run` and `set_safe_mode` on the session. Each has its test in `core/crates/ffi/tests`. - `tests/TablePro.App.Model.Tests` covers settings, the session file with the window, the pane and the tab windows, the library tree with tags and search, selection by connection, drops, the engine families, closed tabs, the pager with its page sizes and Go to page, and the workspace bars, a run's messages, the form's fields and the object folders on any machine. `tests/TablePro.App.UITests` drives the unpackaged build through UI Automation (FlaUI): first run, Settings, the form and its engine families, connecting to a SQLite file, opening, sorting and paging a table with a width set through Transform and the header's ItemStatus, a table's page size and Go to page kept across a relaunch, a tab moved to its own window that comes back with its place and merges back by drag, a tab dragged out whose window closes with its last tab, a query and its sort, a script's rows sorted where they are, a failed connect, a password asked for, session restore, the rail's mark, the pane's width and the window's place across a relaunch, Reopen closed tab, an open transaction's bars, a PostgreSQL notice under Messages, and rename, tags, a drop and selection mode on the Connections page, in light and dark, with screenshots. - SSH connections use the core's in-process transport (core ADR 0009). Host-key, passphrase and interactive login prompts use native ContentDialogs. A changed host key requires confirmation to remove its old entry, then separate confirmation of the replacement key on retry. - Not built yet: importing from other apps (the importers move to the core first). diff --git a/windows/src/TablePro.App.Model/InstanceKey.cs b/windows/src/TablePro.App.Model/InstanceKey.cs new file mode 100644 index 0000000000..e49479638a --- /dev/null +++ b/windows/src/TablePro.App.Model/InstanceKey.cs @@ -0,0 +1,16 @@ +using System.Security.Cryptography; +using System.Text; + +namespace TablePro.App.Model; + +// Hashed, so a folder of any length or spelling gives a short key of plain characters. +public static class InstanceKey +{ + public static string For(string dataFolder) + { + ArgumentException.ThrowIfNullOrWhiteSpace(dataFolder); + // Windows names one folder in any case, with or without a trailing separator. + var folder = dataFolder.TrimEnd('\\', '/').ToUpperInvariant(); + return "TablePro-" + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(folder))); + } +} diff --git a/windows/src/TablePro.App.Model/LaunchClaim.cs b/windows/src/TablePro.App.Model/LaunchClaim.cs new file mode 100644 index 0000000000..473aa0c0a8 --- /dev/null +++ b/windows/src/TablePro.App.Model/LaunchClaim.cs @@ -0,0 +1,70 @@ +namespace TablePro.App.Model; + +public enum HandOverResult +{ + Taken, + OwnerExited, + NoAnswer, +} + +// A launch must always end with a window somewhere: in the app it handed over to, or its own. +public static class LaunchClaim +{ + public const int Attempts = 3; + + // findOwner gives null once this process holds the data folder's key, or the process that holds it. + public static bool StartsHere( + Func findOwner, Func handOver, Action failed) + where TOwner : class + { + ArgumentNullException.ThrowIfNull(findOwner); + ArgumentNullException.ThrowIfNull(handOver); + ArgumentNullException.ThrowIfNull(failed); + try + { + for (var attempt = 0; attempt < Attempts; attempt++) + { + if (findOwner() is not { } owner) + { + return true; + } + + switch (handOver(owner)) + { + case HandOverResult.Taken: + return false; + case HandOverResult.NoAnswer: + return true; + case HandOverResult.OwnerExited: + break; + } + } + } + catch (Exception exception) + { + failed(exception); + } + + return true; + } + + // The lookup gives null when the owner gave the key up between registering and finding, as one that quits does. + public static TOwner? OwnerOf(TOwner? found, Func isCurrent) + where TOwner : class + { + ArgumentNullException.ThrowIfNull(isCurrent); + return found is null || isCurrent(found) ? null : found; + } + + // The wait ends on the redirect, the owner's exit or the timeout; only a redirect that finished means it was taken. + public static HandOverResult ReadWait(bool timedOut, Task redirect) + { + ArgumentNullException.ThrowIfNull(redirect); + if (timedOut) + { + return HandOverResult.NoAnswer; + } + + return redirect.IsCompletedSuccessfully ? HandOverResult.Taken : HandOverResult.OwnerExited; + } +} diff --git a/windows/src/TablePro.App.Model/TabWork.cs b/windows/src/TablePro.App.Model/TabWork.cs new file mode 100644 index 0000000000..d1795f1e94 --- /dev/null +++ b/windows/src/TablePro.App.Model/TabWork.cs @@ -0,0 +1,90 @@ +namespace TablePro.App.Model; + +// What a tab has in flight. Closing cancels each piece and waits for it to end, so the tab lets go of its run, +// its editor and its session only once nothing still uses them. +public sealed class TabWork +{ + private readonly Lock _lock = new(); + private readonly Dictionary _running = []; + private bool _closed; + + public bool IsClosed + { + get + { + lock (_lock) + { + return _closed; + } + } + } + + // False when the tab closed first, or while it ran: a cancel the close caused ends the work without an exception. + public async Task RunAsync(Func work) + { + ArgumentNullException.ThrowIfNull(work); + using var cancel = new CancellationTokenSource(); + var ended = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + if (!Add(ended.Task, cancel)) + { + return false; + } + + try + { + await work(cancel.Token).ConfigureAwait(true); + } + catch (OperationCanceledException) when (IsClosed) + { + // Nothing is left to show what the work would have. + } + finally + { + // Out of the list before its token is disposed, so a close never cancels a disposed one. + Remove(ended.Task); + ended.SetResult(); + } + + return !IsClosed; + } + + public Task CloseAsync() + { + lock (_lock) + { + _closed = true; + foreach (var (ended, cancel) in _running.ToList()) + { + // A work the cancel ended at once has left the list and disposed its token. + if (_running.ContainsKey(ended)) + { + cancel.Cancel(); + } + } + + return Task.WhenAll(_running.Keys.ToList()); + } + } + + private bool Add(Task ended, CancellationTokenSource cancel) + { + lock (_lock) + { + if (_closed) + { + return false; + } + + _running.Add(ended, cancel); + return true; + } + } + + private void Remove(Task ended) + { + lock (_lock) + { + _running.Remove(ended); + } + } +} diff --git a/windows/src/TablePro.App/App.xaml.cs b/windows/src/TablePro.App/App.xaml.cs index 0a41b8fe99..a5f524762e 100644 --- a/windows/src/TablePro.App/App.xaml.cs +++ b/windows/src/TablePro.App/App.xaml.cs @@ -8,6 +8,7 @@ namespace TablePro.App; public partial class App : Application { private MainWindow? _window; + private bool _closed; public App() { @@ -22,8 +23,40 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) Directory.CreateDirectory(folder); var core = TableProCore.CreateWithSystemSecrets(new HostConfiguration(AppStorage.Version, folder)); _window = new MainWindow(new Workbench(core, folder)); + _window.Closed += (_, _) => ReleaseFolder(); _window.ActivateRestored(); // Off the first grid's path: the fallback fonts for non-Latin cells load in the background. DataGridView.WarmUpFonts(); } + + internal void BringToFront() + { + if (_window is null || _closed) + { + return; + } + + try + { + SingleInstance.BringToFront(_window); + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + } + + // The session is saved by now, so a launch from here on starts its own window instead of handing over to this one. + private void ReleaseFolder() + { + _closed = true; + try + { + SingleInstance.Release(); + } + catch (Exception exception) + { + CrashLog.Write(exception); + } + } } diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index 541e5e3c0e..d5d233fbb9 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -575,6 +575,11 @@ private async Task CloseConnectionAsync(OpenConnection open) return; } + if (!await ConfirmDisconnectAsync(open)) + { + return; + } + DropSpare(); var wasShown = ReferenceEquals(Rail.Content, open.View); if (wasShown) @@ -648,20 +653,111 @@ private async Task OnFormCompletedAsync(FormResult result) open.Rename(result.Saved.Name); } + // An edited connection that is open connects again with what was saved, and keeps its tabs. + var reconnecting = result.Connect && open is { State: ConnectionState.Connected or ConnectionState.Failed } + ? open + : null; + await ApplySafeModeAsync(result.Saved, reconnecting); if (!result.Connect) { return; } - // An edited connection that is open connects again with what was saved. - if (open is not null && open.State is ConnectionState.Connected or ConnectionState.Failed) + if (reconnecting is not null) { - await open.DisconnectAsync(); + if (!await ConfirmDisconnectAsync(reconnecting)) + { + ShowChangesOnConnect(reconnecting, result.Saved); + return; + } + + await reconnecting.DisconnectAsync(keepTabs: true); } Connect(result.Saved); } + // A saved level reaches every window's open sessions of the connection at once, as on Linux. Read only is set + // as a connection opens, so a change across it reconnects once held transactions may go, or waits for the next + // connect. + private async Task ApplySafeModeAsync(SavedConnection saved, OpenConnection? reconnecting) + { + var crossed = new List<(MainWindow Window, OpenConnection Open)>(); + List windows = [this, .. _tabWindows]; + foreach (var window in windows) + { + foreach (var each in window._open.Where(item => item.Matches(saved.Id, null))) + { + if (each.Session is not { } session) + { + continue; + } + + if (session.SetSafeMode(saved.SafeMode)) + { + each.View.HideChangesOnConnect(); + continue; + } + + // Until a new connection opens read only, every write on this one is asked about. + if (saved.SafeMode == SafeMode.ReadOnly) + { + _ = session.SetSafeMode(SafeMode.ConfirmWrites); + } + + if (!ReferenceEquals(each, reconnecting)) + { + crossed.Add((window, each)); + } + } + } + + foreach (var (window, each) in crossed) + { + if (each.State != ConnectionState.Connected) + { + continue; + } + + if (await window.ConfirmDisconnectAsync(each)) + { + DropSpare(); + await each.DisconnectAsync(keepTabs: true); + await each.ConnectAsync(); + } + else + { + ShowChangesOnConnect(each, saved); + } + } + } + + private static void ShowChangesOnConnect(OpenConnection open, SavedConnection saved) => + open.View.ShowChangesOnConnect(saved.SafeMode == SafeMode.ReadOnly && open.Session is { IsReadOnly: false }); + + // Disconnecting ends each tab's own connection, so a held transaction is asked about first, as closing its tab + // asks. The connection shows first, since the question names one of its tabs. + private async Task ConfirmDisconnectAsync(OpenConnection open) + { + var confirmed = new HashSet(); + if (!open.View.HasUnconfirmedTransactions(confirmed)) + { + return true; + } + + Activate(); + if (IsPrimary) + { + Select(open); + } + else + { + Show(open); + } + + return await open.View.ConfirmCloseAsync(confirmed); + } + private void OnRailSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { if (_home is null) @@ -1003,10 +1099,10 @@ private async void OnEditConnection(object sender, RoutedEventArgs e) private async void OnReconnect(object sender, RoutedEventArgs e) { - if (_front is { } open) + if (_front is { } open && await ConfirmDisconnectAsync(open)) { _primary.DropSpare(); - await open.DisconnectAsync(); + await open.DisconnectAsync(keepTabs: true); await open.ConnectAsync(); } } diff --git a/windows/src/TablePro.App/Program.cs b/windows/src/TablePro.App/Program.cs new file mode 100644 index 0000000000..e675444189 --- /dev/null +++ b/windows/src/TablePro.App/Program.cs @@ -0,0 +1,51 @@ +using Microsoft.UI.Dispatching; +using Microsoft.UI.Xaml; +using Microsoft.Windows.AppLifecycle; +using TablePro.App.Model; +using TablePro.App.Services; + +namespace TablePro.App; + +// In place of the XAML compiler's Main, so a second launch on a data folder hands over before any window opens. +internal static class Program +{ + [STAThread] + private static void Main() + { + WinRT.ComWrappersSupport.InitializeComWrappers(); + var starts = LaunchClaim.StartsHere( + () => SingleInstance.FindOwner(InstanceKey.For(AppStorage.DataFolder)), + SingleInstance.HandOver, + CrashLog.Write); + if (!starts) + { + return; + } + + AppInstance? current = null; + Application.Start(_ => + { + var queue = DispatcherQueue.GetForCurrentThread(); + SynchronizationContext.SetSynchronizationContext(new DispatcherQueueSynchronizationContext(queue)); + var app = new App(); + current = Listen(app, queue); + }); + GC.KeepAlive(current); + } + + // Raised off the UI thread, when another launch on this data folder hands over. + private static AppInstance? Listen(App app, DispatcherQueue queue) + { + try + { + var current = AppInstance.GetCurrent(); + current.Activated += (_, _) => queue.TryEnqueue(app.BringToFront); + return current; + } + catch (Exception exception) + { + CrashLog.Write(exception); + return null; + } + } +} diff --git a/windows/src/TablePro.App/Services/SingleInstance.cs b/windows/src/TablePro.App/Services/SingleInstance.cs new file mode 100644 index 0000000000..e7fad8cbf4 --- /dev/null +++ b/windows/src/TablePro.App/Services/SingleInstance.cs @@ -0,0 +1,91 @@ +using System.Runtime.InteropServices; +using Microsoft.UI.Xaml; +using Microsoft.Win32.SafeHandles; +using Microsoft.Windows.AppLifecycle; +using TablePro.App.Model; +using WinRT.Interop; + +namespace TablePro.App.Services; + +// Two processes on one data folder would each write their copy of the session and settings over the other's. +internal static partial class SingleInstance +{ + private const uint Synchronize = 0x00100000; + private const int SwRestore = 9; + + // The owner only queues the activation and answers at once; one that is quitting may never answer. + private const uint AnswerMilliseconds = 5_000; + + // A key the owner gives up between the lookup and the register reads as null once; the second call registers it. + public static AppInstance? FindOwner(string key) => + LaunchClaim.OwnerOf( + AppInstance.FindOrRegisterForKey(key) ?? AppInstance.FindOrRegisterForKey(key), + instance => instance.IsCurrent); + + // RedirectActivationToAsync waits for the owner with no limit, so the wait here also ends on its exit or a timeout. + public static HandOverResult HandOver(AppInstance owner) + { + ArgumentNullException.ThrowIfNull(owner); + var activation = AppInstance.GetCurrent().GetActivatedEventArgs(); + // Only the process the user just started may take the foreground, so it passes that on to the owner. + _ = AllowSetForegroundWindow(owner.ProcessId); + using var exited = OpenProcess(Synchronize, false, owner.ProcessId); + // Not disposed: the continuation may still set it after this returns, from an owner that answers late. + var done = new ManualResetEvent(false); + // No .NET thread waits on it, so a dead owner that never answers holds nothing of this launch. + var redirect = Task.Run(() => owner.RedirectActivationToAsync(activation).AsTask()); + _ = redirect.ContinueWith( + _ => { done.Set(); }, + CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously, + TaskScheduler.Default); + IntPtr[] handles = exited.IsInvalid + ? [done.SafeWaitHandle.DangerousGetHandle()] + : [done.SafeWaitHandle.DangerousGetHandle(), exited.DangerousGetHandle()]; + // The redirect needs this STA thread to keep pumping COM calls, which CoWaitForMultipleObjects does. + var waited = CoWaitForMultipleObjects(0, AnswerMilliseconds, (uint)handles.Length, handles, out _); + return LaunchClaim.ReadWait(waited != 0, redirect); + } + + public static void BringToFront(Window window) + { + ArgumentNullException.ThrowIfNull(window); + var handle = WindowNative.GetWindowHandle(window); + if (IsIconic(handle)) + { + _ = ShowWindow(handle, SwRestore); + } + + window.Activate(); + _ = SetForegroundWindow(handle); + } + + public static void Release() + { + AppInstance.GetCurrent().UnregisterKey(); + } + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool AllowSetForegroundWindow(uint processId); + + [LibraryImport("kernel32.dll")] + private static partial SafeProcessHandle OpenProcess( + uint access, [MarshalAs(UnmanagedType.Bool)] bool inherit, uint processId); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool IsIconic(IntPtr window); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool ShowWindow(IntPtr window, int command); + + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool SetForegroundWindow(IntPtr window); + + [LibraryImport("ole32.dll")] + private static partial int CoWaitForMultipleObjects( + uint flags, uint timeout, uint count, IntPtr[] handles, out uint index); +} diff --git a/windows/src/TablePro.App/Services/WindowDialogs.cs b/windows/src/TablePro.App/Services/WindowDialogs.cs index b3424869ed..0724a20cf5 100644 --- a/windows/src/TablePro.App/Services/WindowDialogs.cs +++ b/windows/src/TablePro.App/Services/WindowDialogs.cs @@ -1,8 +1,10 @@ +using System.Runtime.InteropServices; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TablePro.App.Services; +// WinUI opens one ContentDialog per thread, even across windows, and throws on a second, so a second is declined. internal static class WindowDialogs { private static readonly HashSet OpenRoots = []; @@ -11,11 +13,12 @@ internal static class WindowDialogs public static async Task ShowAsync(ContentDialog dialog, FrameworkElement owner) { - if (owner.XamlRoot is not { } root || !OpenRoots.Add(root)) + if (owner.XamlRoot is not { } root || OpenRoots.Count > 0) { return ContentDialogResult.None; } + OpenRoots.Add(root); // Closing or moving the owner must dismiss its prompt before that root is gone. void OnUnloaded(object sender, RoutedEventArgs args) => dialog.Hide(); owner.Unloaded += OnUnloaded; @@ -24,6 +27,11 @@ public static async Task ShowAsync(ContentDialog dialog, Fr dialog.XamlRoot = root; return await dialog.ShowAsync(); } + catch (COMException) + { + // A dialog this class did not open holds the thread. + return ContentDialogResult.None; + } finally { owner.Unloaded -= OnUnloaded; diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index fe17da4e85..5228a24fb6 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -1284,6 +1284,36 @@ Close and roll back + + Run this statement? + + + Run these {0} statements? + + + It changes the database on {0}, which asks before every write. + + + They change the database on {0}, which asks before every write. + + + It drops or empties a table, or changes every row of one, on {0}. + + + Some of them drop or empty a table, or change every row of one, on {0}. + + + Run + + + Connecting was cancelled. Retry opens the tabs again. + + + Changes apply the next time you connect + + + Until then, this connection asks before every write. + Connection lost. Reconnecting, attempt {0}... diff --git a/windows/src/TablePro.App/TablePro.App.csproj b/windows/src/TablePro.App/TablePro.App.csproj index 82e26743e4..4595fa9984 100644 --- a/windows/src/TablePro.App/TablePro.App.csproj +++ b/windows/src/TablePro.App/TablePro.App.csproj @@ -28,6 +28,8 @@ SideloadOnly Never false + + $(DefineConstants);DISABLE_XAML_GENERATED_MAIN diff --git a/windows/src/TablePro.App/Views/OpenConnection.cs b/windows/src/TablePro.App/Views/OpenConnection.cs index 46d4a46200..69336512e4 100644 --- a/windows/src/TablePro.App/Views/OpenConnection.cs +++ b/windows/src/TablePro.App/Views/OpenConnection.cs @@ -22,6 +22,8 @@ internal sealed class OpenConnection private CancellationTokenSource? _connecting; private SessionConnection? _restore; private SessionShare? _share; + // A reconnect's tabs wait in _restore, so cancelling its connect keeps the item to retry instead of closing it. + private bool _keepOnCancel; public OpenConnection(Workbench workbench, SessionConnection origin) { @@ -121,6 +123,7 @@ public async Task ConnectAsync() SetState(ConnectionState.Connected); View.ShowConnected(_share, _restore); _restore = null; + _keepOnCancel = false; if (Origin.ConnectionId is not null) { _workbench.NotifyLibraryChanged(); @@ -129,7 +132,15 @@ public async Task ConnectAsync() catch (OperationCanceledException) { SetState(ConnectionState.NotConnected); - Cancelled?.Invoke(this, EventArgs.Empty); + if (_keepOnCancel) + { + var message = Strings.Get("ConnectCancelledKeepsTabs"); + View.ShowFailed(message, message); + } + else + { + Cancelled?.Invoke(this, EventArgs.Empty); + } } catch (TableProException exception) { @@ -232,12 +243,20 @@ private async Task SaveSecretAsync(Guid id, ConnectionSecrets secrets) } } - public async Task DisconnectAsync() + // A reconnect keeps the tabs, their text and the tabs closed before, to open again on the next session; only + // what their own connections held ends with this one. + public async Task DisconnectAsync(bool keepTabs = false) { CancelConnect(); + _keepOnCancel = keepTabs; + if (keepTabs && State == ConnectionState.Connected && Snapshot() is { Tabs.Count: > 0 } tabs) + { + _restore = tabs; + } + var share = _share; _share = null; - await View.CloseTabsAsync().ConfigureAwait(true); + await View.CloseTabsAsync(keepClosed: keepTabs).ConfigureAwait(true); if (share is not null) { await share.ReleaseAsync().ConfigureAwait(true); diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index 5292063b21..09340d9292 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -1,6 +1,7 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media; using TablePro.App.Model; using TablePro.App.Services; using TablePro.DataGrid; @@ -16,12 +17,14 @@ public sealed partial class QueryTabView : UserControl, ITabContent private readonly Workbench _workbench; private readonly SessionShare _share; private readonly DatabaseSession _session; + private readonly TabWork _work = new(); private QueryEditor? _editor; private Task? _closing; private QueryRun? _run; private string? _statement; private GridSort _sort = GridSort.None; private bool _sorting; + private bool _confirming; private EditTarget _target; internal QueryTabView(Workbench workbench, SessionShare share, string title, string? sql) @@ -70,8 +73,11 @@ internal QueryTabView(Workbench workbench, SessionShare share, string title, str // Once only, however often it is asked for: the tab lets go of its session exactly once. public Task CloseAsync() => _closing ??= CloseOnceAsync(); + // The run in flight stops and ends before the run, the editor and the session it uses are let go. private async Task CloseOnceAsync() { + _run?.Cancel(); + await _work.CloseAsync().ConfigureAwait(true); _run?.Dispose(); _run = null; Results.Clear(); @@ -87,7 +93,7 @@ private async Task CloseOnceAsync() private async Task RunAsync() { - if (IsRunning) + if (IsRunning || _confirming || _work.IsClosed) { return; } @@ -103,36 +109,113 @@ private async Task RunAsync() return; } + if (!await SafeModeAllowsAsync(sql).ConfigureAwait(true) || IsRunning || _work.IsClosed) + { + return; + } + _statement = sql; _sort = GridSort.None; await RunSqlAsync(sql).ConfigureAwait(true); } - private async Task RunSqlAsync(string sql) + // Typed SQL only: Commit and Roll back end a transaction at every level, and a sort runs again only a read. + private async Task SafeModeAllowsAsync(string sql) + { + var gate = _session.GateRun(sql); + if (gate.Refused) + { + Results.ShowError(Strings.Get("Error_driver_read_only")); + return false; + } + + if (gate.Confirm.Count == 0) + { + return true; + } + + _confirming = true; + try + { + return await ConfirmWritesAsync(gate).ConfigureAwait(true); + } + finally + { + _confirming = false; + } + } + + // Cancel is the answer Enter gives, and every statement shows in full, as the Linux app asks. + private async Task ConfirmWritesAsync(RunGate gate) + { + var count = gate.Confirm.Count; + var message = gate.Destructive + ? Strings.Format(count == 1 ? "ConfirmRunDestructiveOne" : "ConfirmRunDestructiveMany", _session.Name) + : Strings.Format(count == 1 ? "ConfirmRunWriteOne" : "ConfirmRunWriteMany", _session.Name); + var content = new StackPanel { Spacing = 12 }; + content.Children.Add(new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }); + var statements = new TextBlock + { + Text = string.Join("\n\n", gate.Confirm), + FontFamily = new FontFamily("Cascadia Mono, Consolas, Courier New"), + TextWrapping = TextWrapping.Wrap, + IsTextSelectionEnabled = true, + }; + AutomationProperties.SetAutomationId(statements, "ConfirmRunStatements"); + content.Children.Add(statements); + var dialog = new ContentDialog + { + XamlRoot = XamlRoot, + RequestedTheme = ActualTheme, + Title = count == 1 ? Strings.Get("ConfirmRunTitleOne") : Strings.Format("ConfirmRunTitleMany", count), + Content = new ScrollViewer { Content = content, MaxHeight = 480 }, + PrimaryButtonText = Strings.Get("ConfirmRunButton"), + CloseButtonText = Strings.Get("Cancel"), + DefaultButton = ContentDialogButton.Close, + }; + return await WindowDialogs.ShowAsync(dialog, this) == ContentDialogResult.Primary; + } + + private Task RunSqlAsync(string sql) => _work.RunAsync(token => RunOnEditorAsync(sql, token)); + + private async Task RunOnEditorAsync(string sql, CancellationToken cancellationToken) { SetRunning(true); try { var held = WorkspaceNotices.Holds(Transaction); - if (await ReleaseLostEditorAsync().ConfigureAwait(true) && held) + if ((await ReleaseLostEditorAsync().ConfigureAwait(true) && held) || _work.IsClosed) + { + return; + } + + // An editor that opens as the tab closes stays in _editor, which the close disposes. + _editor ??= await _session.OpenEditorAsync(cancellationToken).ConfigureAwait(true); + if (_work.IsClosed) { return; } - _editor ??= await _session.OpenEditorAsync().ConfigureAwait(true); _run?.Dispose(); var settings = _workbench.Settings; _run = _editor.Run( sql, new QueryOptions( settings.RowLimit, - settings.QueryTimeoutSeconds > 0 ? TimeSpan.FromSeconds(settings.QueryTimeoutSeconds) : null)); - await Results.ShowRunAsync(_run, _sort).ConfigureAwait(true); - await AfterRunAsync().ConfigureAwait(true); + settings.QueryTimeoutSeconds > 0 ? TimeSpan.FromSeconds(settings.QueryTimeoutSeconds) : null), + cancellationToken); + await Results.ShowRunAsync(_run, _sort, cancellationToken: cancellationToken).ConfigureAwait(true); + if (!_work.IsClosed) + { + await AfterRunAsync().ConfigureAwait(true); + } } catch (TableProException exception) { - Results.ShowError(Strings.Error(exception.Error)); + if (!_work.IsClosed) + { + Results.ShowError(Strings.Error(exception.Error)); + } } finally { @@ -249,7 +332,8 @@ private async void OnSortRequested(object? sender, GridSort sort) : SortedQuery(_statement, columns, sort.Column, sort.Direction == SortDirection.Ascending); if (sorted is null) { - await SortHeldRowsAsync(sort).ConfigureAwait(true); + // The close waits for it, since it clears the sets the sort reads. + await _work.RunAsync(_ => SortHeldRowsAsync(sort)).ConfigureAwait(true); return; } diff --git a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs index a74710b558..5865bc95be 100644 --- a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs +++ b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs @@ -105,7 +105,11 @@ public void Clear() } // The rows shown stay until the new set has rows or ends, so a sort or a refresh never blanks the grid. - internal async Task ShowRunAsync(QueryRun run, GridSort sort, int visibleColumns = int.MaxValue) + internal async Task ShowRunAsync( + QueryRun run, + GridSort sort, + int visibleColumns = int.MaxValue, + CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(run); ErrorBar.IsOpen = false; @@ -134,7 +138,7 @@ void ShowWaiting() try { - await foreach (var queryEvent in run.ReadEventsAsync().ConfigureAwait(true)) + await foreach (var queryEvent in run.ReadEventsAsync(cancellationToken).ConfigureAwait(true)) { switch (queryEvent) { diff --git a/windows/src/TablePro.App/Views/TableTabView.xaml.cs b/windows/src/TablePro.App/Views/TableTabView.xaml.cs index 39f90c89a5..b2f38e391c 100644 --- a/windows/src/TablePro.App/Views/TableTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/TableTabView.xaml.cs @@ -18,6 +18,7 @@ public sealed partial class TableTabView : UserControl, ITabContent private readonly DatabaseSession _session; private readonly string? _schema; private readonly string _table; + private readonly TabWork _work = new(); private QueryEditor? _editor; private Task? _closing; private QueryRun? _run; @@ -97,9 +98,11 @@ public bool Shows(string? schema, string table) => // Once only, however often it is asked for: the tab lets go of its session exactly once. public Task CloseAsync() => _closing ??= CloseOnceAsync(); + // The page in flight and its count stop and end before the run, the editor and the session they use are let go. private async Task CloseOnceAsync() { - _counting?.Cancel(); + _run?.Cancel(); + await _work.CloseAsync().ConfigureAwait(true); _run?.Dispose(); _run = null; Results.Clear(); @@ -114,11 +117,14 @@ private async Task CloseOnceAsync() private async Task LoadAsync(long offset) { - if (IsRunning) + if (!IsRunning) { - return; + await _work.RunAsync(token => LoadPageAsync(offset, token)).ConfigureAwait(true); } + } + private async Task LoadPageAsync(long offset, CancellationToken cancellationToken) + { _finished = null; SetRunning(true); try @@ -129,7 +135,8 @@ private async Task LoadAsync(long offset) await lost.DisposeAsync().ConfigureAwait(true); } - _editor ??= await _session.OpenEditorAsync().ConfigureAwait(true); + // An editor that opens as the tab closes stays in _editor, which the close disposes. + _editor ??= await _session.OpenEditorAsync(cancellationToken).ConfigureAwait(true); var page = _page.At(offset); // A default changed in Settings applies from the page holding the first row shown. if (_chosenSize is null && page.Size != PageSize) @@ -140,23 +147,37 @@ private async Task LoadAsync(long offset) var sort = _sort.Direction == SortDirection.None ? null : new ColumnSort(_sort.Column, _sort.Direction == SortDirection.Ascending); - var query = await _session.GetTableQueryAsync(new TableName(_schema, _table), sort, page.Size, page.Offset) + var query = await _session.GetTableQueryAsync( + new TableName(_schema, _table), sort, page.Size, page.Offset, cancellationToken) .ConfigureAwait(true); + if (_work.IsClosed) + { + return; + } + _run?.Dispose(); _page = page with { CanSeek = query.CanSeek }; // The statement holds the page; a run limit one past it keeps a full page from reading as cut short. - _run = _editor.Run(query.Sql, new QueryOptions(page.Size + 1)); - await Results.ShowRunAsync(_run, _sort, query.VisibleColumns).ConfigureAwait(true); + _run = _editor.Run(query.Sql, new QueryOptions(page.Size + 1), cancellationToken); + await Results.ShowRunAsync(_run, _sort, query.VisibleColumns, cancellationToken).ConfigureAwait(true); + if (_work.IsClosed) + { + return; + } + _shownSort = _sort; _page = _page with { Rows = Results.RowCount }; CountIfNeeded(); } catch (TableProException exception) { - // An engine that reads a table only in its own order refuses the sort; the rows shown keep theirs. - _sort = _shownSort; - Results.Sort = _shownSort; - Results.ShowError(Strings.Error(exception.Error)); + if (!_work.IsClosed) + { + // An engine that reads a table only in its own order refuses the sort; the rows shown keep theirs. + _sort = _shownSort; + Results.Sort = _shownSort; + Results.ShowError(Strings.Error(exception.Error)); + } } finally { @@ -164,8 +185,10 @@ private async Task LoadAsync(long offset) } } + private async void CountIfNeeded() => await _work.RunAsync(CountAsync).ConfigureAwait(true); + // A first page that is not full holds the whole table; otherwise the server counts it, in the background. - private async void CountIfNeeded() + private async Task CountAsync(CancellationToken closing) { if (_page.Total is not null || !_page.CanSeek) { @@ -182,7 +205,7 @@ private async void CountIfNeeded() UpdatePager(); _counting?.Cancel(); - using var counting = new CancellationTokenSource(); + using var counting = CancellationTokenSource.CreateLinkedTokenSource(closing); _counting = counting; try { diff --git a/windows/src/TablePro.App/Views/WorkspaceView.xaml b/windows/src/TablePro.App/Views/WorkspaceView.xaml index 2219680b4b..98d9332d63 100644 --- a/windows/src/TablePro.App/Views/WorkspaceView.xaml +++ b/windows/src/TablePro.App/Views/WorkspaceView.xaml @@ -162,6 +162,7 @@ + @@ -191,12 +192,21 @@ + + ConfirmCloseAsync(ISet confirmed) return true; } - public async Task CloseTabsAsync() + // A reconnect keeps the closed tabs, which hold only their text or table, to reopen on the next session. + public async Task CloseTabsAsync(bool keepClosed = false) { CancelRecovery(); _hostKeyFailure = null; @@ -251,7 +252,12 @@ public async Task CloseTabsAsync() _watching = null; HealthBar.IsOpen = false; ElsewhereBar.IsOpen = false; - _closed.Clear(); + HideChangesOnConnect(); + if (!keepClosed) + { + _closed.Clear(); + } + foreach (var content in Tabs.TabItems.OfType().Select(item => item.Content).OfType().ToList()) { await content.CloseAsync().ConfigureAwait(true); @@ -650,6 +656,24 @@ private async Task ConfirmCloseHeldAsync(ITabContent content) return await WindowDialogs.ShowAsync(dialog, this) == ContentDialogResult.Primary; } + // A saved change only a new connection takes, kept back because a held transaction would go with this one. + internal void ShowChangesOnConnect(bool asksBeforeWrites) + { + ChangesBar.Title = Strings.Get("ChangesOnConnectTitle"); + ChangesBar.Message = asksBeforeWrites ? Strings.Get("ChangesOnConnectAsks") : string.Empty; + // A closed InfoBar keeps its margin, so the bar is collapsed while it is closed. + ChangesBar.Visibility = Visibility.Visible; + // Narrator reads an InfoBar when it opens, not when its text changes. + ChangesBar.IsOpen = false; + ChangesBar.IsOpen = true; + } + + internal void HideChangesOnConnect() + { + ChangesBar.IsOpen = false; + ChangesBar.Visibility = Visibility.Collapsed; + } + // The tab shown names its own transaction; this bar names one another tab holds. private void UpdateElsewhereBar() { diff --git a/windows/src/TablePro.Interop/DatabaseSession.cs b/windows/src/TablePro.Interop/DatabaseSession.cs index 195332dfa9..5321cbdb9a 100644 --- a/windows/src/TablePro.Interop/DatabaseSession.cs +++ b/windows/src/TablePro.Interop/DatabaseSession.cs @@ -124,6 +124,18 @@ public async Task WaitForHealthChangeAsync( return CoreCall.Run(() => _session.SortedQuery(sql, [.. columns], checked((uint)column), ascending)); } + // At the session's Safe Mode level now, read with the engine's own statement split. + public RunGate GateRun(string sql) + { + ArgumentNullException.ThrowIfNull(sql); + var gate = _session.GateRun(sql); + return new RunGate(gate.Refused, [.. gate.Confirm], gate.Destructive); + } + + // A level saved while the session is open applies to it at once. False when it crosses Read only, which only a + // new connection can do; the session keeps its level until then. + public bool SetSafeMode(SafeMode level) => _session.SetSafeMode(Mapping.Level(level)); + // An editor tab's own connection, which keeps its transaction from one run to the next. public async Task OpenEditorAsync(CancellationToken cancellationToken = default) { diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index 41a3d209b5..6c6aa3f2f7 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -1012,6 +1012,10 @@ public struct UniffiVTableCallbackInterfaceRunListener + + + + @@ -1698,6 +1702,17 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_method_databasesession_driver_id(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_databasesession_gate_run(ulong @ptr,RustBuffer @sql,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -1808,6 +1823,17 @@ public static extern ulong uniffi_tablepro_ffi_fn_method_databasesession_schemas(ulong @ptr,RustBuffer @cancel ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + sbyte uniffi_tablepro_ffi_fn_method_databasesession_set_safe_mode(ulong @ptr,RustBuffer @level,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -2952,6 +2978,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_databasesession_driver_id( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_gate_run( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -3062,6 +3099,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_databasesession_schemas( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_set_safe_mode( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -3528,6 +3576,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_driver_id` checksum `42377`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_gate_run(); + if (checksum != 5582) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_gate_run` checksum `5582`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_health(); if (checksum != 16691) { @@ -3588,6 +3642,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_schemas` checksum `64032`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_set_safe_mode(); + if (checksum != 16374) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_set_safe_mode` checksum `16374`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_sorted_query(); if (checksum != 35269) { @@ -4822,6 +4882,7 @@ internal interface IDatabaseSession { /// Task Columns(TableName @table, CancelToken? @cancel); string DriverId(); + RunGate GateRun(string @sql); SessionHealthTag Health(); /// Task HealthChange(SessionHealthTag @seen, CancelToken? @cancel); @@ -4837,6 +4898,7 @@ internal interface IDatabaseSession { Task RefreshObjects(ObjectScopeArg @scope, CancelToken? @cancel); /// Task Schemas(CancelToken? @cancel); + bool SetSafeMode(SafeModeTag @level); /// string? SortedQuery(string @sql, string[] @columns, uint @column, bool @ascending); /// @@ -4981,6 +5043,14 @@ public string DriverId() { } + public RunGate GateRun(string @sql) { + return CallWithPointer(thisPtr => FfiConverterTypeRunGate.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_gate_run(thisPtr, FfiConverterString.INSTANCE.Lower(@sql), ref _status) +))); + } + + public SessionHealthTag Health() { return CallWithPointer(thisPtr => FfiConverterTypeSessionHealthTag.INSTANCE.Lift( _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => @@ -5132,6 +5202,14 @@ public async Task Schemas(CancelToken? @cancel) { ); } + public bool SetSafeMode(SafeModeTag @level) { + return CallWithPointer(thisPtr => FfiConverterBoolean.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_set_safe_mode(thisPtr, FfiConverterTypeSafeModeTag.INSTANCE.Lower(@level), ref _status) +))); + } + + /// public string? SortedQuery(string @sql, string[] @columns, uint @column, bool @ascending) { return CallWithPointer(thisPtr => FfiConverterOptionalString.INSTANCE.Lift( @@ -6795,6 +6873,40 @@ public override void Write(ResultColumnInfo value, BigEndianStream stream) { +internal record RunGate ( + bool Refused, + string[] Confirm, + bool Destructive +) { +} + +class FfiConverterTypeRunGate: FfiConverterRustBuffer { + public static FfiConverterTypeRunGate INSTANCE = new FfiConverterTypeRunGate(); + + public override RunGate Read(BigEndianStream stream) { + return new RunGate( + Refused: FfiConverterBoolean.INSTANCE.Read(stream), + Confirm: FfiConverterSequenceString.INSTANCE.Read(stream), + Destructive: FfiConverterBoolean.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(RunGate value) { + return 0 + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Refused) + + FfiConverterSequenceString.INSTANCE.AllocationSize(value.Confirm) + + FfiConverterBoolean.INSTANCE.AllocationSize(value.Destructive); + } + + public override void Write(RunGate value, BigEndianStream stream) { + FfiConverterBoolean.INSTANCE.Write(value.Refused, stream); + FfiConverterSequenceString.INSTANCE.Write(value.Confirm, stream); + FfiConverterBoolean.INSTANCE.Write(value.Destructive, stream); + } +} + + + /// /// What one run may do. `None` takes the editor's defaults: 10,000 rows /// and no deadline beyond the connection's. diff --git a/windows/src/TablePro.Interop/Mapping.cs b/windows/src/TablePro.Interop/Mapping.cs index 77aef9084e..6faa25d78d 100644 --- a/windows/src/TablePro.Interop/Mapping.cs +++ b/windows/src/TablePro.Interop/Mapping.cs @@ -254,6 +254,13 @@ public static Native.HostConfig Host(HostConfiguration configuration) => public static Native.ConnectionSecrets Secrets(ConnectionSecrets secrets) => new(secrets.DatabasePassword, secrets.SshPassword, secrets.SshPassphrase); + public static Native.SafeModeTag Level(SafeMode level) => level switch + { + SafeMode.ConfirmWrites => Native.SafeModeTag.ConfirmWrites, + SafeMode.ReadOnly => Native.SafeModeTag.ReadOnly, + _ => Native.SafeModeTag.Off, + }; + public static Native.ConnectionRecord Record(SavedConnection connection) => new( connection.Id?.ToString() ?? string.Empty, connection.Name, @@ -283,12 +290,7 @@ connection.Ssh is { } ssh _ => new Native.SshAuthTag.Agent(), }) : null, - connection.SafeMode switch - { - SafeMode.ConfirmWrites => Native.SafeModeTag.ConfirmWrites, - SafeMode.ReadOnly => Native.SafeModeTag.ReadOnly, - _ => Native.SafeModeTag.Off, - }, + Level(connection.SafeMode), connection.QueryTimeoutSeconds is { } timeout ? checked((uint)timeout) : null, connection.Color, connection.GroupId?.ToString(), diff --git a/windows/src/TablePro.Interop/QueryRun.cs b/windows/src/TablePro.Interop/QueryRun.cs index a8f37a7426..c6c1415615 100644 --- a/windows/src/TablePro.Interop/QueryRun.cs +++ b/windows/src/TablePro.Interop/QueryRun.cs @@ -4,11 +4,14 @@ namespace TablePro.Interop; -// Disposing it stops the run. +// Disposing it stops the run, and its events end where they were read to. public sealed class QueryRun : IDisposable { private readonly Native.QueryRun _run; private readonly RunWake _wake; + // A tab can close while its run is read, so no native call starts once Dispose has. + private readonly Lock _gate = new(); + private bool _disposed; private int _reading; internal QueryRun(Native.QueryRun run, RunWake wake, CancellationToken cancellationToken) @@ -30,11 +33,15 @@ public async IAsyncEnumerable ReadEventsAsync( throw new InvalidOperationException("The run's events are already being read."); } - while (true) + // Resumes on the caller's context, which is where the events are meant to be read. + while (await _wake.WaitAsync(cancellationToken)) { - // Resumes on the caller's context, which is where the events are meant to be read. - await _wake.WaitAsync(cancellationToken); - foreach (var native in _run.DrainEvents()) + if (Drain() is not { } natives) + { + yield break; + } + + foreach (var native in natives) { var queryEvent = Mapping.Event(native); yield return queryEvent; @@ -46,28 +53,82 @@ public async IAsyncEnumerable ReadEventsAsync( } } - // Null until the set at index, from 0, has started. + // Null until the set at index, from 0, has started, and once the run is disposed. public ResultSet? GetResultSet(int index) { ArgumentOutOfRangeException.ThrowIfNegative(index); - return _run.ResultSet(checked((uint)index)) is { } set ? new ResultSet(set, index) : null; + lock (_gate) + { + if (_disposed) + { + return null; + } + + return _run.ResultSet(checked((uint)index)) is { } set ? new ResultSet(set, index) : null; + } } - public void Cancel() => _run.Cancel(); + public void Cancel() + { + lock (_gate) + { + if (!_disposed) + { + _run.Cancel(); + } + } + } public void Dispose() { - _run.Cancel(); - _run.Dispose(); + lock (_gate) + { + if (_disposed) + { + return; + } + + _disposed = true; + _run.Cancel(); + _run.Dispose(); + } + + _wake.Close(); + } + + // Null once disposed. The drain also takes the wake, so the core calls again for the next events. + private Native.RunEvent[]? Drain() + { + lock (_gate) + { + if (_disposed) + { + return null; + } + + _wake.Take(); + return _run.DrainEvents(); + } } private async Task WaitForOutcomeAsync(CancellationToken cancellationToken) { - using var registration = cancellationToken.UnsafeRegister( - static state => ((Native.QueryRun)state!).Cancel(), - _run); - var outcome = await CoreCall.RunAsync(() => _run.Outcome()).ConfigureAwait(false); - return Mapping.Outcome(outcome); + using var registration = cancellationToken.UnsafeRegister(static state => ((QueryRun)state!).Cancel(), this); + return await CoreCall.RunAsync(() => OutcomeAsync()).ConfigureAwait(false); + } + + // A run disposed before its wait started was stopped by that dispose. + private async Task OutcomeAsync() => + StartOutcome() is { } outcome + ? Mapping.Outcome(await outcome.ConfigureAwait(false)) + : new QueryOutcome.Cancelled(); + + private Task? StartOutcome() + { + lock (_gate) + { + return _disposed ? null : _run.Outcome(); + } } } @@ -83,6 +144,11 @@ internal sealed class RunWake : Native.RunListener public void EventsPending() => _signal.Writer.TryWrite(true); + // False once the run is disposed and no wake is left. public ValueTask WaitAsync(CancellationToken cancellationToken) => - _signal.Reader.ReadAsync(cancellationToken); + _signal.Reader.WaitToReadAsync(cancellationToken); + + public void Take() => _signal.Reader.TryRead(out _); + + public void Close() => _signal.Writer.TryComplete(); } diff --git a/windows/src/TablePro.Interop/RunGate.cs b/windows/src/TablePro.Interop/RunGate.cs new file mode 100644 index 0000000000..805387a10f --- /dev/null +++ b/windows/src/TablePro.Interop/RunGate.cs @@ -0,0 +1,5 @@ +namespace TablePro.Interop; + +// What Safe Mode lets an editor run do. An editor sends its text whole, so Refused keeps the whole run back; +// Confirm holds each statement to ask about first, as it would run. +public sealed record RunGate(bool Refused, IReadOnlyList Confirm, bool Destructive); diff --git a/windows/tests/TablePro.App.Model.Tests/InstanceKeyTests.cs b/windows/tests/TablePro.App.Model.Tests/InstanceKeyTests.cs new file mode 100644 index 0000000000..1562b5c28b --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/InstanceKeyTests.cs @@ -0,0 +1,23 @@ +namespace TablePro.App.Model.Tests; + +public sealed class InstanceKeyTests +{ + [Fact] + public void AFolderGivesOneKeyWhateverItsCaseOrTrailingSeparator() => + Assert.Equal( + InstanceKey.For(@"C:\Users\Ada\AppData\Local\TablePro"), + InstanceKey.For(@"c:\users\ada\appdata\local\tablepro\")); + + [Fact] + public void TwoFoldersGiveTwoKeys() => + Assert.NotEqual(InstanceKey.For(@"C:\Data\One"), InstanceKey.For(@"C:\Data\Two")); + + [Fact] + public void AKeyIsShortAndPlainHoweverLongTheFolder() + { + var key = InstanceKey.For(@"C:\" + string.Join('\\', Enumerable.Repeat("a folder name", 40))); + + Assert.Equal(InstanceKey.For(@"C:\a").Length, key.Length); + Assert.Matches("^[A-Za-z0-9-]+$", key); + } +} diff --git a/windows/tests/TablePro.App.Model.Tests/LaunchClaimTests.cs b/windows/tests/TablePro.App.Model.Tests/LaunchClaimTests.cs new file mode 100644 index 0000000000..58b51224f9 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/LaunchClaimTests.cs @@ -0,0 +1,112 @@ +namespace TablePro.App.Model.Tests; + +public sealed class LaunchClaimTests +{ + [Fact] + public void AProcessThatHoldsTheKeyStartsWithoutHandingOver() + { + var handed = 0; + + var starts = LaunchClaim.StartsHere( + () => null, _ => Count(ref handed, HandOverResult.Taken), _ => { }); + + Assert.True(starts); + Assert.Equal(0, handed); + } + + [Fact] + public void ALaunchTheRunningAppTookEnds() => + Assert.False(LaunchClaim.StartsHere(() => new Owner(), _ => HandOverResult.Taken, _ => { })); + + [Fact] + public void ALaunchStartsItsOwnWindowWhenTheRunningAppGivesNoAnswer() => + Assert.True(LaunchClaim.StartsHere(() => new Owner(), _ => HandOverResult.NoAnswer, _ => { })); + + [Fact] + public void ALaunchLooksAgainOnceTheOwnerExitedAndStartsWithTheKey() + { + var owners = new Queue([new Owner(), null]); + var handed = 0; + + var starts = LaunchClaim.StartsHere( + owners.Dequeue, _ => Count(ref handed, HandOverResult.OwnerExited), _ => { }); + + Assert.True(starts); + Assert.Equal(1, handed); + } + + [Fact] + public void ALaunchStartsAfterItsAttemptsWhenEveryOwnerExits() + { + var handed = 0; + + var starts = LaunchClaim.StartsHere( + () => new Owner(), _ => Count(ref handed, HandOverResult.OwnerExited), _ => { }); + + Assert.True(starts); + Assert.Equal(LaunchClaim.Attempts, handed); + } + + [Fact] + public void ALaunchStartsAndReportsWhenTheGuardFails() + { + var failure = new InvalidOperationException("The shared memory could not be opened."); + Exception? reported = null; + + var starts = LaunchClaim.StartsHere( + () => throw failure, _ => HandOverResult.Taken, exception => reported = exception); + + Assert.True(starts); + Assert.Same(failure, reported); + } + + [Fact] + public void AKeyNobodyHoldsAnyMoreHasNoOwner() => + Assert.Null(LaunchClaim.OwnerOf(null, _ => false)); + + [Fact] + public void ThisProcessIsNeverItsOwnOwner() => + Assert.Null(LaunchClaim.OwnerOf(new Owner(), _ => true)); + + [Fact] + public void AnotherProcessHoldingTheKeyIsTheOwner() + { + var other = new Owner(); + + Assert.Same(other, LaunchClaim.OwnerOf(other, _ => false)); + } + + [Fact] + public void AWaitThatTimedOutIsNoAnswerWhateverTheRedirectDidLater() => + Assert.Equal(HandOverResult.NoAnswer, LaunchClaim.ReadWait(timedOut: true, Task.CompletedTask)); + + [Fact] + public void AFinishedRedirectIsTaken() => + Assert.Equal(HandOverResult.Taken, LaunchClaim.ReadWait(timedOut: false, Task.CompletedTask)); + + [Fact] + public void AFailedRedirectIsAnOwnerThatExited() + { + var failed = Task.FromException(new InvalidOperationException("The owner is gone.")); + + Assert.Equal(HandOverResult.OwnerExited, LaunchClaim.ReadWait(timedOut: false, failed)); + } + + [Fact] + public void AWaitThatEndedOnTheOwnersExitWithTheRedirectPendingIsAnOwnerThatExited() + { + var pending = new TaskCompletionSource().Task; + + Assert.Equal(HandOverResult.OwnerExited, LaunchClaim.ReadWait(timedOut: false, pending)); + } + + private static HandOverResult Count(ref int handed, HandOverResult result) + { + handed++; + return result; + } + + private sealed class Owner + { + } +} diff --git a/windows/tests/TablePro.App.Model.Tests/TabWorkTests.cs b/windows/tests/TablePro.App.Model.Tests/TabWorkTests.cs new file mode 100644 index 0000000000..6572a9e2a6 --- /dev/null +++ b/windows/tests/TablePro.App.Model.Tests/TabWorkTests.cs @@ -0,0 +1,76 @@ +namespace TablePro.App.Model.Tests; + +public sealed class TabWorkTests +{ + [Fact] + public async Task ClosingCancelsTheWorkInFlightAndWaitsForItToEnd() + { + var work = new TabWork(); + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var cleanup = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var ended = false; + var running = work.RunAsync(async token => + { + started.SetResult(); + try + { + await Task.Delay(Timeout.Infinite, token); + } + finally + { + await cleanup.Task; + ended = true; + } + }); + await started.Task; + + var closing = work.CloseAsync(); + var waited = !closing.IsCompleted; + cleanup.SetResult(); + await closing; + + Assert.True(waited); + Assert.True(ended); + Assert.True(work.IsClosed); + Assert.False(await running); + } + + [Fact] + public async Task WorkAskedForOnceTheTabClosedNeverStarts() + { + var work = new TabWork(); + await work.CloseAsync(); + var ran = false; + + var accepted = await work.RunAsync(_ => + { + ran = true; + return Task.CompletedTask; + }); + + Assert.False(accepted); + Assert.False(ran); + } + + [Fact] + public async Task WorkThatEndsBeforeTheCloseSaysItRan() + { + var work = new TabWork(); + + var ran = await work.RunAsync(_ => Task.CompletedTask); + await work.CloseAsync(); + + Assert.True(ran); + } + + [Fact] + public async Task AFailureTheCloseDidNotCauseStillReachesTheCaller() + { + var work = new TabWork(); + + await Assert.ThrowsAsync( + () => work.RunAsync(_ => throw new InvalidOperationException("broken"))); + await Assert.ThrowsAnyAsync( + () => work.RunAsync(_ => throw new OperationCanceledException())); + } +} diff --git a/windows/tests/TablePro.App.UITests/SafeModeAndReconnectTests.cs b/windows/tests/TablePro.App.UITests/SafeModeAndReconnectTests.cs new file mode 100644 index 0000000000..f392776488 --- /dev/null +++ b/windows/tests/TablePro.App.UITests/SafeModeAndReconnectTests.cs @@ -0,0 +1,114 @@ +using FlaUI.Core.Tools; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +public sealed partial class ShellTests +{ + private const string Endless = + "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT count(*) FROM c"; + + [Theory] + [MemberData(nameof(Themes))] + public void EmptyingATableAsksFirstAndCancelRunsNothing(string theme) + { + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", "DELETE FROM Track"); + app.Chord(() => app.TryFind("ConfirmRunStatements") is not null, VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + + Assert.Contains("DELETE FROM Track", app.Find("ConfirmRunStatements").Name, StringComparison.Ordinal); + app.Screenshot("39-run-asked"); + app.CloseDialog(); + app.TypeInto("Query 1", "SELECT COUNT(*) = 3503 FROM Track;"); + app.Chord(() => app.Find("QueryStatus").Name.StartsWith("Rows: 1", StringComparison.Ordinal), + VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + Assert.Equal("1", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "1")); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ReconnectAsksBeforeRollingBackAndBringsTheTabsBack(string theme) + { + const string Update = "BEGIN; UPDATE Track SET Name = 'Pending' WHERE TrackId = 1;"; + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", Update); + app.Chord(() => app.TryFind("CommitButton") is { IsOffscreen: false }, VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + + Reconnect(app); + app.CloseDialog(); + Assert.True(app.Find("CommitButton").IsEnabled); + + Reconnect(app); + app.Screenshot("40-reconnect-asked"); + app.PressDialogButton(); + var rolledBack = Retry.WhileFalse( + () => app.TryFind("CommitButton") is null or { IsOffscreen: true }, + AppSession.Wait, + throwOnTimeout: false).Success; + Assert.True(rolledBack); + Assert.Equal(Update, AppSession.EditorText(app.FindEditor("Query 1")).TrimEnd('\r', '\n')); + app.TypeInto("Query 1", "SELECT COUNT(*) FROM Track WHERE TrackId = 1 AND Name = 'Pending';"); + app.Chord(() => app.Find("QueryStatus").Name.StartsWith("Rows: 1", StringComparison.Ordinal), + VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + Assert.Equal("0", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "0")); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ClosingATabWhileItsQueryRunsLeavesTheAppRunning(string theme) + { + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + var tabs = app.Find("Tabs"); + bool Open() => tabs.FindFirstDescendant(condition => condition.ByName("Query 2")) is not null; + app.Chord(Open, VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_T); + app.TypeInto("Query 2", Endless); + app.Chord(() => app.Find("StopButton").IsEnabled, VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + app.Chord(() => !Open(), VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_W); + + AnswerRuns(app); + } + + [Theory] + [MemberData(nameof(Themes))] + public void ReconnectingWhileAQueryRunsBringsItsTabBack(string theme) + { + using var app = AppSession.Launch(theme); + + app.Click(app.Find("SampleCard"), () => app.TryFind("Rail_Chinook (Sample)") is not null); + app.FindByName("Track", app.Find("ObjectTree")); + app.TypeInto("Query 1", Endless); + app.Chord(() => app.Find("StopButton").IsEnabled, VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + Reconnect(app); + + var back = Retry.WhileFalse( + () => AppSession.EditorText(app.FindEditor("Query 1")).TrimEnd('\r', '\n') == Endless, + AppSession.Wait, + throwOnTimeout: false).Success; + Assert.True(back); + AnswerRuns(app); + } + + // The app outlived the close, and its connection still runs a query. + private static void AnswerRuns(AppSession app) + { + app.TypeInto("Query 1", "SELECT 42 AS answer;"); + app.Chord(() => app.Find("QueryStatus").Name.StartsWith("Rows: 1", StringComparison.Ordinal), + VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); + Assert.Equal("42", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "42")); + } + + private static void Reconnect(AppSession app) + { + app.Click(app.Find("DatabaseMenu"), () => app.TryFindMenuItem("Reconnect") is not null); + app.FindMenuItem("Reconnect").Patterns.Invoke.Pattern.Invoke(); + } +} diff --git a/windows/tests/TablePro.Interop.Tests/RunDisposalTests.cs b/windows/tests/TablePro.Interop.Tests/RunDisposalTests.cs new file mode 100644 index 0000000000..bbbe008a38 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/RunDisposalTests.cs @@ -0,0 +1,65 @@ +namespace TablePro.Interop.Tests; + +// A tab disposes its run when it closes, which can be while the run's events are still read. +public sealed class RunDisposalTests +{ + private const string Endless = + "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT count(*) FROM c"; + + [Fact] + public async Task DisposingARunWhileItsEventsAreReadEndsTheReadingQuietly() + { + using var ui = new UiThread(); + await using var database = await SqliteDatabase.CreateAsync(); + + await ui.RunAsync(async () => + { + var run = database.Editor.Run(Endless); + var completion = run.Completion; + var closing = Task.Run( + async () => + { + await Task.Delay(200, CancellationToken.None); + ui.Post(run.Dispose); + }, + CancellationToken.None); + await foreach (var _ in run.ReadEventsAsync()) + { + } + + await closing; + Assert.IsType(await completion); + }); + } + + [Fact] + public async Task ADisposedRunAnswersEveryCallWithoutThrowing() + { + await using var database = await SqliteDatabase.CreateAsync(); + var run = database.Editor.Run( + "SELECT name FROM people", + cancellationToken: TestContext.Current.CancellationToken); + await run.Completion; + + run.Dispose(); + run.Dispose(); + run.Cancel(); + + Assert.Null(run.GetResultSet(0)); + await foreach (var _ in run.ReadEventsAsync(TestContext.Current.CancellationToken)) + { + Assert.Fail("A disposed run has no events left to read."); + } + } + + [Fact] + public async Task ARunDisposedAtOnceStillCompletes() + { + await using var database = await SqliteDatabase.CreateAsync(); + + var run = database.Editor.Run(Endless, cancellationToken: TestContext.Current.CancellationToken); + run.Dispose(); + + Assert.IsType(await run.Completion); + } +} diff --git a/windows/tests/TablePro.Interop.Tests/SafeModeGateTests.cs b/windows/tests/TablePro.Interop.Tests/SafeModeGateTests.cs new file mode 100644 index 0000000000..9b90aa6ba8 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/SafeModeGateTests.cs @@ -0,0 +1,113 @@ +namespace TablePro.Interop.Tests; + +public sealed class SafeModeGateTests : IDisposable +{ + private readonly string _directory = Directory.CreateTempSubdirectory("tablepro-safe-mode-").FullName; + private readonly TableProCore _core; + + public SafeModeGateTests() + { + _core = TableProCore.Create(new HostConfiguration("test", _directory), new MemorySecretStore()); + } + + public void Dispose() + { + _core.Dispose(); + Directory.Delete(_directory, recursive: true); + } + + [Fact] + public async Task ConfirmWritesAsksAboutEachWriteAndNeverAboutARead() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await using var session = await ConnectAsync(database.Path, SafeMode.ConfirmWrites, cancellationToken); + + var script = session.GateRun("SELECT 1; UPDATE people SET name = 'Ada' WHERE id = 1; DELETE FROM people"); + var read = session.GateRun("SELECT * FROM people"); + + Assert.Equal(["UPDATE people SET name = 'Ada' WHERE id = 1", "DELETE FROM people"], script.Confirm); + Assert.True(script.Destructive); + Assert.False(script.Refused); + Assert.Empty(read.Confirm); + Assert.False(read.Refused); + } + + [Fact] + public async Task OffAsksOnlyBeforeAStatementThatEmptiesATable() + { + await using var database = await SqliteDatabase.CreateAsync(); + + var insert = database.Session.GateRun("INSERT INTO people (name) VALUES ('Ada')"); + var delete = database.Session.GateRun("DELETE FROM people"); + + Assert.Empty(insert.Confirm); + Assert.Equal(["DELETE FROM people"], delete.Confirm); + Assert.True(delete.Destructive); + } + + [Fact] + public async Task AFileOpenedReadOnlyKeepsAWriteBack() + { + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await using var session = await _core.OpenFileAsync( + new FileOpenOptions(database.Path, ReadOnly: true), + cancellationToken); + + var write = session.GateRun("SELECT 1; INSERT INTO people (name) VALUES ('Ada')"); + var read = session.GateRun("SELECT * FROM people"); + + Assert.True(write.Refused); + Assert.Empty(write.Confirm); + Assert.False(read.Refused); + } + + [Fact] + public async Task ALevelSetOnAnOpenSessionAppliesAtOnceButNeverAcrossReadOnly() + { + const string Update = "UPDATE people SET name = 'Ada' WHERE id = 1"; + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await using var session = await ConnectAsync(database.Path, SafeMode.Off, cancellationToken); + + var atOff = session.GateRun(Update); + var confirmed = session.SetSafeMode(SafeMode.ConfirmWrites); + var asked = session.GateRun(Update); + var readOnly = session.SetSafeMode(SafeMode.ReadOnly); + var stillAsked = session.GateRun(Update); + + Assert.Empty(atOff.Confirm); + Assert.True(confirmed); + Assert.Equal([Update], asked.Confirm); + Assert.False(readOnly); + Assert.False(stillAsked.Refused); + Assert.Equal([Update], stillAsked.Confirm); + } + + [Fact] + public async Task ASessionThatCannotTakeReadOnlyStillAsksBeforeEveryWrite() + { + const string Update = "UPDATE people SET name = 'Ada' WHERE id = 1"; + var cancellationToken = TestContext.Current.CancellationToken; + await using var database = await SqliteDatabase.CreateAsync(); + await using var session = await ConnectAsync(database.Path, SafeMode.Off, cancellationToken); + + var readOnly = session.SetSafeMode(SafeMode.ReadOnly); + var meanwhile = session.SetSafeMode(SafeMode.ConfirmWrites); + var gate = session.GateRun(Update); + + Assert.False(readOnly); + Assert.True(meanwhile); + Assert.Equal([Update], gate.Confirm); + } + + private async Task ConnectAsync(string path, SafeMode safeMode, CancellationToken cancellationToken) + { + var saved = await _core.Library.SaveAsync( + new SavedConnection( + null, "app", "sqlite", string.Empty, 0, path, string.Empty, TlsSettings.Disabled, safeMode), + ConnectionSecrets.None); + return await _core.ConnectAsync(saved.Id!.Value, cancellationToken: cancellationToken); + } +}