From 149783536622f3fb91f4c5ba67b58b5a3bdeceea Mon Sep 17 00:00:00 2001 From: necessarylion Date: Sun, 8 Nov 2026 08:31:43 +0700 Subject: [PATCH 1/2] Always list PostgreSQL 18 to 12 in the backup dialog and show how to install a missing version The backup and restore dialog listed only the pg_dump and pg_restore versions found on this computer. Now it always lists PostgreSQL 18 to 12. A missing version is marked "not installed". When the user picks it, the button is off and a short install guide for this platform shows, with a Copy button and a Check again button. The app does not download or install any tool. Also: - An installed version shows its full version, like PostgreSQL 17.6. - The "not found" error now points to the guide. - A slow tool list from an older open of the dialog is dropped. - A cancelled restore keeps its status when the reload of the tables fails. --- CLAUDE.md | 2 + src-tauri/src/commands.rs | 5 ++ src-tauri/src/constants/mod.rs | 7 ++ src-tauri/src/lib.rs | 1 + src-tauri/src/model.rs | 2 + src-tauri/src/repository/dump.rs | 101 +++++++++++++++++++------- src-tauri/src/service.rs | 5 ++ src/components/PgToolDialog.vue | 116 ++++++++++++++++++++++++++--- src/constants/pgTools.ts | 20 +++++ src/main.ts | 3 + src/services/DatabaseApi.ts | 5 ++ src/services/PgInstallGuide.ts | 86 ++++++++++++++++++++++ src/services/PgToolChoice.ts | 121 +++++++++++++++++++++++++++---- src/services/Session.ts | 4 +- src/services/keys.ts | 2 + src/types.ts | 2 + tests/Backup.test.ts | 32 +++----- tests/PgTools.test.ts | 120 ++++++++++++++++++++++++++++++ 18 files changed, 563 insertions(+), 71 deletions(-) create mode 100644 src/constants/pgTools.ts create mode 100644 src/services/PgInstallGuide.ts create mode 100644 tests/PgTools.test.ts diff --git a/CLAUDE.md b/CLAUDE.md index 388128e..ad33fc2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -106,6 +106,8 @@ Constants are in `constants/mod.rs`. Errors are `AppError` (`error.rs`), which r table and keeps only the real error lines. Progress (file size, time, table) goes to the frontend through a `Channel` about once per second. `cancel_dump` kills the tool and deletes the `.part` file. Esc stops a backup only when Escape has no other job there (`Shortcuts.cancelsBackup`). + The PostgreSQL dialog always lists versions 18 to 12 (`PG_TOOL_VERSIONS`). The app never installs a tool: + a missing version shows an install guide for the platform (`PgInstallGuide`, links open through `open_link`). - PostgreSQL only (`PgToolDialog.vue`): the user picks plain SQL or custom format (`--format=custom`, a `.dump` file) and the pg_dump version. `DumpRepository::pg_tools` lists the installed versions (`--version`). The default is the server version (`SHOW server_version_num`), else the newest. diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 090b04e..bba3ed3 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -411,6 +411,11 @@ pub async fn pg_tools(service: Service<'_>, id: String, restore: bool) -> AppRes service.pg_tools(&id, restore).await } +#[tauri::command] +pub fn open_link(url: String) -> AppResult<()> { + ConnectionService::open_link(&url) +} + #[tauri::command] pub fn is_custom_dump(path: PathBuf) -> AppResult { ConnectionService::is_custom_dump(&path) diff --git a/src-tauri/src/constants/mod.rs b/src-tauri/src/constants/mod.rs index b5fba1a..59efb18 100644 --- a/src-tauri/src/constants/mod.rs +++ b/src-tauri/src/constants/mod.rs @@ -97,6 +97,13 @@ pub const PG_CUSTOM_DUMP_ELSEWHERE: &str = "This file is a PostgreSQL custom bac /// The error when a restore runs on a database that is not `PostgreSQL`. pub const RESTORE_ONLY_POSTGRES: &str = "Restore from .dump works only with PostgreSQL"; +/// Added to the error when a `pg_dump` or `pg_restore` is missing. The dialog has the guide. +pub const PG_TOOL_HINT: &str = + "Pick the version in the backup dialog to see how to install it, then press Check again."; + +/// The start of a link the app may open in the browser. +pub const LINK_PREFIX: &str = "https://"; + /// The Windows flag `CREATE_NO_WINDOW`. A tool started with it opens no console window. pub const CREATE_NO_WINDOW: u32 = 0x0800_0000; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a8bfebf..0ad1518 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -99,6 +99,7 @@ pub fn run() { commands::restore_dump, commands::pg_tools, commands::is_custom_dump, + commands::open_link, commands::export_tables, commands::export_rows, commands::rows_insert_sql, diff --git a/src-tauri/src/model.rs b/src-tauri/src/model.rs index fa4f228..904e8c5 100644 --- a/src-tauri/src/model.rs +++ b/src-tauri/src/model.rs @@ -546,6 +546,8 @@ pub struct RestoreOptions { pub struct PgTool { /// The major version, like 17. pub version: u32, + /// The full version the tool prints, like `17.6`. + pub release: String, pub path: PathBuf, } diff --git a/src-tauri/src/repository/dump.rs b/src-tauri/src/repository/dump.rs index cfa82c0..dedc4e1 100644 --- a/src-tauri/src/repository/dump.rs +++ b/src-tauri/src/repository/dump.rs @@ -17,8 +17,9 @@ use tokio::io::{AsyncBufReadExt as _, BufReader}; use tokio::process::Command; use crate::constants::{ - BACKUP_CANCELLED, DUMP_PROGRESS_EVERY, MSSQL_NO_BACKUP, MYSQL_PASSWORD_ENV, - PARTIAL_DUMP_SUFFIX, PG_CUSTOM_MAGIC, POSTGRES_PASSWORD_ENV, RESTORE_ONLY_POSTGRES, + BACKUP_CANCELLED, DUMP_PROGRESS_EVERY, LINK_PREFIX, MSSQL_NO_BACKUP, MYSQL_PASSWORD_ENV, + PARTIAL_DUMP_SUFFIX, PG_CUSTOM_MAGIC, PG_TOOL_HINT, POSTGRES_PASSWORD_ENV, + RESTORE_ONLY_POSTGRES, }; #[cfg(unix)] use crate::constants::{ @@ -125,10 +126,15 @@ impl DumpRepository { if !real.is_file() || !seen.insert(real) { continue; } - if let Some(version) = Self::major_version(&path).await + if let Some(release) = Self::release(&path).await + && let Some(version) = Self::parse_major(&release) && !tools.iter().any(|tool| tool.version == version) { - tools.push(PgTool { version, path }); + tools.push(PgTool { + version, + release, + path, + }); } } tools.sort_by_key(|tool| std::cmp::Reverse(tool.version)); @@ -147,22 +153,41 @@ impl DumpRepository { /// The tool `name` with this major version. None means the first one found. async fn pg_tool(name: &str, version: Option) -> AppResult { let Some(version) = version else { - return Self::find(&[name]).ok_or_else(|| { - AppError::Message(format!( - "{name} was not found. Install the PostgreSQL client tools (macOS: brew install libpq)" - )) - }); + return Self::find(&[name]) + .ok_or_else(|| AppError::Message(format!("{name} was not found. {PG_TOOL_HINT}"))); }; Self::pg_tools(name) .await .into_iter() .find(|tool| tool.version == version) .map(|tool| tool.path) - .ok_or_else(|| AppError::Message(format!("{name} {version} was not found"))) + .ok_or_else(|| { + AppError::Message(format!("{name} {version} was not found. {PG_TOOL_HINT}")) + }) + } + + /// Opens a web page in the browser, like an install guide. Only `https://` links. + pub fn open_link(url: &str) -> AppResult<()> { + if !url.starts_with(LINK_PREFIX) || url.chars().any(char::is_whitespace) { + return Err("Only https links can be opened".into()); + } + #[cfg(target_os = "macos")] + let opener = "open"; + #[cfg(windows)] + let opener = "explorer"; + #[cfg(all(unix, not(target_os = "macos")))] + let opener = "xdg-open"; + std::process::Command::new(opener) + .arg(url) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()?; + Ok(()) } - /// The major version a tool prints with `--version`. None when it does not run. - async fn major_version(tool: &Path) -> Option { + /// The version a tool prints with `--version`, like `17.2`. None when it does not run. + async fn release(tool: &Path) -> Option { let mut command = Command::new(tool); super::hide_console(&mut command); let output = command @@ -171,15 +196,19 @@ impl DumpRepository { .output() .await .ok()?; - Self::parse_major(&String::from_utf8_lossy(&output.stdout)) + Self::parse_release(&String::from_utf8_lossy(&output.stdout)) + } + + /// The version in text like `pg_dump (PostgreSQL) 17.2 (Homebrew)`: `17.2`. + fn parse_release(text: &str) -> Option { + text.split_whitespace() + .find(|word| word.starts_with(|c: char| c.is_ascii_digit())) + .map(str::to_owned) } - /// The major version in text like `pg_dump (PostgreSQL) 17.2 (Homebrew)`. - fn parse_major(text: &str) -> Option { - let word = text - .split_whitespace() - .find(|word| word.starts_with(|c: char| c.is_ascii_digit()))?; - let digits: String = word.chars().take_while(char::is_ascii_digit).collect(); + /// The major version in a version like `17.2` or `18beta1`. + fn parse_major(release: &str) -> Option { + let digits: String = release.chars().take_while(char::is_ascii_digit).collect(); digits.parse().ok() } @@ -714,12 +743,33 @@ mod tests { } #[test] - fn reads_the_major_version_of_the_tools() { - let parse = DumpRepository::parse_major; - assert_eq!(parse("pg_dump (PostgreSQL) 17.2\n"), Some(17)); - assert_eq!(parse("pg_restore (PostgreSQL) 15.10 (Homebrew)"), Some(15)); - assert_eq!(parse("pg_dump (PostgreSQL) 18beta1"), Some(18)); - assert_eq!(parse("pg_dump (PostgreSQL) 9.6.24"), Some(9)); + fn opens_only_https_links() { + assert!(DumpRepository::open_link("file:///etc/passwd").is_err()); + assert!(DumpRepository::open_link("https://a.b/ --flag").is_err()); + } + + #[test] + fn reads_the_version_of_the_tools() { + let parse = |text| { + let release = DumpRepository::parse_release(text)?; + Some((DumpRepository::parse_major(&release)?, release)) + }; + assert_eq!( + parse("pg_dump (PostgreSQL) 17.2\n"), + Some((17, "17.2".into())) + ); + assert_eq!( + parse("pg_restore (PostgreSQL) 15.10 (Homebrew)"), + Some((15, "15.10".into())) + ); + assert_eq!( + parse("pg_dump (PostgreSQL) 18beta1"), + Some((18, "18beta1".into())) + ); + assert_eq!( + parse("pg_dump (PostgreSQL) 9.6.24"), + Some((9, "9.6.24".into())) + ); assert_eq!(parse("command not found"), None); assert_eq!(parse(""), None); } @@ -728,6 +778,7 @@ mod tests { fn the_default_version_is_the_one_of_the_server() { let tool = |version| PgTool { version, + release: format!("{version}.1"), path: format!("/pg{version}/pg_dump").into(), }; let tools = [tool(18), tool(17), tool(15)]; diff --git a/src-tauri/src/service.rs b/src-tauri/src/service.rs index 579d155..a961f83 100644 --- a/src-tauri/src/service.rs +++ b/src-tauri/src/service.rs @@ -631,6 +631,11 @@ impl ConnectionService { DumpRepository::is_custom_dump(path) } + /// Opens an `https://` web page in the browser. + pub fn open_link(url: &str) -> AppResult<()> { + DumpRepository::open_link(url) + } + /// The login of a tab for a dump tool. With an SSH tunnel it goes through the same /// tunnel as the tab. The tunnel stays open while `connection` lives. fn tool_config(connection: &Connection) -> ConnectionConfig { diff --git a/src/components/PgToolDialog.vue b/src/components/PgToolDialog.vue index 4f17e35..fc53e90 100644 --- a/src/components/PgToolDialog.vue +++ b/src/components/PgToolDialog.vue @@ -1,15 +1,21 @@