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
7 changes: 7 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ 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`).
- 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.
- `restore_dump` loads a `.dump` file with `pg_restore --no-owner --no-privileges` into the current
database, optionally with `--clean --if-exists`. It uses the same progress, cancel map and `DumpLog`.
pg_restore goes on after errors; the error then counts them. The SQL import sends a file that starts
with `PGDMP` to the restore dialog.
- **AI notes** (`service/notes.rs`): each database has a `claude.md` file at
`~/.sql-pro/<database>-<hash>/claude.md` (`~/.sql-pro-dev/` in a debug build). The hash (FNV-1a, 8 hex) is
of driver, server host, port and database (SQLite: the file path), not the user. The user edits it in Ask AI. The AI adds facts to it (`remember`).
Expand Down
39 changes: 33 additions & 6 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use tauri::ipc::Channel;

use crate::error::{AppError, AppResult};
use crate::model::{
AccessLevel, AiOptions, Attachment, BrowseRequest, ClaudeReply, ColumnInfo, ColumnSpec,
ConnectionConfig, CsvImport, CsvPreview, DataDiff, DataKinds, DbObject, DbProcess, Diagram,
DumpProgress, ExportRequest, NewColumn, NewForeignKey, NewIndex, NewUser, Notes, QueryPlan,
QueryStat, ResultSet, RowUpdate, RowsExport, RowsSql, SaveResult, SchemaChange, SyncSql,
TableKeys, TablePage, TableRef, UserList, Values,
AccessLevel, AiOptions, Attachment, BackupOptions, BrowseRequest, ClaudeReply, ColumnInfo,
ColumnSpec, ConnectionConfig, CsvImport, CsvPreview, DataDiff, DataKinds, DbObject, DbProcess,
Diagram, DumpProgress, ExportRequest, NewColumn, NewForeignKey, NewIndex, NewUser, Notes,
PgTools, QueryPlan, QueryStat, RestoreOptions, ResultSet, RowUpdate, RowsExport, RowsSql,
SaveResult, SchemaChange, SyncSql, TableKeys, TablePage, TableRef, UserList, Values,
};
use crate::open_file::OpenedFiles;
use crate::repository::PasswordRepository;
Expand Down Expand Up @@ -374,10 +374,11 @@ pub async fn dump_database(
service: Service<'_>,
id: String,
path: PathBuf,
options: BackupOptions,
progress: Channel<DumpProgress>,
) -> AppResult<()> {
service
.dump_database(&id, &path, |step| {
.dump_database(&id, &path, &options, |step| {
// The page may be gone. The backup goes on anyway.
let _ = progress.send(step);
})
Expand All @@ -389,6 +390,32 @@ pub fn cancel_dump(service: Service<'_>, id: String) {
service.cancel_dump(&id);
}

#[tauri::command]
pub async fn restore_dump(
service: Service<'_>,
id: String,
path: PathBuf,
options: RestoreOptions,
progress: Channel<DumpProgress>,
) -> AppResult<()> {
service
.restore_dump(&id, &path, &options, |step| {
// The page may be gone. The restore goes on anyway.
let _ = progress.send(step);
})
.await
}

#[tauri::command]
pub async fn pg_tools(service: Service<'_>, id: String, restore: bool) -> AppResult<PgTools> {
service.pg_tools(&id, restore).await
}

#[tauri::command]
pub fn is_custom_dump(path: PathBuf) -> AppResult<bool> {
ConnectionService::is_custom_dump(&path)
}

#[tauri::command]
pub async fn export_tables(
service: Service<'_>,
Expand Down
26 changes: 25 additions & 1 deletion src-tauri/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,31 @@ pub const TOOL_DIRS: [&str; 8] = [
];

/// Homebrew keeps each `PostgreSQL` version in a folder like `postgresql@16` in here.
pub const HOMEBREW_OPT_DIR: &str = "/opt/homebrew/opt";
/// The first is Apple silicon, the second Intel.
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";

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

/// RHEL and Fedora keep each `PostgreSQL` version in a folder like `/usr/pgsql-17`.
pub const RHEL_POSTGRES_PARENT: &str = "/usr";

/// The first bytes of a custom format file of `pg_dump`. Only `pg_restore` reads it.
pub const PG_CUSTOM_MAGIC: &[u8; 5] = b"PGDMP";

/// The error when a custom format file of `pg_dump` is opened as a SQL file.
pub const PG_CUSTOM_DUMP: &str =
"This file is a pg_dump custom archive, not SQL. Use Restore from .dump… to load it";

/// The same error on a database that is not `PostgreSQL`. It has no restore.
pub const PG_CUSTOM_DUMP_ELSEWHERE: &str = "This file is a PostgreSQL custom backup, not SQL. \
It can only be restored into a PostgreSQL database";

/// 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";

/// 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
3 changes: 3 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub fn run() {
commands::drop_object,
commands::dump_database,
commands::cancel_dump,
commands::restore_dump,
commands::pg_tools,
commands::is_custom_dump,
commands::export_tables,
commands::export_rows,
commands::rows_insert_sql,
Expand Down
40 changes: 40 additions & 0 deletions src-tauri/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,46 @@ pub struct DumpProgress {
pub tables: usize,
}

/// How to back up a `PostgreSQL` database. Other databases ignore it.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct BackupOptions {
/// True for the custom format of `pg_dump`, which `pg_restore` reads. Else plain SQL.
pub custom: bool,
/// The major version of `pg_dump` to use. None means the default one.
pub version: Option<u32>,
}

/// How to load a `pg_restore` file into the current database.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct RestoreOptions {
/// The major version of `pg_restore` to use. None means the default one.
pub version: Option<u32>,
/// Drop each object before it is made again (`--clean --if-exists`).
pub clean: bool,
}

/// A `pg_dump` or `pg_restore` that is installed on this computer.
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
pub struct PgTool {
/// The major version, like 17.
pub version: u32,
pub path: PathBuf,
}

/// The versions of one `PostgreSQL` tool on this computer, and the one to use by default.
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PgTools {
/// The major version of the server, like 17.
pub server: u32,
/// Newest first.
pub tools: Vec<PgTool>,
/// The tool with the version of the server, else the newest one. None when none was found.
pub default_version: Option<u32>,
}

/// What to export, and where to write it.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
Loading
Loading