Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`). `open_link` opens only the guide links (`GUIDE_LINKS`).
- 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.
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
ConnectionService::is_custom_dump(&path)
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub const HOMEBREW_OPT_DIRS: [&str; 2] = ["/opt/homebrew/opt", "/usr/local/opt"]
/// Postgres.app keeps each version in a folder like `17` in here.
pub const POSTGRES_APP_VERSIONS_DIR: &str = "/Applications/Postgres.app/Contents/Versions";

/// The EDB installer for macOS keeps each `PostgreSQL` version in a folder like `17` in here.
pub const EDB_MAC_POSTGRES_DIR: &str = "/Library/PostgreSQL";

/// Debian and Ubuntu keep each `PostgreSQL` version in a folder like `17` in here.
pub const LINUX_POSTGRES_DIR: &str = "/usr/lib/postgresql";

Expand All @@ -97,6 +100,20 @@ 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 only web pages the app opens in the browser: the links of the install guide.
/// The frontend has the same links in `src/constants/pgTools.ts`.
pub const GUIDE_LINKS: [&str; 5] = [
"https://postgresapp.com/",
"https://www.postgresql.org/download/macosx/",
"https://wiki.postgresql.org/wiki/Apt",
"https://www.postgresql.org/download/linux/redhat/",
"https://www.postgresql.org/download/windows/",
];

/// The Windows flag `CREATE_NO_WINDOW`. A tool started with it opens no console window.
pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;

Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
111 changes: 84 additions & 27 deletions src-tauri/src/repository/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ 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, GUIDE_LINKS, 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::{
HOMEBREW_OPT_DIRS, LINUX_POSTGRES_DIR, POSTGRES_APP_VERSIONS_DIR, RHEL_POSTGRES_PARENT,
TOOL_DIRS,
EDB_MAC_POSTGRES_DIR, HOMEBREW_OPT_DIRS, LINUX_POSTGRES_DIR, POSTGRES_APP_VERSIONS_DIR,
RHEL_POSTGRES_PARENT, TOOL_DIRS,
};
use crate::error::{AppError, AppResult};
use crate::model::{BackupOptions, ConnectionConfig, Driver, DumpProgress, PgTool, RestoreOptions};
Expand Down Expand Up @@ -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));
Expand All @@ -147,22 +153,45 @@ impl DumpRepository {
/// The tool `name` with this major version. None means the first one found.
async fn pg_tool(name: &str, version: Option<u32>) -> AppResult<PathBuf> {
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}"))
})
}

/// The major version a tool prints with `--version`. None when it does not run.
async fn major_version(tool: &Path) -> Option<u32> {
/// Opens a link of the install guide in the browser. Other links are refused.
pub fn open_link(url: &str) -> AppResult<()> {
if !GUIDE_LINKS.contains(&url) {
return Err("This link cannot 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";
let mut child = std::process::Command::new(opener)
.arg(url)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
// Wait for the opener, so no finished process is left behind.
std::thread::spawn(move || {
let _ = child.wait();
});
Ok(())
}

/// The version a tool prints with `--version`, like `17.2`. None when it does not run.
async fn release(tool: &Path) -> Option<String> {
let mut command = Command::new(tool);
super::hide_console(&mut command);
let output = command
Expand All @@ -171,15 +200,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 major version in text like `pg_dump (PostgreSQL) 17.2 (Homebrew)`.
fn parse_major(text: &str) -> Option<u32> {
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 version in text like `pg_dump (PostgreSQL) 17.2 (Homebrew)`: `17.2`.
fn parse_release(text: &str) -> Option<String> {
text.split_whitespace()
.find(|word| word.starts_with(|c: char| c.is_ascii_digit()))
.map(str::to_owned)
}

/// The major version in a version like `17.2` or `18beta1`.
fn parse_major(release: &str) -> Option<u32> {
let digits: String = release.chars().take_while(char::is_ascii_digit).collect();
digits.parse().ok()
}

Expand Down Expand Up @@ -437,6 +470,7 @@ impl DumpRepository {
}
}
folders.extend(Self::bin_folders(Path::new(POSTGRES_APP_VERSIONS_DIR), ""));
folders.extend(Self::bin_folders(Path::new(EDB_MAC_POSTGRES_DIR), ""));
folders.extend(Self::bin_folders(Path::new(LINUX_POSTGRES_DIR), ""));
folders.extend(Self::bin_folders(Path::new(RHEL_POSTGRES_PARENT), "pgsql-"));
}
Expand Down Expand Up @@ -714,12 +748,34 @@ 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_the_guide_links() {
assert!(DumpRepository::open_link("file:///etc/passwd").is_err());
assert!(DumpRepository::open_link("https://example.com/").is_err());
assert!(DumpRepository::open_link("https://postgresapp.com/,x").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);
}
Expand All @@ -728,6 +784,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)];
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading