mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-18 16:08:55 +00:00
feat(database): export with pg_dump and restore with pg_restore / psql (#3718)
PostgreSQL data sources, databases, schemas and tables get Import/Export > Export with 'pg_dump'... and Restore with 'pg_restore'/'psql'.... ide-db finds the installed client tools, turns the dialog's options into the command line, and runs it connected like the data source (SSH tunnel, SSL settings, read-only flag), with the password in PGPASSWORD and never in the arguments. Output streams to the UI; a RequestId cancels the run, which continues after the dialog closes and ends with a notification. The dialogs preselect the client matching the server's major version and warn when it does not fit; Restore reads the dump header to pick psql or pg_restore. Not offered in remote windows. Co-authored-by: EjaYF <9842286+EjaYF@users.noreply.github.com>
This commit is contained in:
co-authored by
EjaYF
parent
921378d531
commit
9bf1f320eb
@@ -14,6 +14,7 @@ mod snowflake;
|
||||
pub mod source;
|
||||
pub mod split;
|
||||
pub mod sql;
|
||||
pub mod tools;
|
||||
mod watch;
|
||||
pub use watch::SourcesWatch;
|
||||
|
||||
@@ -787,6 +788,56 @@ impl DbManager {
|
||||
Ok(v.unwrap_or_else(|| "Connected".into()))
|
||||
}
|
||||
|
||||
/// Runs `job` with the client tool at `executable` against target `id`, connected like the
|
||||
/// data source (through its SSH tunnel, with its SSL settings and password), until it exits
|
||||
/// or `cancel` fires. Connects first, so a missing password fails the way it does for a
|
||||
/// console. Returns the exit code.
|
||||
pub async fn run_tool(
|
||||
&self,
|
||||
id: &str,
|
||||
executable: &std::path::Path,
|
||||
job: &tools::DbToolJob,
|
||||
cancel: &CancellationToken,
|
||||
on_output: impl FnMut(Vec<tools::ToolLine>),
|
||||
) -> IdeResult<Option<i32>> {
|
||||
let tool = job.tool();
|
||||
if !tool.is_program(executable) {
|
||||
return Err(IdeError::invalid(format!("{} is not {}", executable.display(), tool.program())));
|
||||
}
|
||||
let source = self.source(id)?;
|
||||
if source.driver != tool.driver() {
|
||||
return Err(IdeError::invalid(format!("{} cannot connect to {}", tool.program(), source.name)));
|
||||
}
|
||||
self.pool(id).await?;
|
||||
let password = self.password(&source).await;
|
||||
// Holding the tunnel keeps it open while the tool runs, even if the tree disconnects.
|
||||
let (target, _tunnel) = self.endpoint(&source, None, true).await?;
|
||||
let prepared = tools::prepare(job, &target, password.as_deref());
|
||||
tools::run(executable, &prepared.args, &prepared.env, cancel, on_output).await
|
||||
}
|
||||
|
||||
/// The command [`DbManager::run_tool`] would run, for the dialogs' preview: the data source's
|
||||
/// own host and port (not a tunnel's local end), and no password.
|
||||
pub fn tool_preview(&self, id: &str, executable: &std::path::Path, job: &tools::DbToolJob) -> IdeResult<String> {
|
||||
let source = self.source(id)?;
|
||||
let prepared = tools::prepare(job, &source, None);
|
||||
Ok(tools::command_line(executable, &prepared.args))
|
||||
}
|
||||
|
||||
/// The PostgreSQL server's `server_version_num` (`150013` for 15.13).
|
||||
pub async fn server_version(&self, id: &str) -> IdeResult<u32> {
|
||||
let pool = self.pool(id).await?;
|
||||
if pool.driver() != DbDriver::Postgres {
|
||||
return Err(IdeError::invalid("not a PostgreSQL data source"));
|
||||
}
|
||||
let rows = pool.rows("SHOW server_version_num", 1).await?;
|
||||
rows.into_iter()
|
||||
.next()
|
||||
.and_then(|r| r.into_iter().next().flatten())
|
||||
.and_then(|v| v.trim().parse().ok())
|
||||
.ok_or_else(|| IdeError::internal("no server version"))
|
||||
}
|
||||
|
||||
pub fn is_connected(&self, id: &str) -> bool {
|
||||
self.pools.lock().contains_key(id)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,339 @@
|
||||
//! What a dump or restore dialog asks for, as typed options, and the arguments that do it.
|
||||
//! The connection arguments come from [`super::connection`]; these are the rest.
|
||||
|
||||
use super::DbTool;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DbDumpFormat {
|
||||
/// SQL text, restored with `psql`.
|
||||
Plain,
|
||||
Custom,
|
||||
/// A directory of files; the only format `pg_dump` writes in parallel.
|
||||
Directory,
|
||||
Tar,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DbDumpStatements {
|
||||
Copy,
|
||||
Inserts,
|
||||
ColumnInserts,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DbDumpContent {
|
||||
All,
|
||||
SchemaOnly,
|
||||
DataOnly,
|
||||
}
|
||||
|
||||
/// Name lists hold `pg_dump` patterns (`*`, `?`, quoted names).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DbPgDumpOptions {
|
||||
pub format: DbDumpFormat,
|
||||
pub statements: DbDumpStatements,
|
||||
pub content: DbDumpContent,
|
||||
pub schemas: Vec<String>,
|
||||
pub tables: Vec<String>,
|
||||
pub exclude_tables: Vec<String>,
|
||||
/// Plain format only: archives get DROP / CREATE at restore.
|
||||
pub clean: bool,
|
||||
pub if_exists: bool,
|
||||
pub create: bool,
|
||||
pub no_owner: bool,
|
||||
pub no_privileges: bool,
|
||||
/// Directory format only.
|
||||
pub jobs: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DbPgRestoreOptions {
|
||||
pub content: DbDumpContent,
|
||||
pub schemas: Vec<String>,
|
||||
pub clean: bool,
|
||||
pub if_exists: bool,
|
||||
pub no_owner: bool,
|
||||
pub no_privileges: bool,
|
||||
pub single_transaction: bool,
|
||||
pub exit_on_error: bool,
|
||||
/// Ignored with a single transaction, which `pg_restore` cannot split.
|
||||
pub jobs: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DbPsqlOptions {
|
||||
pub single_transaction: bool,
|
||||
pub stop_on_error: bool,
|
||||
}
|
||||
|
||||
/// A run of a client tool: which one, the file it writes or reads, and its options.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "tool", rename_all = "camelCase")]
|
||||
pub enum DbToolJob {
|
||||
PgDump {
|
||||
file: String,
|
||||
options: DbPgDumpOptions,
|
||||
},
|
||||
/// `file`: an archive, or a directory archive.
|
||||
PgRestore {
|
||||
file: String,
|
||||
options: DbPgRestoreOptions,
|
||||
},
|
||||
Psql {
|
||||
file: String,
|
||||
options: DbPsqlOptions,
|
||||
},
|
||||
}
|
||||
|
||||
impl DbToolJob {
|
||||
pub fn tool(&self) -> DbTool {
|
||||
match self {
|
||||
DbToolJob::PgDump { .. } => DbTool::PgDump,
|
||||
DbToolJob::PgRestore { .. } => DbTool::PgRestore,
|
||||
DbToolJob::Psql { .. } => DbTool::Psql,
|
||||
}
|
||||
}
|
||||
|
||||
/// The arguments after the connection ones.
|
||||
pub fn args(&self) -> Vec<String> {
|
||||
match self {
|
||||
DbToolJob::PgDump { file, options } => pg_dump(options, file),
|
||||
DbToolJob::PgRestore { file, options } => pg_restore(options, file),
|
||||
DbToolJob::Psql { file, options } => psql(options, file),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn flag(on: bool, flag: &str) -> Option<String> {
|
||||
on.then(|| flag.to_owned())
|
||||
}
|
||||
|
||||
fn content(c: DbDumpContent) -> Option<String> {
|
||||
match c {
|
||||
DbDumpContent::All => None,
|
||||
DbDumpContent::SchemaOnly => Some("--schema-only".into()),
|
||||
DbDumpContent::DataOnly => Some("--data-only".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn each(names: &[String], option: &str) -> impl Iterator<Item = String> {
|
||||
names.iter().map(|n| n.trim()).filter(|n| !n.is_empty()).map(move |n| format!("--{option}={n}"))
|
||||
}
|
||||
|
||||
fn pg_dump(o: &DbPgDumpOptions, file: &str) -> Vec<String> {
|
||||
let plain = o.format == DbDumpFormat::Plain;
|
||||
let format = match o.format {
|
||||
DbDumpFormat::Plain => "p",
|
||||
DbDumpFormat::Custom => "c",
|
||||
DbDumpFormat::Directory => "d",
|
||||
DbDumpFormat::Tar => "t",
|
||||
};
|
||||
let mut args = vec![format!("--format={format}"), format!("--file={file}")];
|
||||
args.extend(match o.statements {
|
||||
DbDumpStatements::Copy => None,
|
||||
DbDumpStatements::Inserts => Some("--inserts".to_owned()),
|
||||
DbDumpStatements::ColumnInserts => Some("--column-inserts".to_owned()),
|
||||
});
|
||||
args.extend(content(o.content));
|
||||
args.extend(each(&o.schemas, "schema"));
|
||||
args.extend(each(&o.tables, "table"));
|
||||
args.extend(each(&o.exclude_tables, "exclude-table"));
|
||||
args.extend(flag(plain && o.clean, "--clean"));
|
||||
args.extend(flag(plain && o.clean && o.if_exists, "--if-exists"));
|
||||
args.extend(flag(plain && o.create, "--create"));
|
||||
args.extend(flag(o.no_owner, "--no-owner"));
|
||||
args.extend(flag(o.no_privileges, "--no-privileges"));
|
||||
if o.format == DbDumpFormat::Directory && o.jobs > 1 {
|
||||
args.push(format!("--jobs={}", o.jobs));
|
||||
}
|
||||
args.push("--verbose".into());
|
||||
args
|
||||
}
|
||||
|
||||
fn pg_restore(o: &DbPgRestoreOptions, file: &str) -> Vec<String> {
|
||||
let mut args: Vec<String> = content(o.content).into_iter().collect();
|
||||
args.extend(each(&o.schemas, "schema"));
|
||||
args.extend(flag(o.clean, "--clean"));
|
||||
args.extend(flag(o.clean && o.if_exists, "--if-exists"));
|
||||
args.extend(flag(o.no_owner, "--no-owner"));
|
||||
args.extend(flag(o.no_privileges, "--no-privileges"));
|
||||
args.extend(flag(o.single_transaction, "--single-transaction"));
|
||||
args.extend(flag(o.exit_on_error, "--exit-on-error"));
|
||||
if o.jobs > 1 && !o.single_transaction {
|
||||
args.push(format!("--jobs={}", o.jobs));
|
||||
}
|
||||
args.push("--verbose".into());
|
||||
if !file.is_empty() {
|
||||
args.push(file.to_owned());
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
fn psql(o: &DbPsqlOptions, file: &str) -> Vec<String> {
|
||||
let mut args: Vec<String> = (!file.is_empty()).then(|| format!("--file={file}")).into_iter().collect();
|
||||
args.extend(flag(o.single_transaction, "--single-transaction"));
|
||||
args.extend(flag(o.stop_on_error, "--set=ON_ERROR_STOP=1"));
|
||||
args
|
||||
}
|
||||
|
||||
/// The command as a shell would need it typed: the dialogs' preview.
|
||||
pub fn command_line(program: &Path, args: &[String]) -> String {
|
||||
let quote = |s: &str| {
|
||||
if !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || "@%+=:,./-_".contains(c)) {
|
||||
s.to_owned()
|
||||
} else {
|
||||
format!("'{}'", s.replace('\'', "'\\''"))
|
||||
}
|
||||
};
|
||||
std::iter::once(program.to_string_lossy().into_owned())
|
||||
.chain(args.iter().cloned())
|
||||
.map(|a| quote(&a))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dump() -> DbPgDumpOptions {
|
||||
DbPgDumpOptions {
|
||||
format: DbDumpFormat::Plain,
|
||||
statements: DbDumpStatements::Copy,
|
||||
content: DbDumpContent::All,
|
||||
schemas: vec![],
|
||||
tables: vec![],
|
||||
exclude_tables: vec![],
|
||||
clean: false,
|
||||
if_exists: false,
|
||||
create: false,
|
||||
no_owner: false,
|
||||
no_privileges: false,
|
||||
jobs: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn restore() -> DbPgRestoreOptions {
|
||||
DbPgRestoreOptions {
|
||||
content: DbDumpContent::All,
|
||||
schemas: vec![],
|
||||
clean: false,
|
||||
if_exists: false,
|
||||
no_owner: false,
|
||||
no_privileges: false,
|
||||
single_transaction: false,
|
||||
exit_on_error: true,
|
||||
jobs: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn args(job: DbToolJob) -> Vec<String> {
|
||||
job.args()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dumps_plain_sql_with_copy_by_default() {
|
||||
let job = DbToolJob::PgDump { file: "/tmp/shop.sql".into(), options: dump() };
|
||||
assert_eq!(args(job), ["--format=p", "--file=/tmp/shop.sql", "--verbose"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn turns_lists_and_switches_into_flags() {
|
||||
let options = DbPgDumpOptions {
|
||||
statements: DbDumpStatements::ColumnInserts,
|
||||
content: DbDumpContent::DataOnly,
|
||||
schemas: vec!["public".into(), "audit".into()],
|
||||
tables: vec!["public.orders".into()],
|
||||
exclude_tables: vec!["public.log_*".into()],
|
||||
clean: true,
|
||||
if_exists: true,
|
||||
create: true,
|
||||
no_owner: true,
|
||||
..dump()
|
||||
};
|
||||
assert_eq!(
|
||||
args(DbToolJob::PgDump { file: "/tmp/x.sql".into(), options }),
|
||||
[
|
||||
"--format=p",
|
||||
"--file=/tmp/x.sql",
|
||||
"--column-inserts",
|
||||
"--data-only",
|
||||
"--schema=public",
|
||||
"--schema=audit",
|
||||
"--table=public.orders",
|
||||
"--exclude-table=public.log_*",
|
||||
"--clean",
|
||||
"--if-exists",
|
||||
"--create",
|
||||
"--no-owner",
|
||||
"--verbose",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_drop_and_create_to_the_restore_of_an_archive() {
|
||||
let options = DbPgDumpOptions { format: DbDumpFormat::Custom, clean: true, create: true, ..dump() };
|
||||
let a = args(DbToolJob::PgDump { file: "/tmp/x.dump".into(), options });
|
||||
assert!(!a.iter().any(|a| a == "--clean" || a == "--create"), "{a:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runs_parallel_jobs_only_where_the_tool_can() {
|
||||
let dir = DbPgDumpOptions { format: DbDumpFormat::Directory, jobs: 4, ..dump() };
|
||||
assert!(args(DbToolJob::PgDump { file: "/tmp/d".into(), options: dir.clone() }).contains(&"--jobs=4".into()));
|
||||
let custom = DbPgDumpOptions { format: DbDumpFormat::Custom, ..dir };
|
||||
assert!(!args(DbToolJob::PgDump { file: "/tmp/d".into(), options: custom }).contains(&"--jobs=4".into()));
|
||||
let single = DbPgRestoreOptions { jobs: 4, single_transaction: true, ..restore() };
|
||||
let a = args(DbToolJob::PgRestore { file: "/tmp/x".into(), options: single });
|
||||
assert!(a.contains(&"--single-transaction".into()) && !a.iter().any(|a| a.starts_with("--jobs")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restores_the_archive_named_last_and_stops_on_the_first_error() {
|
||||
assert_eq!(
|
||||
args(DbToolJob::PgRestore { file: "/tmp/shop.dump".into(), options: restore() }),
|
||||
["--exit-on-error", "--verbose", "/tmp/shop.dump"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_out_an_archive_not_chosen_yet() {
|
||||
let a = args(DbToolJob::PgRestore { file: String::new(), options: restore() });
|
||||
assert_eq!(a, ["--exit-on-error", "--verbose"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runs_a_sql_file_in_one_transaction_that_stops_on_error() {
|
||||
let options = DbPsqlOptions { single_transaction: true, stop_on_error: true };
|
||||
assert_eq!(
|
||||
args(DbToolJob::Psql { file: "/tmp/shop.sql".into(), options }),
|
||||
["--file=/tmp/shop.sql", "--single-transaction", "--set=ON_ERROR_STOP=1"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quotes_what_a_shell_would_split() {
|
||||
let line = command_line(
|
||||
Path::new("/usr/bin/pg_dump"),
|
||||
&["--port=5432".into(), "--file=/tmp/my dump.sql".into(), "--table=it's".into()],
|
||||
);
|
||||
assert_eq!(line, r#"/usr/bin/pg_dump --port=5432 '--file=/tmp/my dump.sql' '--table=it'\''s'"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_the_job_the_dialog_sends() {
|
||||
let json = r#"{"tool":"psql","file":"/tmp/a.sql","options":{"singleTransaction":true,"stopOnError":false}}"#;
|
||||
let job: DbToolJob = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(job.tool(), DbTool::Psql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,536 @@
|
||||
//! Client tools of a database (`pg_dump`, `pg_restore`, `psql`): finding installed copies, the
|
||||
//! arguments and environment that connect them like the data source, and running them.
|
||||
|
||||
mod jobs;
|
||||
pub use jobs::*;
|
||||
|
||||
use crate::source::{DataSource, DbDriver, DbSslMode};
|
||||
use ide_core::{CancellationToken, IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::time::Duration;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DbTool {
|
||||
PgDump,
|
||||
PgRestore,
|
||||
Psql,
|
||||
}
|
||||
|
||||
impl DbTool {
|
||||
pub fn program(self) -> &'static str {
|
||||
self.programs()[0]
|
||||
}
|
||||
|
||||
/// Executable names.
|
||||
pub fn programs(self) -> &'static [&'static str] {
|
||||
match self {
|
||||
DbTool::PgDump => &["pg_dump"],
|
||||
DbTool::PgRestore => &["pg_restore"],
|
||||
DbTool::Psql => &["psql"],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn driver(self) -> DbDriver {
|
||||
DbDriver::Postgres
|
||||
}
|
||||
|
||||
/// Whether `path` names this tool (`pg_dump`, `pg_dump.exe`, `pg_dump-17`…).
|
||||
/// The data source's password is handed only to such a program.
|
||||
pub fn is_program(self, path: &Path) -> bool {
|
||||
let Some(stem) = path.file_stem().and_then(OsStr::to_str) else { return false };
|
||||
self.programs().iter().any(|p| {
|
||||
stem.strip_prefix(p).is_some_and(|rest| {
|
||||
let digits = rest.strip_prefix(['-', '.', '_']).unwrap_or(rest);
|
||||
digits.chars().all(|c| c.is_ascii_digit())
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// An installed copy of a tool.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase", rename = "DbToolInstall")]
|
||||
pub struct ToolInstall {
|
||||
pub path: String,
|
||||
/// First line of `--version`, e.g. `pg_dump (PostgreSQL) 17.2`.
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
/// A line the tool wrote.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase", rename = "DbToolLine")]
|
||||
pub struct ToolLine {
|
||||
pub stderr: bool,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// Directories holding the tools that are often missing from `PATH`, by platform.
|
||||
fn known_dirs() -> Vec<PathBuf> {
|
||||
let mut dirs = Vec::new();
|
||||
if cfg!(target_os = "macos") {
|
||||
for prefix in ["/opt/homebrew", "/usr/local"] {
|
||||
dirs.push(PathBuf::from(format!("{prefix}/bin")));
|
||||
let opt = PathBuf::from(format!("{prefix}/opt"));
|
||||
for keg in ["libpq", "postgresql"] {
|
||||
dirs.extend(children(&opt, keg).into_iter().map(|d| d.join("bin")));
|
||||
}
|
||||
}
|
||||
dirs.extend(
|
||||
children(Path::new("/Applications/Postgres.app/Contents/Versions"), "").into_iter().map(|d| d.join("bin")),
|
||||
);
|
||||
} else if cfg!(windows) {
|
||||
for var in ["ProgramFiles", "ProgramFiles(x86)"] {
|
||||
if let Some(base) = std::env::var_os(var) {
|
||||
let root = PathBuf::from(base).join("PostgreSQL");
|
||||
dirs.extend(children(&root, "").into_iter().map(|d| d.join("bin")));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dirs.extend(children(Path::new("/usr/lib/postgresql"), "").into_iter().map(|d| d.join("bin")));
|
||||
dirs.extend(children(Path::new("/usr"), "pgsql-").into_iter().map(|d| d.join("bin")));
|
||||
}
|
||||
dirs
|
||||
}
|
||||
|
||||
/// Subdirectories of `parent` whose name starts with `prefix`, newest version first.
|
||||
fn children(parent: &Path, prefix: &str) -> Vec<PathBuf> {
|
||||
let Ok(entries) = std::fs::read_dir(parent) else { return Vec::new() };
|
||||
let mut dirs: Vec<PathBuf> = entries
|
||||
.flatten()
|
||||
.filter(|e| e.file_name().to_str().is_some_and(|n| n.starts_with(prefix)))
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_dir())
|
||||
.collect();
|
||||
dirs.sort_by_key(|p| std::cmp::Reverse(version_key(&p.to_string_lossy())));
|
||||
dirs
|
||||
}
|
||||
|
||||
/// The numbers in `text`, for comparing versions (`17.2` after `9.6`).
|
||||
fn version_key(text: &str) -> Vec<u32> {
|
||||
text.split(|c: char| !c.is_ascii_digit()).filter_map(|n| n.parse().ok()).collect()
|
||||
}
|
||||
|
||||
/// The installed copies of `tool` on `PATH` and in the usual install directories, newest first.
|
||||
/// Blocking: runs each one's `--version`.
|
||||
pub fn find_installs(tool: DbTool) -> Vec<ToolInstall> {
|
||||
let path = ide_core::process::host_var_os("PATH").unwrap_or_default();
|
||||
let mut dirs: Vec<PathBuf> = std::env::split_paths(&path).collect();
|
||||
dirs.extend(known_dirs());
|
||||
let mut seen = Vec::new();
|
||||
let mut out = Vec::new();
|
||||
for dir in dirs {
|
||||
for program in tool.programs() {
|
||||
let Some(found) = ide_core::process::find_executable(program, dir.as_os_str()) else { continue };
|
||||
let real = dunce::canonicalize(&found).unwrap_or_else(|_| found.clone());
|
||||
if seen.contains(&real) {
|
||||
continue;
|
||||
}
|
||||
seen.push(real);
|
||||
if let Ok(version) = version_of(&found) {
|
||||
out.push(ToolInstall { path: found.to_string_lossy().into_owned(), version });
|
||||
}
|
||||
}
|
||||
}
|
||||
out.sort_by_key(|i| std::cmp::Reverse(version_key(&i.version)));
|
||||
out
|
||||
}
|
||||
|
||||
/// The first line `path --version` prints. Blocking.
|
||||
pub fn version_of(path: &Path) -> IdeResult<String> {
|
||||
let (ok, text) = capture(path, "--version")?;
|
||||
match text.lines().next().map(str::trim).filter(|l| !l.is_empty()) {
|
||||
Some(line) if ok => Ok(line.to_owned()),
|
||||
_ => Err(IdeError::invalid(format!("{} is not a working executable", path.display()))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `path arg` for at most five seconds; whether it succeeded, and its standard output.
|
||||
fn capture(path: &Path, arg: &str) -> IdeResult<(bool, String)> {
|
||||
if !path.is_file() {
|
||||
return Err(IdeError::not_found(format!("{} does not exist", path.display())));
|
||||
}
|
||||
let mut cmd = std::process::Command::new(path);
|
||||
cmd.arg(arg).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null());
|
||||
ide_core::process::prepare(&mut cmd);
|
||||
let mut child = cmd.spawn().map_err(|e| IdeError::invalid(format!("cannot run {}: {e}", path.display())))?;
|
||||
let mut stdout = child.stdout.take().expect("piped");
|
||||
let reader = std::thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
let _ = std::io::Read::read_to_end(&mut stdout, &mut buf);
|
||||
buf
|
||||
});
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(5);
|
||||
let status = loop {
|
||||
if let Some(status) = child.try_wait()? {
|
||||
break status;
|
||||
}
|
||||
if std::time::Instant::now() > deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return Err(IdeError::invalid(format!("{} {arg} did not finish", path.display())));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
};
|
||||
let out = reader.join().unwrap_or_default();
|
||||
Ok((status.success(), String::from_utf8_lossy(&out).into_owned()))
|
||||
}
|
||||
|
||||
/// What made a dump, read from its first bytes (a directory: its `toc.dat`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DbDumpKind {
|
||||
/// `pg_dump` custom or tar archive, or a directory archive: `pg_restore`.
|
||||
PostgresArchive,
|
||||
/// Plain SQL from `pg_dump`: `psql`.
|
||||
PostgresSql,
|
||||
/// `mysqldump` / `mariadb-dump`: not for the PostgreSQL tools.
|
||||
MysqlSql,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// Blocking: reads at most the first 4 KiB.
|
||||
pub fn dump_kind(path: &Path) -> IdeResult<DbDumpKind> {
|
||||
use std::io::Read;
|
||||
if path.is_dir() {
|
||||
return Ok(if path.join("toc.dat").is_file() { DbDumpKind::PostgresArchive } else { DbDumpKind::Unknown });
|
||||
}
|
||||
let mut head = Vec::with_capacity(4096);
|
||||
std::fs::File::open(path)
|
||||
.map_err(|e| IdeError::not_found(format!("cannot read {}: {e}", path.display())))?
|
||||
.take(4096)
|
||||
.read_to_end(&mut head)?;
|
||||
Ok(kind_of(&head))
|
||||
}
|
||||
|
||||
fn kind_of(head: &[u8]) -> DbDumpKind {
|
||||
// A tar archive keeps `toc.dat` first; its name starts the 512-byte header.
|
||||
if head.starts_with(b"PGDMP") || head.starts_with(b"toc.dat\0") {
|
||||
return DbDumpKind::PostgresArchive;
|
||||
}
|
||||
let text = String::from_utf8_lossy(head);
|
||||
let comments: Vec<&str> = text.lines().take(20).filter(|l| l.starts_with("--")).collect();
|
||||
if comments.iter().any(|l| l.contains("PostgreSQL database dump")) {
|
||||
DbDumpKind::PostgresSql
|
||||
} else if comments.iter().any(|l| l.starts_with("-- MySQL dump") || l.starts_with("-- MariaDB dump")) {
|
||||
DbDumpKind::MysqlSql
|
||||
} else {
|
||||
DbDumpKind::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
/// Arguments and environment that connect `tool` to `source` (already resolved, through its
|
||||
/// tunnel when it has one). The password is left to [`prepare`].
|
||||
pub fn connection(tool: DbTool, source: &DataSource) -> (Vec<String>, Vec<(String, String)>) {
|
||||
let mut args = Vec::new();
|
||||
let host = source.host.trim();
|
||||
if !host.is_empty() {
|
||||
args.push(format!("--host={host}"));
|
||||
}
|
||||
args.push(format!("--port={}", source.port.or(source.driver.default_port()).unwrap_or(5432)));
|
||||
if !source.user.trim().is_empty() {
|
||||
args.push(format!("--username={}", source.user.trim()));
|
||||
}
|
||||
if !source.database.is_empty() {
|
||||
args.push(format!("--dbname={}", source.database));
|
||||
}
|
||||
// Without a terminal a password prompt would wait forever: fail instead.
|
||||
args.push("--no-password".into());
|
||||
|
||||
let mut env = vec![
|
||||
("PGAPPNAME".to_owned(), format!("OpenIDEA {}", tool.program())),
|
||||
(
|
||||
"PGSSLMODE".to_owned(),
|
||||
match source.ssl {
|
||||
DbSslMode::Disable => "disable",
|
||||
DbSslMode::Prefer => "prefer",
|
||||
DbSslMode::Require => "require",
|
||||
DbSslMode::VerifyCa => "verify-ca",
|
||||
DbSslMode::VerifyFull => "verify-full",
|
||||
}
|
||||
.to_owned(),
|
||||
),
|
||||
];
|
||||
for (var, file) in [
|
||||
("PGSSLROOTCERT", &source.ssl_ca_file),
|
||||
("PGSSLCERT", &source.ssl_cert_file),
|
||||
("PGSSLKEY", &source.ssl_key_file),
|
||||
] {
|
||||
if !file.trim().is_empty() {
|
||||
env.push((var.to_owned(), ide_ssh::expand(file.trim()).to_string_lossy().into_owned()));
|
||||
}
|
||||
}
|
||||
if source.read_only {
|
||||
env.push(("PGOPTIONS".to_owned(), "-c default_transaction_read_only=on".to_owned()));
|
||||
}
|
||||
(args, env)
|
||||
}
|
||||
|
||||
/// How a tool is started: connection arguments, then the job's, and the environment.
|
||||
pub struct Prepared {
|
||||
pub args: Vec<String>,
|
||||
pub env: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// The command for `job` against `source`. The password goes in `PGPASSWORD`, never in the
|
||||
/// arguments, which other users of the machine can read in the process list.
|
||||
pub fn prepare(job: &DbToolJob, source: &DataSource, password: Option<&str>) -> Prepared {
|
||||
let (mut args, mut env) = connection(job.tool(), source);
|
||||
args.extend(job.args());
|
||||
if let Some(p) = password.filter(|p| !p.is_empty()) {
|
||||
env.push(("PGPASSWORD".into(), p.to_owned()));
|
||||
}
|
||||
Prepared { args, env }
|
||||
}
|
||||
|
||||
/// Runs `program` until it exits, passing its output in batches. Returns the exit code
|
||||
/// (`None`: ended by a signal). Cancelling kills it and everything it started.
|
||||
pub async fn run(
|
||||
program: &Path,
|
||||
args: &[String],
|
||||
env: &[(String, String)],
|
||||
cancel: &CancellationToken,
|
||||
mut on_output: impl FnMut(Vec<ToolLine>),
|
||||
) -> IdeResult<Option<i32>> {
|
||||
let mut cmd = tokio::process::Command::new(program);
|
||||
cmd.args(args)
|
||||
.envs(env.iter().map(|(k, v)| (k, v)))
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.kill_on_drop(true);
|
||||
ide_core::process::prepare(cmd.as_std_mut());
|
||||
ide_core::process::own_group(cmd.as_std_mut());
|
||||
let mut child = cmd.spawn().map_err(|e| IdeError::invalid(format!("cannot start {}: {e}", program.display())))?;
|
||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
if let Some(out) = child.stdout.take() {
|
||||
forward(out, false, tx.clone());
|
||||
}
|
||||
if let Some(err) = child.stderr.take() {
|
||||
forward(err, true, tx);
|
||||
}
|
||||
|
||||
let mut batch = Vec::new();
|
||||
let mut tick = tokio::time::interval(Duration::from_millis(100));
|
||||
loop {
|
||||
tokio::select! {
|
||||
line = rx.recv() => match line {
|
||||
Some(line) => {
|
||||
batch.push(line);
|
||||
if batch.len() >= 500 {
|
||||
on_output(std::mem::take(&mut batch));
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
},
|
||||
_ = tick.tick() => {
|
||||
if !batch.is_empty() {
|
||||
on_output(std::mem::take(&mut batch));
|
||||
}
|
||||
}
|
||||
_ = cancel.cancelled() => {
|
||||
if let Some(pid) = child.id() {
|
||||
ide_core::process::kill_tree_of(pid);
|
||||
}
|
||||
let _ = child.kill().await;
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
}
|
||||
}
|
||||
if !batch.is_empty() {
|
||||
on_output(batch);
|
||||
}
|
||||
let status = tokio::select! {
|
||||
status = child.wait() => status?,
|
||||
_ = cancel.cancelled() => {
|
||||
if let Some(pid) = child.id() {
|
||||
ide_core::process::kill_tree_of(pid);
|
||||
}
|
||||
let _ = child.kill().await;
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
};
|
||||
Ok(status.code())
|
||||
}
|
||||
|
||||
fn forward(
|
||||
pipe: impl AsyncRead + Unpin + Send + 'static,
|
||||
stderr: bool,
|
||||
tx: tokio::sync::mpsc::UnboundedSender<ToolLine>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
let mut reader = BufReader::new(pipe);
|
||||
let mut buf = Vec::new();
|
||||
loop {
|
||||
buf.clear();
|
||||
match reader.read_until(b'\n', &mut buf).await {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(_) => {
|
||||
let text = String::from_utf8_lossy(&buf).trim_end_matches(['\r', '\n']).to_owned();
|
||||
if tx.send(ToolLine { stderr, text }).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn pg() -> DataSource {
|
||||
DataSource {
|
||||
driver: DbDriver::Postgres,
|
||||
host: "db.example.test".into(),
|
||||
port: Some(6543),
|
||||
user: "app".into(),
|
||||
database: "shop".into(),
|
||||
..DataSource::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connects_like_the_data_source_without_prompting() {
|
||||
let (args, env) = connection(DbTool::PgDump, &pg());
|
||||
assert_eq!(args, ["--host=db.example.test", "--port=6543", "--username=app", "--dbname=shop", "--no-password"]);
|
||||
assert!(env.contains(&("PGSSLMODE".into(), "prefer".into())));
|
||||
assert!(!env.iter().any(|(k, _)| k == "PGPASSWORD" || k == "PGOPTIONS"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_port_and_optional_fields() {
|
||||
let source = DataSource { port: None, user: String::new(), database: String::new(), ..pg() };
|
||||
let (args, _) = connection(DbTool::Psql, &source);
|
||||
assert_eq!(args, ["--host=db.example.test", "--port=5432", "--no-password"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_only_sources_refuse_writes() {
|
||||
let source = DataSource { read_only: true, ssl: DbSslMode::VerifyFull, ..pg() };
|
||||
let (_, env) = connection(DbTool::PgRestore, &source);
|
||||
assert!(env.contains(&("PGOPTIONS".into(), "-c default_transaction_read_only=on".into())));
|
||||
assert!(env.contains(&("PGSSLMODE".into(), "verify-full".into())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_tool_itself_is_accepted() {
|
||||
for ok in ["/usr/bin/pg_dump", "/pg/bin/pg_dump.exe", "/usr/bin/pg_dump-17", "/opt/pg_dump17"] {
|
||||
assert!(DbTool::PgDump.is_program(Path::new(ok)), "{ok}");
|
||||
}
|
||||
for bad in ["/usr/bin/pg_dumpall", "/bin/sh", "/usr/bin/pg_restore", "/tmp/evil"] {
|
||||
assert!(!DbTool::PgDump.is_program(Path::new(bad)), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tells_dumps_apart_by_their_header() {
|
||||
assert_eq!(kind_of(b"PGDMP\x01\x0f"), DbDumpKind::PostgresArchive);
|
||||
assert_eq!(kind_of(b"toc.dat\0\0\0"), DbDumpKind::PostgresArchive);
|
||||
assert_eq!(kind_of(b"--\n-- PostgreSQL database dump\n--\n"), DbDumpKind::PostgresSql);
|
||||
assert_eq!(kind_of(b"-- MySQL dump 10.13 Distrib 8.4.6\n--\n"), DbDumpKind::MysqlSql);
|
||||
assert_eq!(kind_of(b"-- MariaDB dump 10.19 Distrib 11.4.2\n"), DbDumpKind::MysqlSql);
|
||||
assert_eq!(kind_of(b"CREATE TABLE t (id int);\n"), DbDumpKind::Unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newer_versions_sort_first() {
|
||||
assert!(version_key("pg_dump (PostgreSQL) 17.2") > version_key("pg_dump (PostgreSQL) 9.6.24"));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn streams_both_pipes_and_reports_the_exit_code() {
|
||||
let mut lines = Vec::new();
|
||||
let code = run(
|
||||
Path::new("/bin/sh"),
|
||||
&["-c".into(), "echo out; echo err >&2; exit 3".into()],
|
||||
&[],
|
||||
&CancellationToken::new(),
|
||||
|batch| lines.extend(batch),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(code, Some(3));
|
||||
assert!(lines.contains(&ToolLine { stderr: false, text: "out".into() }));
|
||||
assert!(lines.contains(&ToolLine { stderr: true, text: "err".into() }));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn cancelling_stops_the_process() {
|
||||
let cancel = CancellationToken::new();
|
||||
let stop = cancel.clone();
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
stop.cancel();
|
||||
});
|
||||
let result = run(Path::new("/bin/sh"), &["-c".into(), "sleep 30".into()], &[], &cancel, |_| {}).await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
/// A stand-in for `tool` that prints each argument on a line, then whether `PGPASSWORD`
|
||||
/// holds the expected password.
|
||||
#[cfg(unix)]
|
||||
fn echo_tool(dir: &Path, tool: DbTool) -> PathBuf {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let path = dir.join(tool.program());
|
||||
std::fs::write(
|
||||
&path,
|
||||
"#!/bin/sh\nfor a in \"$@\"; do echo \"arg:$a\"; done\necho \"env:${PGPASSWORD:+set}\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
path
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn the_password_never_reaches_the_arguments() {
|
||||
const SECRET: &str = "s3cret-pass-do-not-leak";
|
||||
let dir = std::env::temp_dir().join(format!("openidea-tools-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("out.sql").to_string_lossy().into_owned();
|
||||
let jobs = [
|
||||
DbToolJob::PgDump {
|
||||
file: file.clone(),
|
||||
options: DbPgDumpOptions {
|
||||
format: DbDumpFormat::Plain,
|
||||
statements: DbDumpStatements::Copy,
|
||||
content: DbDumpContent::All,
|
||||
schemas: vec!["public".into()],
|
||||
tables: vec![],
|
||||
exclude_tables: vec![],
|
||||
clean: true,
|
||||
if_exists: true,
|
||||
create: false,
|
||||
no_owner: false,
|
||||
no_privileges: false,
|
||||
jobs: 1,
|
||||
},
|
||||
},
|
||||
DbToolJob::Psql {
|
||||
file: file.clone(),
|
||||
options: DbPsqlOptions { single_transaction: true, stop_on_error: true },
|
||||
},
|
||||
];
|
||||
for job in jobs {
|
||||
let program = echo_tool(&dir, job.tool());
|
||||
let prepared = prepare(&job, &pg(), Some(SECRET));
|
||||
assert!(!prepared.args.iter().any(|a| a.contains(SECRET)), "{:?}", prepared.args);
|
||||
let mut lines = Vec::new();
|
||||
run(&program, &prepared.args, &prepared.env, &CancellationToken::new(), |b| lines.extend(b)).await.unwrap();
|
||||
let text: Vec<&str> = lines.iter().map(|l| l.text.as_str()).collect();
|
||||
assert!(text.contains(&"arg:--dbname=shop"), "{text:?}");
|
||||
assert!(!text.iter().any(|l| l.contains(SECRET)), "{text:?}");
|
||||
assert!(text.contains(&"env:set"), "the tool gets the password from PGPASSWORD: {text:?}");
|
||||
}
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -228,3 +228,78 @@ async fn opening_a_table_reads_multi_column_keys_in_order() -> IdeResult<()> {
|
||||
db.execute("p", "DROP SCHEMA oi_keys CASCADE", 10, &c).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Runs `job` with the newest installed copy of its tool, the way the dialogs do.
|
||||
async fn run_tool(db: &DbManager, target: &str, job: ide_db::tools::DbToolJob) -> IdeResult<()> {
|
||||
let install = ide_db::tools::find_installs(job.tool()).into_iter().next().expect("installed");
|
||||
let mut lines = Vec::new();
|
||||
let code = db.run_tool(target, install.path.as_ref(), &job, &CancellationToken::new(), |b| lines.extend(b)).await?;
|
||||
assert_eq!(code, Some(0), "{} failed: {lines:?}", job.tool().program());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dumps_and_restores_with_the_client_tools() -> IdeResult<()> {
|
||||
use ide_db::tools::{
|
||||
DbDumpContent, DbDumpFormat, DbDumpStatements, DbPgDumpOptions, DbPgRestoreOptions, DbPsqlOptions, DbTool,
|
||||
DbToolJob, find_installs,
|
||||
};
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let Some(db) = manager(&tmp).await else { return Ok(()) };
|
||||
let server = db.server_version("p").await? / 10000;
|
||||
let major = |t: DbTool| {
|
||||
find_installs(t).first().and_then(|i| i.version.split(' ').nth(2)?.split('.').next()?.parse::<u32>().ok())
|
||||
};
|
||||
// Other major versions write or refuse what this server needs; the dialogs warn about that instead.
|
||||
if [DbTool::PgDump, DbTool::PgRestore, DbTool::Psql].into_iter().any(|t| major(t) != Some(server)) {
|
||||
return Ok(());
|
||||
}
|
||||
create_schemas(&db).await;
|
||||
let c = CancellationToken::new();
|
||||
db.execute("p", "DROP DATABASE IF EXISTS oi_restore", 1, &c).await?;
|
||||
db.execute("p", "CREATE DATABASE oi_restore", 1, &c).await?;
|
||||
let file = |name: &str| tmp.path().join(name).to_string_lossy().into_owned();
|
||||
|
||||
let dump = |format, file: String, schema: &str| DbToolJob::PgDump {
|
||||
file,
|
||||
options: DbPgDumpOptions {
|
||||
format,
|
||||
statements: DbDumpStatements::Copy,
|
||||
content: DbDumpContent::All,
|
||||
schemas: vec![schema.into()],
|
||||
tables: vec![],
|
||||
exclude_tables: vec![],
|
||||
clean: false,
|
||||
if_exists: false,
|
||||
create: false,
|
||||
no_owner: false,
|
||||
no_privileges: false,
|
||||
jobs: 1,
|
||||
},
|
||||
};
|
||||
let archive = file("a.dump");
|
||||
run_tool(&db, "p", dump(DbDumpFormat::Custom, archive.clone(), "oi_a")).await?;
|
||||
let restore = DbPgRestoreOptions {
|
||||
content: DbDumpContent::All,
|
||||
schemas: vec![],
|
||||
clean: false,
|
||||
if_exists: false,
|
||||
no_owner: false,
|
||||
no_privileges: false,
|
||||
single_transaction: false,
|
||||
exit_on_error: true,
|
||||
jobs: 1,
|
||||
};
|
||||
run_tool(&db, "p:oi_restore", DbToolJob::PgRestore { file: archive, options: restore }).await?;
|
||||
|
||||
let script = file("b.sql");
|
||||
run_tool(&db, "p", dump(DbDumpFormat::Plain, script.clone(), "oi_b")).await?;
|
||||
let psql = DbPsqlOptions { single_transaction: true, stop_on_error: true };
|
||||
run_tool(&db, "p:oi_restore", DbToolJob::Psql { file: script, options: psql }).await?;
|
||||
|
||||
let r = db.execute("p:oi_restore", "SELECT a.tag || ', ' || b.tag FROM oi_a.items a, oi_b.items b", 1, &c).await?;
|
||||
assert_eq!(r.rows[0][0].as_deref(), Some("from a, from b"));
|
||||
db.disconnect("p:oi_restore").await;
|
||||
db.execute("p", "DROP DATABASE oi_restore", 1, &c).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -532,3 +532,35 @@ fn console_dir(host: &Host, id: &str) -> IdeResult<PathBuf> {
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
/// Runs a client tool against target `id` until it exits or the request is cancelled.
|
||||
/// Desktop only: the file paths come from local dialogs.
|
||||
pub async fn run_tool(
|
||||
host: &Host,
|
||||
window: &str,
|
||||
request: RequestId,
|
||||
id: &str,
|
||||
executable: &std::path::Path,
|
||||
job: &ide_db::tools::DbToolJob,
|
||||
on_output: impl FnMut(Vec<ide_db::tools::ToolLine>),
|
||||
) -> IdeResult<Option<i32>> {
|
||||
let db = host.project(window)?.db.clone();
|
||||
let cancel = host.begin_request(request);
|
||||
let result = db.run_tool(id, executable, job, &cancel, on_output).await;
|
||||
host.end_request(request);
|
||||
result
|
||||
}
|
||||
|
||||
pub fn tool_preview(
|
||||
host: &Host,
|
||||
window: &str,
|
||||
id: &str,
|
||||
executable: &std::path::Path,
|
||||
job: &ide_db::tools::DbToolJob,
|
||||
) -> IdeResult<String> {
|
||||
host.project(window)?.db.tool_preview(id, executable, job)
|
||||
}
|
||||
|
||||
pub async fn server_version(host: &Host, window: &str, id: &str) -> IdeResult<u32> {
|
||||
host.project(window)?.db.server_version(id).await
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
use crate::state::AppState;
|
||||
use ide_core::{IdeResult, IpcPath, RequestId};
|
||||
use ide_db::split::SqlStatement;
|
||||
use ide_db::tools::{DbDumpKind, DbTool, DbToolJob, ToolInstall, ToolLine};
|
||||
use ide_db::{DataSource, DbObjectKind, DbRowEdit, SortKey};
|
||||
#[allow(unused_imports)]
|
||||
pub use ide_host::ops::db::*;
|
||||
use tauri::ipc::Channel;
|
||||
use tauri::{State, Window};
|
||||
|
||||
#[tauri::command]
|
||||
@@ -369,3 +371,63 @@ pub fn db_console_path(state: State<'_, AppState>, id: String) -> IdeResult<IpcP
|
||||
pub fn db_consoles(state: State<'_, AppState>, id: String, create: bool) -> IdeResult<Vec<IpcPath>> {
|
||||
ide_host::ops::db::consoles(&state.host, &id, create)
|
||||
}
|
||||
|
||||
/// Installed copies of `tool`, newest first.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn db_tool_installs(tool: DbTool) -> IdeResult<Vec<ToolInstall>> {
|
||||
super::doc::blocking(move || Ok(ide_db::tools::find_installs(tool))).await
|
||||
}
|
||||
|
||||
/// The version line of the executable at `path`.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn db_tool_version(path: std::path::PathBuf) -> IdeResult<String> {
|
||||
super::doc::blocking(move || ide_db::tools::version_of(&path)).await
|
||||
}
|
||||
|
||||
/// What made the dump at `path` (from its first bytes), so a restore picks the right tool.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn db_dump_kind(path: std::path::PathBuf) -> IdeResult<DbDumpKind> {
|
||||
super::doc::blocking(move || ide_db::tools::dump_kind(&path)).await
|
||||
}
|
||||
|
||||
/// The command line `db_run_tool` would run, without the password.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn db_tool_preview(
|
||||
window: Window,
|
||||
state: State<'_, AppState>,
|
||||
id: String,
|
||||
executable: std::path::PathBuf,
|
||||
job: DbToolJob,
|
||||
) -> IdeResult<String> {
|
||||
ide_host::ops::db::tool_preview(&state.host, window.label(), &id, &executable, &job)
|
||||
}
|
||||
|
||||
/// Runs `job` against target `id` and streams the tool's output; returns its exit code. Stays
|
||||
/// in the desktop shell like [`db_export_file`]: the paths in `job` are local.
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn db_run_tool(
|
||||
window: Window,
|
||||
state: State<'_, AppState>,
|
||||
request: RequestId,
|
||||
id: String,
|
||||
executable: std::path::PathBuf,
|
||||
job: DbToolJob,
|
||||
on_output: Channel<Vec<ToolLine>>,
|
||||
) -> IdeResult<Option<i32>> {
|
||||
ide_host::ops::db::run_tool(&state.host, window.label(), request, &id, &executable, &job, |lines| {
|
||||
let _ = on_output.send(lines);
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// `server_version_num` of the PostgreSQL server of target `id` (connects first).
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn db_server_version(window: Window, state: State<'_, AppState>, id: String) -> IdeResult<u32> {
|
||||
ide_host::ops::db::server_version(&state.host, window.label(), &id).await
|
||||
}
|
||||
|
||||
@@ -787,5 +787,11 @@ pub fn builder() -> tauri_specta::Builder<tauri::Wry> {
|
||||
db::db_apply_edits,
|
||||
db::db_console_path,
|
||||
db::db_consoles,
|
||||
db::db_tool_installs,
|
||||
db::db_tool_version,
|
||||
db::db_tool_preview,
|
||||
db::db_dump_kind,
|
||||
db::db_run_tool,
|
||||
db::db_server_version,
|
||||
])
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ db_database_catalog trusted project_host bin
|
||||
db_databases trusted project_host bindings async
|
||||
db_ddl trusted project_host bindings async
|
||||
db_disconnect trusted project_host bindings async
|
||||
db_dump_kind trusted desktop bindings async
|
||||
db_execute trusted project_host bindings async
|
||||
db_explain trusted project_host bindings async
|
||||
db_export_file always desktop bindings async
|
||||
@@ -104,10 +105,12 @@ db_history trusted project_host bin
|
||||
db_object_ddl trusted project_host bindings async
|
||||
db_remove_history trusted project_host bindings async
|
||||
db_retain_catalog trusted project_host bindings async
|
||||
db_run_tool trusted desktop bindings async
|
||||
db_save_sources trusted project_host bindings async
|
||||
db_saved_catalog trusted project_host bindings async
|
||||
db_schema_objects trusted project_host bindings async
|
||||
db_schemas trusted project_host bindings async
|
||||
db_server_version trusted desktop bindings async
|
||||
db_session_context trusted project_host bindings async
|
||||
db_set_manual_commit trusted project_host bindings async
|
||||
db_set_password trusted project_host bindings async
|
||||
@@ -119,6 +122,9 @@ db_table_page trusted project_host bin
|
||||
db_tables trusted project_host bindings async
|
||||
db_test trusted project_host bindings async
|
||||
db_test_tunnel trusted project_host bindings async
|
||||
db_tool_installs trusted desktop bindings async
|
||||
db_tool_preview trusted desktop bindings sync
|
||||
db_tool_version trusted desktop bindings async
|
||||
db_trust_ssh_host trusted project_host bindings async
|
||||
debug_attachable_processes always project_host bindings async
|
||||
debug_breakpoint_states trusted project_host bindings sync
|
||||
|
||||
@@ -1903,6 +1903,21 @@ export const commands = {
|
||||
dbConsolePath: (id: string) => typedError<IpcPath, IdeError>(__TAURI_INVOKE("db_console_path", { id })),
|
||||
/** The query consoles of data source `id`; `create` adds the next one first. */
|
||||
dbConsoles: (id: string, create: boolean) => typedError<IpcPath[], IdeError>(__TAURI_INVOKE("db_consoles", { id, create })),
|
||||
/** Installed copies of `tool`, newest first. */
|
||||
dbToolInstalls: (tool: DbTool) => typedError<DbToolInstall[], IdeError>(__TAURI_INVOKE("db_tool_installs", { tool })),
|
||||
/** The version line of the executable at `path`. */
|
||||
dbToolVersion: (path: string) => typedError<string, IdeError>(__TAURI_INVOKE("db_tool_version", { path })),
|
||||
/** The command line `db_run_tool` would run, without the password. */
|
||||
dbToolPreview: (id: string, executable: string, job: DbToolJob) => typedError<string, IdeError>(__TAURI_INVOKE("db_tool_preview", { id, executable, job })),
|
||||
/** What made the dump at `path` (from its first bytes), so a restore picks the right tool. */
|
||||
dbDumpKind: (path: string) => typedError<DbDumpKind, IdeError>(__TAURI_INVOKE("db_dump_kind", { path })),
|
||||
/**
|
||||
* Runs `job` against target `id` and streams the tool's output; returns its exit code. Stays
|
||||
* in the desktop shell like [`db_export_file`]: the paths in `job` are local.
|
||||
*/
|
||||
dbRunTool: (request: RequestId, id: string, executable: string, job: DbToolJob, onOutput: Channel<DbToolLine[]>) => typedError<number | null, IdeError>(__TAURI_INVOKE("db_run_tool", { request, id, executable, job, onOutput })),
|
||||
/** `server_version_num` of the PostgreSQL server of target `id` (connects first). */
|
||||
dbServerVersion: (id: string) => typedError<number, IdeError>(__TAURI_INVOKE("db_server_version", { id })),
|
||||
};
|
||||
|
||||
/* Types */
|
||||
@@ -4120,6 +4135,25 @@ export type DbDriver = "sqlite" | "postgres" |
|
||||
/** Oracle Database (pure-Rust thin driver, no client library). */
|
||||
"oracle" | "snowflake";
|
||||
|
||||
export type DbDumpContent = "all" | "schemaOnly" | "dataOnly";
|
||||
|
||||
export type DbDumpFormat =
|
||||
/** SQL text, restored with `psql`. */
|
||||
"plain" | "custom" |
|
||||
/** A directory of files; the only format `pg_dump` writes in parallel. */
|
||||
"directory" | "tar";
|
||||
|
||||
/** What made a dump, read from its first bytes (a directory: its `toc.dat`). */
|
||||
export type DbDumpKind =
|
||||
/** `pg_dump` custom or tar archive, or a directory archive: `pg_restore`. */
|
||||
"postgresArchive" |
|
||||
/** Plain SQL from `pg_dump`: `psql`. */
|
||||
"postgresSql" |
|
||||
/** `mysqldump` / `mariadb-dump`: not for the PostgreSQL tools. */
|
||||
"mysqlSql" | "unknown";
|
||||
|
||||
export type DbDumpStatements = "copy" | "inserts" | "columnInserts";
|
||||
|
||||
/** `schema.table (columns) REFERENCES ref_schema.ref_table (ref_columns)`. */
|
||||
export type DbForeignKey = {
|
||||
schema: string,
|
||||
@@ -4184,6 +4218,37 @@ export type DbPackageInfo = {
|
||||
name: string,
|
||||
};
|
||||
|
||||
/** Name lists hold `pg_dump` patterns (`*`, `?`, quoted names). */
|
||||
export type DbPgDumpOptions = {
|
||||
format: DbDumpFormat,
|
||||
statements: DbDumpStatements,
|
||||
content: DbDumpContent,
|
||||
schemas: string[],
|
||||
tables: string[],
|
||||
excludeTables: string[],
|
||||
/** Plain format only: archives get DROP / CREATE at restore. */
|
||||
clean: boolean,
|
||||
ifExists: boolean,
|
||||
create: boolean,
|
||||
noOwner: boolean,
|
||||
noPrivileges: boolean,
|
||||
/** Directory format only. */
|
||||
jobs: number,
|
||||
};
|
||||
|
||||
export type DbPgRestoreOptions = {
|
||||
content: DbDumpContent,
|
||||
schemas: string[],
|
||||
clean: boolean,
|
||||
ifExists: boolean,
|
||||
noOwner: boolean,
|
||||
noPrivileges: boolean,
|
||||
singleTransaction: boolean,
|
||||
exitOnError: boolean,
|
||||
/** Ignored with a single transaction, which `pg_restore` cannot split. */
|
||||
jobs: number,
|
||||
};
|
||||
|
||||
/** One operation of a plan; `parent` indexes the plan's node list (roots: `None`). */
|
||||
export type DbPlanNode = {
|
||||
parent: number | null,
|
||||
@@ -4209,6 +4274,11 @@ export type DbProperty = {
|
||||
value?: string,
|
||||
};
|
||||
|
||||
export type DbPsqlOptions = {
|
||||
singleTransaction: boolean,
|
||||
stopOnError: boolean,
|
||||
};
|
||||
|
||||
export type DbQueryResult = {
|
||||
statement: string,
|
||||
columns: DbResultColumn[],
|
||||
@@ -4365,6 +4435,26 @@ export type DbTablePage = {
|
||||
offset: number | null,
|
||||
};
|
||||
|
||||
export type DbTool = "pgDump" | "pgRestore" | "psql";
|
||||
|
||||
/** An installed copy of a tool. */
|
||||
export type DbToolInstall = {
|
||||
path: string,
|
||||
/** First line of `--version`, e.g. `pg_dump (PostgreSQL) 17.2`. */
|
||||
version: string,
|
||||
};
|
||||
|
||||
/** A run of a client tool: which one, the file it writes or reads, and its options. */
|
||||
export type DbToolJob = { tool: "pgDump"; file: string; options: DbPgDumpOptions } |
|
||||
/** `file`: an archive, or a directory archive. */
|
||||
{ tool: "pgRestore"; file: string; options: DbPgRestoreOptions } | { tool: "psql"; file: string; options: DbPsqlOptions };
|
||||
|
||||
/** A line the tool wrote. */
|
||||
export type DbToolLine = {
|
||||
stderr: boolean,
|
||||
text: string,
|
||||
};
|
||||
|
||||
export type DbTriggerInfo = {
|
||||
name: string,
|
||||
/** `before`, `after`, `instead of`. */
|
||||
|
||||
@@ -1416,3 +1416,62 @@
|
||||
height: 5px;
|
||||
cursor: row-resize;
|
||||
}
|
||||
|
||||
/* Export with pg_dump / Restore with pg_restore or psql. */
|
||||
.dumpChecks {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: var(--space-1-5) var(--space-4);
|
||||
}
|
||||
.dumpPreview {
|
||||
grid-column: 1 / -1;
|
||||
max-height: 96px;
|
||||
overflow: auto;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-control);
|
||||
background: var(--bg-editor);
|
||||
font-family: var(--openidea-editor-font, var(--font-mono));
|
||||
font-size: var(--fs-code);
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.dumpRun {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-2);
|
||||
padding: 14px;
|
||||
}
|
||||
.dumpLog {
|
||||
height: 320px;
|
||||
overflow: auto;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-control);
|
||||
background: var(--bg-editor);
|
||||
font-family: var(--openidea-editor-font, var(--font-mono));
|
||||
font-size: var(--fs-code);
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.dumpPath {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.dumpStatus {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-0-5);
|
||||
min-width: 0;
|
||||
}
|
||||
.dumpRoute {
|
||||
color: var(--fg-muted);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.dumpError {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ const ExportDialog = lazyComponent(() => import("./ExportDialog").then((m) => m.
|
||||
const HistoryDialog = lazyComponent(() => import("./DataDialogs").then((m) => m.HistoryDialog));
|
||||
const ImportDialog = lazyComponent(() => import("./DataDialogs").then((m) => m.ImportDialog));
|
||||
const SchemaCompareDialog = lazyComponent(() => import("./SchemaCompareDialog").then((m) => m.SchemaCompareDialog));
|
||||
const DumpDialog = lazyComponent(() => import("./DumpDialogs").then((m) => m.DumpDialog));
|
||||
const RestoreDialog = lazyComponent(() => import("./DumpDialogs").then((m) => m.RestoreDialog));
|
||||
|
||||
function PasswordDialog({ source, resolve }: { source: DataSource; resolve: (ok: boolean) => void }) {
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -192,6 +194,26 @@ export function DatabaseDialogs() {
|
||||
return (
|
||||
<ImportDialog key={`${dialog.id}.${dialog.table}`} id={dialog.id} schema={dialog.schema} table={dialog.table} />
|
||||
);
|
||||
if (dialog.kind === "dump")
|
||||
return (
|
||||
<DumpDialog
|
||||
key={dialog.target}
|
||||
target={dialog.target}
|
||||
database={dialog.database}
|
||||
schemas={dialog.schemas}
|
||||
tables={dialog.tables}
|
||||
serverVersion={dialog.serverVersion}
|
||||
/>
|
||||
);
|
||||
if (dialog.kind === "restore")
|
||||
return (
|
||||
<RestoreDialog
|
||||
key={dialog.target}
|
||||
target={dialog.target}
|
||||
database={dialog.database}
|
||||
serverVersion={dialog.serverVersion}
|
||||
/>
|
||||
);
|
||||
if (dialog.kind === "sshSecret")
|
||||
return (
|
||||
<SshSecretDialog key={dialog.source.id} source={dialog.source} prompt={dialog.prompt} resolve={dialog.resolve} />
|
||||
|
||||
@@ -0,0 +1,783 @@
|
||||
/** DataGrip's Export with 'pg_dump' and Restore with 'pg_restore' / 'psql' dialogs. */
|
||||
import { Fragment, useEffect, useLayoutEffect, useRef, useState } from "react";
|
||||
import clsx from "clsx";
|
||||
import {
|
||||
call,
|
||||
commands,
|
||||
type DbDumpContent,
|
||||
type DbDumpFormat,
|
||||
type DbDumpKind,
|
||||
type DbDumpStatements,
|
||||
type DbPsqlOptions,
|
||||
type DbTool,
|
||||
type DbToolInstall,
|
||||
type DbToolJob,
|
||||
} from "@/platform/ipc";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import {
|
||||
Checkbox,
|
||||
Dialog,
|
||||
IconButton,
|
||||
Input,
|
||||
Select,
|
||||
T,
|
||||
Tabs,
|
||||
copyText,
|
||||
selectableText,
|
||||
showContextMenu,
|
||||
type DialogButton,
|
||||
} from "@/ui";
|
||||
import {
|
||||
DEFAULT_DUMP,
|
||||
DEFAULT_PSQL,
|
||||
DEFAULT_RESTORE,
|
||||
DUMP_FORMATS,
|
||||
dumpMismatch,
|
||||
dumpOptions,
|
||||
formatElapsed,
|
||||
majorOf,
|
||||
outputPath,
|
||||
pickInstall,
|
||||
restoreOptions,
|
||||
restoreToolFor,
|
||||
versionFit,
|
||||
withExtension,
|
||||
type DumpForm,
|
||||
type DumpMismatch,
|
||||
type RestoreForm,
|
||||
} from "./dumpTools";
|
||||
import { splitTarget } from "./model";
|
||||
import { closeDialog, refresh, sourceById } from "./store";
|
||||
import { TOOL_PROGRAMS, startToolRun, stopToolRun, useToolRuns } from "./toolRuns";
|
||||
import { copySelectionOnKeyDown } from "@/ui/selectableText";
|
||||
import styles from "./Database.module.css";
|
||||
|
||||
const PROGRAM = TOOL_PROGRAMS;
|
||||
export const pathKey = (tool: DbTool) => `openidea.db.toolPath.${tool}`;
|
||||
|
||||
const CONTENT_OPTIONS: { value: DbDumpContent; label: string }[] = [
|
||||
{ value: "all", label: "Schema and data" },
|
||||
{ value: "schemaOnly", label: "Schema only" },
|
||||
{ value: "dataOnly", label: "Data only" },
|
||||
];
|
||||
|
||||
/** The connection target of `database` on the data source of `target`. */
|
||||
function targetOf(target: string, database: string): string {
|
||||
const { id } = splitTarget(target);
|
||||
return database.trim() ? `${id}:${database.trim()}` : id;
|
||||
}
|
||||
|
||||
/** `150013` → 15. */
|
||||
const majorOfServer = (num: number | null) => (num ? Math.floor(num / 10000) : null);
|
||||
|
||||
export function joinPath(dir: string, name: string): string {
|
||||
return `${dir.replace(/[\\/]+$/, "")}/${name}`;
|
||||
}
|
||||
|
||||
/** What the hint under the executable says about its version next to the server's. */
|
||||
function versionHint(tool: DbTool, version: string, serverMajor: number | null): { text: string; warn: boolean } {
|
||||
const toolMajor = majorOf(version);
|
||||
const fit = versionFit(tool, toolMajor, serverMajor);
|
||||
const server = String(serverMajor);
|
||||
if (fit === "tooOld")
|
||||
return {
|
||||
text: tx`${version}: older than the server (PostgreSQL ${server}), which pg_dump refuses. Choose version ${server} or newer.`,
|
||||
warn: true,
|
||||
};
|
||||
if (fit === "newer")
|
||||
return {
|
||||
text: tx`${version}: newer than the server (PostgreSQL ${server}). What it writes may not load into PostgreSQL ${server}. Install the PostgreSQL ${server} client tools to restore into this server.`,
|
||||
warn: true,
|
||||
};
|
||||
return { text: version, warn: false };
|
||||
}
|
||||
|
||||
/** The executable of `tool`: preselected by the server's version, shows its own and what is wrong. */
|
||||
export function ExecutableField({
|
||||
tool,
|
||||
value,
|
||||
onChange,
|
||||
serverMajor,
|
||||
}: {
|
||||
tool: DbTool;
|
||||
value: string;
|
||||
onChange: (v: string) => void;
|
||||
serverMajor: number | null;
|
||||
}) {
|
||||
const [installs, setInstalls] = useState<DbToolInstall[] | null>(null);
|
||||
const [check, setCheck] = useState<{ path: string; version?: string; error?: string } | null>(null);
|
||||
const program = PROGRAM[tool];
|
||||
|
||||
useEffect(() => {
|
||||
let live = true;
|
||||
void call(commands.dbToolInstalls(tool))
|
||||
.catch(() => [])
|
||||
.then((found) => {
|
||||
if (!live) return;
|
||||
setInstalls(found);
|
||||
if (!value) onChange(pickInstall(found, serverMajor, localStorage.getItem(pathKey(tool))));
|
||||
});
|
||||
return () => {
|
||||
live = false;
|
||||
};
|
||||
// Only when the tool changes: `value` seeds the field once.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [tool]);
|
||||
|
||||
useEffect(() => {
|
||||
const path = value.trim();
|
||||
if (!path) return;
|
||||
let live = true;
|
||||
const timer = setTimeout(() => {
|
||||
call(commands.dbToolVersion(path))
|
||||
.then((version) => live && setCheck({ path, version }))
|
||||
.catch((e) => live && setCheck({ path, error: e instanceof Error ? e.message : String(e) }));
|
||||
}, 250);
|
||||
return () => {
|
||||
live = false;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [value]);
|
||||
|
||||
const browse = async () => {
|
||||
try {
|
||||
const { open } = await import("@/platform/dialog");
|
||||
const file = await open({ multiple: false, directory: false, title: tx`Path to ${program}` });
|
||||
if (typeof file === "string") onChange(file);
|
||||
} catch {
|
||||
// No dialog (tests): keep typing the path.
|
||||
}
|
||||
};
|
||||
|
||||
const found = installs ?? [];
|
||||
const options = found.map((i) => ({ value: i.path, label: i.path, detail: i.version, verbatim: true }));
|
||||
if (value && !found.some((i) => i.path === value)) options.push({ value, label: value, detail: "", verbatim: true });
|
||||
const current = check?.path === value.trim() ? check : null;
|
||||
const hint = current?.version ? versionHint(tool, current.version, serverMajor) : null;
|
||||
return (
|
||||
<>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Path to executable:</T>
|
||||
</span>
|
||||
<div className={styles.formRow}>
|
||||
{found.length > 0 ? (
|
||||
<Select aria-label="Path to executable" value={value || null} options={options} onChange={onChange} />
|
||||
) : (
|
||||
<Input
|
||||
mono
|
||||
className={styles.dumpPath}
|
||||
aria-label="Path to executable"
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
<IconButton icon="folder" title="Browse…" onClick={() => void browse()} />
|
||||
</div>
|
||||
<span />
|
||||
<span className={clsx(styles.formHint, (current?.error || hint?.warn) && styles.dumpError)}>
|
||||
{installs && !value.trim()
|
||||
? tx`${program} was not found. Install the PostgreSQL client tools or choose the executable.`
|
||||
: (hint?.text ?? current?.error ?? "\u00a0")}
|
||||
</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** What made the dump at `path`, read from its header (`null` while unknown). */
|
||||
export function useDumpKind(path: string): DbDumpKind | null {
|
||||
const [known, setKnown] = useState<{ path: string; kind: DbDumpKind } | null>(null);
|
||||
useEffect(() => {
|
||||
const p = path.trim();
|
||||
if (!p) return;
|
||||
let live = true;
|
||||
const timer = setTimeout(() => {
|
||||
call(commands.dbDumpKind(p))
|
||||
.then((kind) => live && setKnown({ path: p, kind }))
|
||||
.catch(() => live && setKnown({ path: p, kind: "unknown" }));
|
||||
}, 250);
|
||||
return () => {
|
||||
live = false;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [path]);
|
||||
return known?.path === path.trim() ? known.kind : null;
|
||||
}
|
||||
|
||||
/** Why the chosen tool cannot restore the chosen dump, as a hint under the path. */
|
||||
export function mismatchText(m: DumpMismatch): string {
|
||||
switch (m) {
|
||||
case "mysqlIntoPostgres":
|
||||
return tx("This is a MySQL dump: restore it into a MySQL data source.");
|
||||
case "archiveIntoPsql":
|
||||
return tx("This is a pg_dump archive: restore it with pg_restore.");
|
||||
case "sqlIntoPgRestore":
|
||||
return tx("This is a plain SQL dump: restore it with psql.");
|
||||
}
|
||||
}
|
||||
|
||||
/** The command line the backend would run for `job` on target `id` (empty while unknown). */
|
||||
function useToolPreview(id: string, executable: string, job: DbToolJob): string {
|
||||
const key = JSON.stringify([id, executable, job]);
|
||||
const [shown, setShown] = useState<{ key: string; line: string } | null>(null);
|
||||
useEffect(() => {
|
||||
let live = true;
|
||||
const timer = setTimeout(() => {
|
||||
call(commands.dbToolPreview(id, executable, job))
|
||||
.then((line) => live && setShown({ key, line }))
|
||||
.catch(() => live && setShown({ key, line: "" }));
|
||||
}, 150);
|
||||
return () => {
|
||||
live = false;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
// `key` stands for the three inputs.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [key]);
|
||||
return shown?.key === key ? shown.line : "";
|
||||
}
|
||||
|
||||
function Preview({ text }: { text: string }) {
|
||||
return (
|
||||
<div className={clsx(styles.dumpPreview, selectableText)} onKeyDown={(e) => void copySelectionOnKeyDown(e)}>
|
||||
<code aria-label="Command line">{text || "\u00a0"}</code>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** The current time, ticking each second while `live`. */
|
||||
function useNow(live: boolean): number {
|
||||
const [now, setNow] = useState(() => Date.now());
|
||||
useEffect(() => {
|
||||
if (!live) return;
|
||||
const timer = setInterval(() => setNow(Date.now()), 1000);
|
||||
return () => clearInterval(timer);
|
||||
}, [live]);
|
||||
return now;
|
||||
}
|
||||
|
||||
/** Live output of a run, followed while scrolled to the bottom. */
|
||||
export function RunOutput({ runKey }: { runKey: number }) {
|
||||
const run = useToolRuns((s) => s.runs[runKey]);
|
||||
const now = useNow(run?.status === "running");
|
||||
const scroller = useRef<HTMLDivElement>(null);
|
||||
const stick = useRef(true);
|
||||
useLayoutEffect(() => {
|
||||
const el = scroller.current;
|
||||
if (el && stick.current) el.scrollTop = el.scrollHeight;
|
||||
}, [run?.lines]);
|
||||
if (!run) return null;
|
||||
const took = formatElapsed((run.endedAt ?? now) - run.startedAt);
|
||||
const status =
|
||||
run.status === "running"
|
||||
? tx`Running ${PROGRAM[run.tool]}… ${took}`
|
||||
: run.status === "done"
|
||||
? tx`Finished in ${took}`
|
||||
: run.status === "cancelled"
|
||||
? tx`Stopped after ${took}`
|
||||
: (run.error ?? tx`Failed with exit code ${String(run.exit ?? "?")} after ${took}`);
|
||||
return (
|
||||
<div className={styles.dumpRun}>
|
||||
<div className={styles.formRow}>
|
||||
<span
|
||||
className={clsx(styles.formHint, styles.dumpStatus, run.status === "failed" && styles.dumpError)}
|
||||
role="status"
|
||||
>
|
||||
{status}
|
||||
<span className={styles.dumpRoute}>{run.route}</span>
|
||||
</span>
|
||||
<IconButton
|
||||
icon="copy"
|
||||
title="Copy Output"
|
||||
disabled={!run.lines.length}
|
||||
onClick={() => void copyText(run.lines.map((l) => l.text).join("\n"))}
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
ref={scroller}
|
||||
className={clsx(styles.dumpLog, selectableText)}
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => void copySelectionOnKeyDown(e)}
|
||||
role="log"
|
||||
aria-label={tx`${PROGRAM[run.tool]} output`}
|
||||
onScroll={(e) => {
|
||||
const el = e.currentTarget;
|
||||
stick.current = el.scrollHeight - el.scrollTop - el.clientHeight < 8;
|
||||
}}
|
||||
>
|
||||
{run.lines.map((l, i) => (
|
||||
<div key={i} className={clsx(l.stderr && /error|fatal/i.test(l.text) && styles.dumpError)}>
|
||||
{l.text || " "}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Footer buttons while a run is shown: Stop while it runs, then Back to change the options. */
|
||||
export function runButtons(runKey: number, running: boolean, back: () => void): DialogButton[] {
|
||||
return running
|
||||
? [
|
||||
{ label: "Close", role: "cancel" },
|
||||
{ label: "Stop", role: "default", onClick: () => stopToolRun(runKey) },
|
||||
]
|
||||
: [
|
||||
{ label: "Back", role: "extra", onClick: back },
|
||||
{ label: "Close", role: "default", onClick: closeDialog },
|
||||
];
|
||||
}
|
||||
|
||||
export function useHome(): string | null {
|
||||
const [home, setHome] = useState<string | null>(null);
|
||||
useEffect(() => {
|
||||
void import("@tauri-apps/api/path")
|
||||
.then((p) => p.homeDir())
|
||||
.then(setHome)
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
return home;
|
||||
}
|
||||
|
||||
export function DumpDialog({
|
||||
target,
|
||||
database: initialDatabase,
|
||||
schemas,
|
||||
tables,
|
||||
serverVersion,
|
||||
}: {
|
||||
target: string;
|
||||
database: string;
|
||||
schemas?: string;
|
||||
tables?: string;
|
||||
serverVersion: number | null;
|
||||
}) {
|
||||
const source = sourceById(target);
|
||||
const home = useHome();
|
||||
const [executable, setExecutable] = useState("");
|
||||
const [database, setDatabase] = useState(initialDatabase);
|
||||
const [options, setOptions] = useState<DumpForm>({
|
||||
...DEFAULT_DUMP,
|
||||
schemas: schemas ?? "",
|
||||
tables: tables ?? "",
|
||||
});
|
||||
const [edited, setOutput] = useState<string | null>(null);
|
||||
const output = edited ?? (home ? joinPath(home, "{data_source}-{database}-{timestamp}.sql") : "");
|
||||
const [runKey, setRunKey] = useState<number | null>(null);
|
||||
const running = useToolRuns((s) => (runKey !== null ? s.runs[runKey]?.status === "running" : false));
|
||||
|
||||
const set =
|
||||
<K extends keyof DumpForm>(k: K) =>
|
||||
(v: DumpForm[K]) =>
|
||||
setOptions((o) => ({ ...o, [k]: v }));
|
||||
const plain = options.format === "plain";
|
||||
const runTarget = targetOf(target, database);
|
||||
const jobFor = (file: string): DbToolJob => ({ tool: "pgDump", file, options: dumpOptions(options) });
|
||||
const preview = useToolPreview(runTarget, executable || "pg_dump", jobFor(output));
|
||||
|
||||
const browse = async () => {
|
||||
try {
|
||||
const { save } = await import("@/platform/dialog");
|
||||
const path = await save({ defaultPath: output });
|
||||
if (path) setOutput(path);
|
||||
} catch {
|
||||
// No dialog (tests): keep typing the path.
|
||||
}
|
||||
};
|
||||
|
||||
const run = async () => {
|
||||
const file = outputPath(output, { dataSource: source?.name ?? "", database, now: new Date() });
|
||||
const job = jobFor(file);
|
||||
localStorage.setItem(pathKey("pgDump"), executable);
|
||||
const commandLine = await call(commands.dbToolPreview(runTarget, executable, job)).catch(() => "");
|
||||
setRunKey(
|
||||
startToolRun({
|
||||
target: runTarget,
|
||||
executable,
|
||||
job,
|
||||
commandLine,
|
||||
output: file,
|
||||
from: database || (source?.name ?? target),
|
||||
to: file,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const buttons: DialogButton[] =
|
||||
runKey !== null
|
||||
? runButtons(runKey, running, () => setRunKey(null))
|
||||
: [
|
||||
{ label: "Cancel", role: "cancel" },
|
||||
{ label: "Run", role: "default", disabled: !executable.trim() || !output.trim(), onClick: () => void run() },
|
||||
];
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
onClose={closeDialog}
|
||||
title={tx`Export with 'pg_dump' (${source?.name ?? target})`}
|
||||
width={720}
|
||||
buttons={buttons}
|
||||
>
|
||||
{runKey !== null ? (
|
||||
<RunOutput runKey={runKey} />
|
||||
) : (
|
||||
<div className={styles.form}>
|
||||
<ExecutableField
|
||||
tool="pgDump"
|
||||
value={executable}
|
||||
onChange={setExecutable}
|
||||
serverMajor={majorOfServer(serverVersion)}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Database:</T>
|
||||
</span>
|
||||
<Input aria-label="Database" value={database} onChange={(e) => setDatabase(e.target.value)} />
|
||||
<span className={styles.formLabel}>
|
||||
<T>Output result to:</T>
|
||||
</span>
|
||||
<div className={styles.formRow}>
|
||||
<Input
|
||||
mono
|
||||
className={styles.dumpPath}
|
||||
aria-label="Output result to"
|
||||
value={output}
|
||||
onChange={(e) => setOutput(e.target.value)}
|
||||
/>
|
||||
<IconButton icon="folder" title="Browse…" onClick={() => void browse()} />
|
||||
</div>
|
||||
<span />
|
||||
<span className={styles.formHint}>
|
||||
<T>Allowed substitution patterns:</T> {"{timestamp}, {data_source}, {database}"}
|
||||
</span>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Format:</T>
|
||||
</span>
|
||||
<Select
|
||||
aria-label="Format"
|
||||
value={options.format}
|
||||
options={DUMP_FORMATS.map((f) => ({ value: f.id, label: f.label }))}
|
||||
onChange={(f: DbDumpFormat) => {
|
||||
set("format")(f);
|
||||
setOutput(withExtension(output, f));
|
||||
}}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Statements:</T>
|
||||
</span>
|
||||
<Select
|
||||
aria-label="Statements"
|
||||
value={options.statements}
|
||||
options={[
|
||||
{ value: "copy", label: "COPY" },
|
||||
{ value: "inserts", label: "INSERT" },
|
||||
{ value: "columnInserts", label: "INSERT with column names" },
|
||||
]}
|
||||
onChange={(v: DbDumpStatements) => set("statements")(v)}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Content:</T>
|
||||
</span>
|
||||
<Select aria-label="Content" value={options.content} options={CONTENT_OPTIONS} onChange={set("content")} />
|
||||
{(
|
||||
[
|
||||
["schemas", "Schemas:"],
|
||||
["tables", "Tables:"],
|
||||
["excludeTables", "Exclude tables:"],
|
||||
] as const
|
||||
).map(([k, label]) => (
|
||||
<Fragment key={k}>
|
||||
<span className={styles.formLabel}>{tx(label)}</span>
|
||||
<Input
|
||||
mono
|
||||
aria-label={tx(label)}
|
||||
placeholder="All"
|
||||
value={options[k]}
|
||||
onChange={(e) => set(k)(e.target.value)}
|
||||
/>
|
||||
</Fragment>
|
||||
))}
|
||||
{options.format === "directory" && (
|
||||
<>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Parallel jobs:</T>
|
||||
</span>
|
||||
<Input
|
||||
aria-label="Parallel jobs"
|
||||
type="number"
|
||||
min={1}
|
||||
value={String(options.jobs)}
|
||||
onChange={(e) => set("jobs")(Math.max(1, Number(e.target.value) || 1))}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<span />
|
||||
<div className={styles.dumpChecks}>
|
||||
<Checkbox
|
||||
checked={plain && options.clean}
|
||||
disabled={!plain}
|
||||
onChange={set("clean")}
|
||||
label="Add DROP before CREATE"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={plain && options.clean && options.ifExists}
|
||||
disabled={!plain || !options.clean}
|
||||
onChange={set("ifExists")}
|
||||
label="Use DROP … IF EXISTS"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={plain && options.create}
|
||||
disabled={!plain}
|
||||
onChange={set("create")}
|
||||
label="Add CREATE DATABASE"
|
||||
/>
|
||||
<Checkbox checked={options.noOwner} onChange={set("noOwner")} label="Skip ownership (--no-owner)" />
|
||||
<Checkbox
|
||||
checked={options.noPrivileges}
|
||||
onChange={set("noPrivileges")}
|
||||
label="Skip privileges (--no-privileges)"
|
||||
/>
|
||||
</div>
|
||||
<Preview text={preview} />
|
||||
</div>
|
||||
)}
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export function RestoreDialog({
|
||||
target,
|
||||
database: initialDatabase,
|
||||
serverVersion,
|
||||
}: {
|
||||
target: string;
|
||||
database: string;
|
||||
serverVersion: number | null;
|
||||
}) {
|
||||
const source = sourceById(target);
|
||||
const [picked, setPicked] = useState<"pgRestore" | "psql" | null>(null);
|
||||
const [executables, setExecutables] = useState<Record<"pgRestore" | "psql", string>>({ pgRestore: "", psql: "" });
|
||||
const [database, setDatabase] = useState(initialDatabase);
|
||||
const [file, setFile] = useState("");
|
||||
const [restore, setRestore] = useState<RestoreForm>(DEFAULT_RESTORE);
|
||||
const [psql, setPsql] = useState<DbPsqlOptions>(DEFAULT_PSQL);
|
||||
const [runKey, setRunKey] = useState<number | null>(null);
|
||||
const running = useToolRuns((s) => (runKey !== null ? s.runs[runKey]?.status === "running" : false));
|
||||
const kind = useDumpKind(file);
|
||||
const tool = picked ?? restoreToolFor(file, kind);
|
||||
const mismatch = dumpMismatch(tool, kind);
|
||||
const executable = executables[tool];
|
||||
const readOnly = !!source?.readOnly;
|
||||
|
||||
const changeFile = setFile;
|
||||
const browse = async (directory: boolean) => {
|
||||
try {
|
||||
const { open } = await import("@/platform/dialog");
|
||||
const path = await open({ multiple: false, directory, title: tx("Path to dump") });
|
||||
if (typeof path === "string") changeFile(path);
|
||||
} catch {
|
||||
// No dialog (tests): keep typing the path.
|
||||
}
|
||||
};
|
||||
|
||||
const runTarget = targetOf(target, database);
|
||||
const job: DbToolJob =
|
||||
tool === "psql" ? { tool, file, options: psql } : { tool, file, options: restoreOptions(restore) };
|
||||
const preview = useToolPreview(runTarget, executable || PROGRAM[tool], job);
|
||||
const setR =
|
||||
<K extends keyof RestoreForm>(k: K) =>
|
||||
(v: RestoreForm[K]) =>
|
||||
setRestore((o) => ({ ...o, [k]: v }));
|
||||
const setP =
|
||||
<K extends keyof DbPsqlOptions>(k: K) =>
|
||||
(v: DbPsqlOptions[K]) =>
|
||||
setPsql((o) => ({ ...o, [k]: v }));
|
||||
|
||||
const run = async () => {
|
||||
localStorage.setItem(pathKey(tool), executable);
|
||||
const commandLine = await call(commands.dbToolPreview(runTarget, executable, job)).catch(() => preview);
|
||||
setRunKey(
|
||||
startToolRun({
|
||||
target: runTarget,
|
||||
executable,
|
||||
job,
|
||||
commandLine,
|
||||
from: file,
|
||||
to: database || (source?.name ?? target),
|
||||
onSuccess: () => void refresh(runTarget),
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const buttons: DialogButton[] =
|
||||
runKey !== null
|
||||
? runButtons(runKey, running, () => setRunKey(null))
|
||||
: [
|
||||
{ label: "Cancel", role: "cancel" },
|
||||
{
|
||||
label: "Run",
|
||||
role: "default",
|
||||
disabled: readOnly || !!mismatch || !executable.trim() || !file.trim(),
|
||||
title: readOnly ? tx`The data source is read-only` : undefined,
|
||||
onClick: () => void run(),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
onClose={closeDialog}
|
||||
title={tx`Restore with 'pg_restore'/'psql' (${source?.name ?? target})`}
|
||||
width={720}
|
||||
buttons={buttons}
|
||||
>
|
||||
{runKey !== null ? (
|
||||
<RunOutput runKey={runKey} />
|
||||
) : (
|
||||
<>
|
||||
<Tabs
|
||||
variant="compact"
|
||||
aria-label="Restore tool"
|
||||
items={[
|
||||
{ id: "pgRestore", label: "pg_restore" },
|
||||
{ id: "psql", label: "psql" },
|
||||
]}
|
||||
activeId={tool}
|
||||
onSelect={(id) => setPicked(id as "pgRestore" | "psql")}
|
||||
/>
|
||||
<div className={styles.form}>
|
||||
<ExecutableField
|
||||
key={tool}
|
||||
tool={tool}
|
||||
value={executable}
|
||||
onChange={(v) => setExecutables((e) => ({ ...e, [tool]: v }))}
|
||||
serverMajor={majorOfServer(serverVersion)}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Database:</T>
|
||||
</span>
|
||||
<Input aria-label="Database" value={database} onChange={(e) => setDatabase(e.target.value)} />
|
||||
<span className={styles.formLabel}>
|
||||
<T>Path to dump:</T>
|
||||
</span>
|
||||
<div className={styles.formRow}>
|
||||
<Input
|
||||
mono
|
||||
className={styles.dumpPath}
|
||||
aria-label="Path to dump"
|
||||
value={file}
|
||||
onChange={(e) => changeFile(e.target.value)}
|
||||
/>
|
||||
<IconButton
|
||||
icon="folder"
|
||||
title="Browse…"
|
||||
onClick={(e) =>
|
||||
tool === "pgRestore"
|
||||
? showContextMenu(e, [
|
||||
{ label: "Choose File…", run: () => void browse(false) },
|
||||
{ label: "Choose Directory Archive…", run: () => void browse(true) },
|
||||
])
|
||||
: void browse(false)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<span />
|
||||
<span className={clsx(styles.formHint, mismatch && styles.dumpError)}>
|
||||
{mismatch ? (
|
||||
mismatchText(mismatch)
|
||||
) : tool === "psql" ? (
|
||||
<T>psql runs a plain SQL dump (.sql).</T>
|
||||
) : (
|
||||
<T>pg_restore reads custom, directory and tar archives.</T>
|
||||
)}
|
||||
</span>
|
||||
{tool === "pgRestore" ? (
|
||||
<>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Content:</T>
|
||||
</span>
|
||||
<Select
|
||||
aria-label="Content"
|
||||
value={restore.content}
|
||||
options={CONTENT_OPTIONS}
|
||||
onChange={setR("content")}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Schemas:</T>
|
||||
</span>
|
||||
<Input
|
||||
mono
|
||||
aria-label="Schemas"
|
||||
placeholder="All"
|
||||
value={restore.schemas}
|
||||
onChange={(e) => setR("schemas")(e.target.value)}
|
||||
/>
|
||||
<span className={styles.formLabel}>
|
||||
<T>Parallel jobs:</T>
|
||||
</span>
|
||||
<Input
|
||||
aria-label="Parallel jobs"
|
||||
type="number"
|
||||
min={1}
|
||||
disabled={restore.singleTransaction}
|
||||
value={String(restore.jobs)}
|
||||
onChange={(e) => setR("jobs")(Math.max(1, Number(e.target.value) || 1))}
|
||||
/>
|
||||
<span />
|
||||
<div className={styles.dumpChecks}>
|
||||
<Checkbox
|
||||
checked={restore.clean}
|
||||
onChange={setR("clean")}
|
||||
label="Drop objects before recreating them"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={restore.clean && restore.ifExists}
|
||||
disabled={!restore.clean}
|
||||
onChange={setR("ifExists")}
|
||||
label="Use DROP … IF EXISTS"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={restore.singleTransaction}
|
||||
onChange={setR("singleTransaction")}
|
||||
label="Wrap the restore in BEGIN … COMMIT"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={restore.exitOnError}
|
||||
onChange={setR("exitOnError")}
|
||||
label="Stop on the first error"
|
||||
/>
|
||||
<Checkbox checked={restore.noOwner} onChange={setR("noOwner")} label="Skip ownership (--no-owner)" />
|
||||
<Checkbox
|
||||
checked={restore.noPrivileges}
|
||||
onChange={setR("noPrivileges")}
|
||||
label="Skip privileges (--no-privileges)"
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<span />
|
||||
<div className={styles.dumpChecks}>
|
||||
<Checkbox
|
||||
checked={psql.singleTransaction}
|
||||
onChange={setP("singleTransaction")}
|
||||
label="Wrap the restore in BEGIN … COMMIT"
|
||||
/>
|
||||
<Checkbox checked={psql.stopOnError} onChange={setP("stopOnError")} label="Stop on the first error" />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{readOnly && (
|
||||
<>
|
||||
<span />
|
||||
<span className={clsx(styles.formHint, styles.dumpError)}>
|
||||
<T>The data source is read-only.</T>
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
<Preview text={preview} />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { DataSource, DbTableInfo } from "@/platform/ipc";
|
||||
import {
|
||||
DEFAULT_DUMP,
|
||||
DEFAULT_RESTORE,
|
||||
baseName,
|
||||
dumpMismatch,
|
||||
dumpOptions,
|
||||
dumpScope,
|
||||
formatElapsed,
|
||||
majorOf,
|
||||
outputPath,
|
||||
pickInstall,
|
||||
restoreOptions,
|
||||
restoreToolFor,
|
||||
versionFit,
|
||||
withExtension,
|
||||
} from "./dumpTools";
|
||||
|
||||
const pg = { id: "s1", name: "Shop / dev", driver: "postgres", host: "localhost", user: "app" } as DataSource;
|
||||
|
||||
describe("forms", () => {
|
||||
it("send the typed name lists as lists", () => {
|
||||
const options = dumpOptions({
|
||||
...DEFAULT_DUMP,
|
||||
schemas: "public, audit",
|
||||
tables: " public.orders ",
|
||||
excludeTables: "",
|
||||
});
|
||||
expect([options.schemas, options.tables, options.excludeTables]).toEqual([
|
||||
["public", "audit"],
|
||||
["public.orders"],
|
||||
[],
|
||||
]);
|
||||
expect(restoreOptions({ ...DEFAULT_RESTORE, schemas: "a b" }).schemas).toEqual(["a", "b"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("restore", () => {
|
||||
it("picks psql for .sql files and pg_restore for the rest", () => {
|
||||
expect(restoreToolFor("/tmp/a.SQL")).toBe("psql");
|
||||
expect(restoreToolFor("/tmp/a.dump")).toBe("pgRestore");
|
||||
expect(restoreToolFor("/tmp/dir")).toBe("pgRestore");
|
||||
});
|
||||
|
||||
it("trusts the dump's header over its extension", () => {
|
||||
expect(restoreToolFor("/tmp/a.backup", "postgresSql")).toBe("psql");
|
||||
expect(restoreToolFor("/tmp/a.sql", "postgresArchive")).toBe("pgRestore");
|
||||
});
|
||||
|
||||
it("refuses a dump the tool cannot read", () => {
|
||||
expect(dumpMismatch("psql", "mysqlSql")).toBe("mysqlIntoPostgres");
|
||||
expect(dumpMismatch("psql", "postgresArchive")).toBe("archiveIntoPsql");
|
||||
expect(dumpMismatch("pgRestore", "postgresSql")).toBe("sqlIntoPgRestore");
|
||||
expect(dumpMismatch("psql", "postgresSql")).toBeNull();
|
||||
expect(dumpMismatch("psql", "unknown")).toBeNull();
|
||||
expect(dumpMismatch("pgRestore", null)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("paths", () => {
|
||||
it("fills the patterns with file-safe values", () => {
|
||||
const now = new Date(2026, 2, 7, 9, 5, 2);
|
||||
expect(
|
||||
outputPath("/tmp/{data_source}-{database}-{timestamp}.sql", { dataSource: "Shop / dev", database: "shop", now }),
|
||||
).toBe("/tmp/Shop_dev-shop-2026-03-07_09-05-02.sql");
|
||||
});
|
||||
|
||||
it("follows the format's extension", () => {
|
||||
expect(withExtension("/tmp/x-{timestamp}.sql", "custom")).toBe("/tmp/x-{timestamp}.dump");
|
||||
expect(withExtension("/tmp/x.dump", "directory")).toBe("/tmp/x");
|
||||
expect(withExtension("/tmp/x", "plain")).toBe("/tmp/x.sql");
|
||||
});
|
||||
});
|
||||
|
||||
describe("dumpScope", () => {
|
||||
const table = { name: "Orders", kind: "table" } as DbTableInfo;
|
||||
|
||||
it("applies to PostgreSQL sources, databases, schemas and tables", () => {
|
||||
const shop = { ...pg, database: "shop" };
|
||||
expect(dumpScope({ kind: "source", key: "s1", source: shop }, shop)).toEqual({ target: "s1", database: "shop" });
|
||||
expect(dumpScope({ kind: "database", key: "k", id: "s1:crm", name: "crm", current: false }, pg)).toEqual({
|
||||
target: "s1:crm",
|
||||
database: "crm",
|
||||
});
|
||||
expect(dumpScope({ kind: "schema", key: "k", id: "s1:crm", schema: "public" }, pg)).toMatchObject({
|
||||
schemas: "public",
|
||||
});
|
||||
expect(dumpScope({ kind: "table", key: "k", id: "s1:crm", schema: "public", table }, pg)).toMatchObject({
|
||||
tables: 'public."Orders"',
|
||||
});
|
||||
});
|
||||
|
||||
it("does not apply to other drivers or views", () => {
|
||||
const mysql = { ...pg, driver: "mysql" } as DataSource;
|
||||
expect(dumpScope({ kind: "source", key: "s1", source: mysql }, mysql)).toBeNull();
|
||||
const view = { ...table, kind: "view" } as DbTableInfo;
|
||||
expect(dumpScope({ kind: "table", key: "k", id: "s1", schema: "public", table: view }, pg)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("client and server versions", () => {
|
||||
const installs = [
|
||||
{ path: "/opt/homebrew/bin/pg_dump", version: "pg_dump (PostgreSQL) 18.4 (Homebrew)" },
|
||||
{ path: "/opt/homebrew/opt/postgresql@15/bin/pg_dump", version: "pg_dump (PostgreSQL) 15.13 (Homebrew)" },
|
||||
];
|
||||
|
||||
it("reads the major version", () => {
|
||||
expect(majorOf("pg_dump (PostgreSQL) 18.4 (Homebrew)")).toBe(18);
|
||||
expect(majorOf("psql (PostgreSQL) 9.6.24")).toBe(9);
|
||||
expect(majorOf("unknown")).toBeNull();
|
||||
});
|
||||
|
||||
it("flags a pg_dump older than the server, and dumps an older server cannot load", () => {
|
||||
expect(versionFit("pgDump", 14, 15)).toBe("tooOld");
|
||||
expect(versionFit("pgDump", 18, 15)).toBe("newer");
|
||||
expect(versionFit("pgRestore", 18, 15)).toBe("newer");
|
||||
expect(versionFit("pgRestore", 14, 15)).toBe("ok");
|
||||
expect(versionFit("psql", 18, 15)).toBe("ok");
|
||||
expect(versionFit("pgDump", 15, null)).toBe("ok");
|
||||
});
|
||||
|
||||
it("preselects the server's version, else the last used, else the newest", () => {
|
||||
expect(pickInstall(installs, 15, "/usr/bin/pg_dump")).toBe("/opt/homebrew/opt/postgresql@15/bin/pg_dump");
|
||||
expect(pickInstall(installs, 16, "/usr/bin/pg_dump")).toBe("/usr/bin/pg_dump");
|
||||
expect(pickInstall(installs, 16, null)).toBe("/opt/homebrew/bin/pg_dump");
|
||||
expect(pickInstall([], null, null)).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("run summaries", () => {
|
||||
it("formats how long a run took", () => {
|
||||
expect(formatElapsed(350)).toBe("< 1 s");
|
||||
expect(formatElapsed(4_200)).toBe("4 s");
|
||||
expect(formatElapsed(724_000)).toBe("12 min 4 s");
|
||||
expect(formatElapsed(7_500_000)).toBe("2 h 5 min");
|
||||
});
|
||||
|
||||
it("shortens paths to their file name", () => {
|
||||
expect(baseName("/tmp/dumps/shop.sql")).toBe("shop.sql");
|
||||
expect(baseName("C:\\dumps\\shop.dump")).toBe("shop.dump");
|
||||
expect(baseName("/tmp/dir/")).toBe("dir");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,180 @@
|
||||
/** The dump / restore dialogs' model: their forms, file names, tool choice and version hints. */
|
||||
import type {
|
||||
DataSource,
|
||||
DbDumpFormat,
|
||||
DbDumpKind,
|
||||
DbPgDumpOptions,
|
||||
DbPgRestoreOptions,
|
||||
DbPsqlOptions,
|
||||
DbTool,
|
||||
DbToolInstall,
|
||||
} from "@/platform/ipc";
|
||||
import { splitTarget } from "./model";
|
||||
import { nodeTarget, type DbNode } from "./nodes";
|
||||
|
||||
export const DUMP_FORMATS: readonly { id: DbDumpFormat; label: string; extension: string }[] = [
|
||||
{ id: "plain", label: "Plain SQL", extension: ".sql" },
|
||||
{ id: "custom", label: "Custom archive", extension: ".dump" },
|
||||
{ id: "directory", label: "Directory", extension: "" },
|
||||
{ id: "tar", label: "Tar archive", extension: ".tar" },
|
||||
];
|
||||
|
||||
/** A name list as typed in a field (`public, audit`). */
|
||||
type Typed<T, K extends keyof T> = Omit<T, K> & { [P in K]: string };
|
||||
|
||||
export type DumpForm = Typed<DbPgDumpOptions, "schemas" | "tables" | "excludeTables">;
|
||||
export type RestoreForm = Typed<DbPgRestoreOptions, "schemas">;
|
||||
|
||||
export const DEFAULT_DUMP: DumpForm = {
|
||||
format: "plain",
|
||||
statements: "copy",
|
||||
content: "all",
|
||||
schemas: "",
|
||||
tables: "",
|
||||
excludeTables: "",
|
||||
clean: false,
|
||||
ifExists: false,
|
||||
create: false,
|
||||
noOwner: false,
|
||||
noPrivileges: false,
|
||||
jobs: 1,
|
||||
};
|
||||
|
||||
export const DEFAULT_RESTORE: RestoreForm = {
|
||||
content: "all",
|
||||
schemas: "",
|
||||
clean: false,
|
||||
ifExists: false,
|
||||
noOwner: false,
|
||||
noPrivileges: false,
|
||||
singleTransaction: false,
|
||||
exitOnError: true,
|
||||
jobs: 1,
|
||||
};
|
||||
|
||||
export const DEFAULT_PSQL: DbPsqlOptions = { singleTransaction: true, stopOnError: true };
|
||||
|
||||
/** `a, b c` → `["a", "b", "c"]`. */
|
||||
export function nameList(text: string): string[] {
|
||||
return text.split(/[\s,]+/).filter(Boolean);
|
||||
}
|
||||
|
||||
export function dumpOptions(f: DumpForm): DbPgDumpOptions {
|
||||
return { ...f, schemas: nameList(f.schemas), tables: nameList(f.tables), excludeTables: nameList(f.excludeTables) };
|
||||
}
|
||||
|
||||
export function restoreOptions(f: RestoreForm): DbPgRestoreOptions {
|
||||
return { ...f, schemas: nameList(f.schemas) };
|
||||
}
|
||||
|
||||
/**
|
||||
* The PostgreSQL tool for a dump: from its header when known, else `psql` for a `.sql` file and
|
||||
* `pg_restore` for anything else (archives, directories).
|
||||
*/
|
||||
export function restoreToolFor(path: string, kind: DbDumpKind | null = null): Extract<DbTool, "pgRestore" | "psql"> {
|
||||
if (kind === "postgresArchive") return "pgRestore";
|
||||
if (kind === "postgresSql") return "psql";
|
||||
return /\.sql$/i.test(path.trim()) ? "psql" : "pgRestore";
|
||||
}
|
||||
|
||||
export type DumpMismatch = "mysqlIntoPostgres" | "archiveIntoPsql" | "sqlIntoPgRestore";
|
||||
|
||||
/** Why `tool` cannot restore a dump of `kind`; `null` when it can, or the kind is unknown. */
|
||||
export function dumpMismatch(tool: DbTool, kind: DbDumpKind | null): DumpMismatch | null {
|
||||
if (!kind || kind === "unknown") return null;
|
||||
if (kind === "mysqlSql") return "mysqlIntoPostgres";
|
||||
if (tool === "psql" && kind === "postgresArchive") return "archiveIntoPsql";
|
||||
if (tool === "pgRestore" && kind === "postgresSql") return "sqlIntoPgRestore";
|
||||
return null;
|
||||
}
|
||||
|
||||
/** `2026-03-07_09-05-02` (local time). */
|
||||
export function timestamp(d: Date): string {
|
||||
const p = (n: number) => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}_${p(d.getHours())}-${p(d.getMinutes())}-${p(d.getSeconds())}`;
|
||||
}
|
||||
|
||||
const unsafe = /[\\/:*?"<>|\s]+/g;
|
||||
|
||||
/** Fills `{data_source}`, `{database}` and `{timestamp}` of an output path pattern. */
|
||||
export function outputPath(pattern: string, values: { dataSource: string; database: string; now: Date }): string {
|
||||
return pattern
|
||||
.replaceAll("{data_source}", values.dataSource.replace(unsafe, "_"))
|
||||
.replaceAll("{database}", values.database.replace(unsafe, "_"))
|
||||
.replaceAll("{timestamp}", timestamp(values.now));
|
||||
}
|
||||
|
||||
/** The pattern with the format's extension in place of the previous one. */
|
||||
export function withExtension(pattern: string, format: DbDumpFormat): string {
|
||||
const ext = DUMP_FORMATS.find((f) => f.id === format)!.extension;
|
||||
return pattern.replace(/\.(sql|dump|backup|tar)$/i, "") + ext;
|
||||
}
|
||||
|
||||
/** A name as a `pg_dump` pattern: quoted unless it is a lower-case identifier. */
|
||||
function patternName(name: string): string {
|
||||
return /^[a-z_][a-z0-9_$]*$/.test(name) ? name : `"${name.replaceAll('"', '""')}"`;
|
||||
}
|
||||
|
||||
export interface DumpScope {
|
||||
target: string;
|
||||
database: string;
|
||||
schemas?: string;
|
||||
tables?: string;
|
||||
}
|
||||
|
||||
/** What the dump / restore actions act on for tree node `n`; `null` when they don't apply. */
|
||||
export function dumpScope(n: DbNode, source: DataSource | undefined): DumpScope | null {
|
||||
const target = nodeTarget(n);
|
||||
if (!target || source?.driver !== "postgres") return null;
|
||||
const database = n.kind === "database" ? n.name : (splitTarget(target).database ?? source.database ?? "");
|
||||
if (n.kind === "source" || n.kind === "database") return { target, database };
|
||||
if (n.kind === "schema") return { target, database, schemas: patternName(n.schema) };
|
||||
if (n.kind === "table" && n.table.kind === "table")
|
||||
return { target, database, tables: `${patternName(n.schema)}.${patternName(n.table.name)}` };
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The major version in a `--version` line (`pg_dump (PostgreSQL) 17.2` → 17). */
|
||||
export function majorOf(version: string): number | null {
|
||||
const m = /(\d+)(?:\.\d+)?/.exec(version);
|
||||
return m ? Number(m[1]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* How a tool's major version fits the server's: `pg_dump` refuses an older-than-server
|
||||
* copy, and what newer `pg_dump` / `pg_restore` write may not load into an older server
|
||||
* (17 and later set `transaction_timeout`, unknown before 17).
|
||||
*/
|
||||
export function versionFit(tool: DbTool, toolMajor: number | null, serverMajor: number | null) {
|
||||
if (tool === "psql" || toolMajor === null || serverMajor === null || toolMajor === serverMajor) return "ok";
|
||||
if (toolMajor < serverMajor) return tool === "pgDump" ? "tooOld" : "ok";
|
||||
return "newer";
|
||||
}
|
||||
|
||||
/** The copy to preselect: the server's major version, else the one used last, else the newest. */
|
||||
export function pickInstall(installs: DbToolInstall[], serverMajor: number | null, last: string | null): string {
|
||||
const matching = installs.find((i) => majorOf(i.version) === serverMajor);
|
||||
return matching?.path ?? last ?? installs[0]?.path ?? "";
|
||||
}
|
||||
|
||||
/** `< 1 s`, `4 s`, `12 min 4 s`, `2 h 5 min`. */
|
||||
export function formatElapsed(ms: number): string {
|
||||
if (ms < 1000) return "< 1 s";
|
||||
const total = Math.floor(ms / 1000);
|
||||
const h = Math.floor(total / 3600);
|
||||
const m = Math.floor((total % 3600) / 60);
|
||||
const sec = total % 60;
|
||||
if (h) return `${h} h ${m} min`;
|
||||
if (m) return `${m} min ${sec} s`;
|
||||
return `${sec} s`;
|
||||
}
|
||||
|
||||
/** The last part of a path, for one-line summaries. */
|
||||
export function baseName(path: string): string {
|
||||
return (
|
||||
path
|
||||
.replace(/[\\/]+$/, "")
|
||||
.split(/[\\/]/)
|
||||
.pop() || path
|
||||
);
|
||||
}
|
||||
@@ -146,7 +146,11 @@ export type DialogState =
|
||||
/** Import Data from File into a table. */
|
||||
| { kind: "import"; id: string; schema: string; table: string }
|
||||
/** Compare Structure: `source` starts as the schema the action was invoked on. */
|
||||
| { kind: "compareSchemas"; source: { id: string; schema: string } | null };
|
||||
| { kind: "compareSchemas"; source: { id: string; schema: string } | null }
|
||||
/** Export with 'pg_dump' of a database, schema or table. */
|
||||
| { kind: "dump"; target: string; database: string; schemas?: string; tables?: string; serverVersion: number | null }
|
||||
/** Restore with 'pg_restore' / 'psql' into a database. */
|
||||
| { kind: "restore"; target: string; database: string; serverVersion: number | null };
|
||||
|
||||
/** The schemas and tables of a data source's databases, as far as introspection has read them. */
|
||||
export interface SourceCatalog {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/** Runs of `pg_dump` / `pg_restore` / `psql`: they outlive the dialog that started them. */
|
||||
import { Channel } from "@tauri-apps/api/core";
|
||||
import { create } from "zustand";
|
||||
import {
|
||||
call,
|
||||
commands,
|
||||
isCancelled,
|
||||
nextRequestId,
|
||||
type DbTool,
|
||||
type DbToolJob,
|
||||
type DbToolLine,
|
||||
type RequestId,
|
||||
} from "@/platform/ipc";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { canRevealInFileManager, revealInFileManager, revealInFileManagerLabel } from "@/platform/fileManager";
|
||||
import { notifications } from "@/workbench/contributions";
|
||||
import { baseName, formatElapsed } from "./dumpTools";
|
||||
import { log, targetLabel, withConnection } from "./store";
|
||||
|
||||
const MAX_LINES = 5000;
|
||||
|
||||
export const TOOL_PROGRAMS: Record<DbTool, string> = {
|
||||
pgDump: "pg_dump",
|
||||
pgRestore: "pg_restore",
|
||||
psql: "psql",
|
||||
};
|
||||
|
||||
export interface ToolRun {
|
||||
key: number;
|
||||
tool: DbTool;
|
||||
target: string;
|
||||
lines: DbToolLine[];
|
||||
status: "running" | "done" | "failed" | "cancelled";
|
||||
/** The process's exit code, or why it did not start. */
|
||||
exit: number | null;
|
||||
error?: string;
|
||||
/** What moves where, e.g. `shop → shop-2026-03-07.sql` (file names shortened). */
|
||||
route: string;
|
||||
startedAt: number;
|
||||
endedAt?: number;
|
||||
}
|
||||
|
||||
export const useToolRuns = create<{ runs: Record<number, ToolRun> }>(() => ({ runs: {} }));
|
||||
|
||||
let nextKey = 1;
|
||||
const requests = new Map<number, RequestId>();
|
||||
|
||||
function update(key: number, change: (r: ToolRun) => Partial<ToolRun>) {
|
||||
useToolRuns.setState((s) => {
|
||||
const r = s.runs[key];
|
||||
return r ? { runs: { ...s.runs, [key]: { ...r, ...change(r) } } } : s;
|
||||
});
|
||||
}
|
||||
|
||||
export interface ToolRunRequest {
|
||||
target: string;
|
||||
executable: string;
|
||||
job: DbToolJob;
|
||||
/** Shown in the Output tab. */
|
||||
commandLine: string;
|
||||
/** The file or directory a dump writes, offered in the finish notification. */
|
||||
output?: string;
|
||||
/** Shown as `from → to`; paths are shortened to their file name. */
|
||||
from: string;
|
||||
to: string;
|
||||
onSuccess?: () => void;
|
||||
}
|
||||
|
||||
/** Starts the tool (asking for the password first when needed) and returns its run's key. */
|
||||
export function startToolRun(req: ToolRunRequest): number {
|
||||
const key = nextKey++;
|
||||
const run: ToolRun = {
|
||||
key,
|
||||
tool: req.job.tool,
|
||||
target: req.target,
|
||||
lines: [],
|
||||
status: "running",
|
||||
exit: null,
|
||||
route: `${shorten(req.from)} → ${shorten(req.to)}`,
|
||||
startedAt: Date.now(),
|
||||
};
|
||||
useToolRuns.setState((s) => ({ runs: { ...s.runs, [key]: run } }));
|
||||
log("statement", req.commandLine);
|
||||
void execute(key, req);
|
||||
return key;
|
||||
}
|
||||
|
||||
async function execute(key: number, req: ToolRunRequest) {
|
||||
const label = targetLabel(req.target);
|
||||
const program = TOOL_PROGRAMS[req.job.tool];
|
||||
try {
|
||||
const exit = await withConnection(req.target, () => {
|
||||
const request = nextRequestId();
|
||||
requests.set(key, request);
|
||||
const channel = new Channel<DbToolLine[]>();
|
||||
channel.onmessage = (batch) =>
|
||||
update(key, (r) => {
|
||||
const lines = r.lines.concat(batch);
|
||||
return { lines: lines.length > MAX_LINES ? lines.slice(lines.length - MAX_LINES) : lines };
|
||||
});
|
||||
return call(commands.dbRunTool(request, req.target, req.executable, req.job, channel));
|
||||
});
|
||||
const ok = exit === 0;
|
||||
const took = finish(key, { status: ok ? "done" : "failed", exit });
|
||||
const route = routeOf(key);
|
||||
log(ok ? "result" : "error", tx`${program} finished with exit code ${String(exit ?? "?")} in ${took}`);
|
||||
if (ok) req.onSuccess?.();
|
||||
const reveal =
|
||||
ok && req.output && canRevealInFileManager()
|
||||
? [{ label: revealInFileManagerLabel(), run: () => void revealInFileManager(req.output!) }]
|
||||
: [];
|
||||
notifications().notify({
|
||||
level: ok ? "info" : "error",
|
||||
title: ok ? tx`${program} finished: ${label}` : tx`${program} failed: ${label}`,
|
||||
body: ok
|
||||
? tx`${route} in ${took}`
|
||||
: errorTail(key) ||
|
||||
tx`Exit code ${String(exit ?? "?")}. The Database tool window's Output tab has the command.`,
|
||||
actions: reveal,
|
||||
key: `db-tool-${key}`,
|
||||
});
|
||||
} catch (e) {
|
||||
if (isCancelled(e)) {
|
||||
const took = finish(key, { status: "cancelled" });
|
||||
log("info", tx`${program} stopped after ${took}`);
|
||||
return;
|
||||
}
|
||||
const error = e instanceof Error ? e.message : String(e);
|
||||
finish(key, { status: "failed", error });
|
||||
log("error", error);
|
||||
notifications().notify({ level: "error", title: tx`Cannot run ${program}`, body: error, key: `db-tool-${key}` });
|
||||
} finally {
|
||||
requests.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
/** Records the end of run `key` and returns how long it took. */
|
||||
function finish(key: number, change: Partial<ToolRun>): string {
|
||||
const endedAt = Date.now();
|
||||
update(key, () => ({ ...change, endedAt }));
|
||||
return formatElapsed(endedAt - (useToolRuns.getState().runs[key]?.startedAt ?? endedAt));
|
||||
}
|
||||
|
||||
const routeOf = (key: number) => useToolRuns.getState().runs[key]?.route ?? "";
|
||||
|
||||
const shorten = (s: string) => (/[\\/]/.test(s) ? baseName(s) : s);
|
||||
|
||||
/** The last lines the tool wrote to stderr, where it reports what went wrong. */
|
||||
function errorTail(key: number): string {
|
||||
const lines = useToolRuns.getState().runs[key]?.lines ?? [];
|
||||
return lines
|
||||
.filter((l) => l.stderr)
|
||||
.slice(-3)
|
||||
.map((l) => l.text)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function stopToolRun(key: number) {
|
||||
const request = requests.get(key);
|
||||
if (request) void commands.cancelRequest(request);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import { canDrop, canModifyTable, canRename, reference, scriptSql, type ScriptKi
|
||||
import {
|
||||
DB_TOOL_WINDOW,
|
||||
OUTPUT_TAB,
|
||||
connect,
|
||||
RESULTS_VIEW,
|
||||
detailsKey,
|
||||
disconnect,
|
||||
@@ -44,6 +45,7 @@ import {
|
||||
removeSource,
|
||||
} from "./store";
|
||||
import { tableInput } from "./tableInput";
|
||||
import { dumpScope } from "./dumpTools";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { chooseItem } from "@/workbench/inputDialogs";
|
||||
import { databaseRequest } from "./requests";
|
||||
@@ -100,6 +102,19 @@ async function tablesIn(id: string, schema: string): Promise<DbTableInfo[]> {
|
||||
return l?.status === "ok" ? l.value : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to `target` before a dump / restore dialog opens (a password prompt would replace
|
||||
* the dialog) and reads the server's version; `undefined` when the user cancelled.
|
||||
*/
|
||||
async function serverVersionOf(target: string): Promise<number | null | undefined> {
|
||||
if (!(await connect(target))) return undefined;
|
||||
try {
|
||||
return await withConnection(target, () => call(commands.dbServerVersion(target)));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** DDL of a table / view, or of every table and view of a schema or data source. */
|
||||
async function ddlOf(n: DbNode): Promise<{ id: string; text: string } | null> {
|
||||
const id = nodeTarget(n);
|
||||
@@ -428,6 +443,31 @@ export function registerDatabaseTreeActions(): () => void {
|
||||
openDialog({ kind: "import", id: n.id, schema: n.schema, table: n.table.name });
|
||||
},
|
||||
}),
|
||||
tree({
|
||||
id: "db.dumpWithPgDump",
|
||||
title: "Export with 'pg_dump'…",
|
||||
icon: "export",
|
||||
keywords: ["backup", "dump", "postgresql"],
|
||||
run: async ({ arg }) => {
|
||||
const n = nodeFrom(arg);
|
||||
const scope = n && dumpScope(n, sourceOf(n));
|
||||
const serverVersion = scope && (await serverVersionOf(scope.target));
|
||||
if (scope && serverVersion !== undefined) openDialog({ kind: "dump", ...scope, serverVersion });
|
||||
},
|
||||
}),
|
||||
tree({
|
||||
id: "db.restoreWithPgRestore",
|
||||
title: "Restore with 'pg_restore'/'psql'…",
|
||||
icon: "download",
|
||||
keywords: ["backup", "restore", "load dump", "postgresql"],
|
||||
run: async ({ arg }) => {
|
||||
const n = nodeFrom(arg);
|
||||
const scope = n && dumpScope(n, sourceOf(n));
|
||||
const serverVersion = scope && (await serverVersionOf(scope.target));
|
||||
if (scope && serverVersion !== undefined)
|
||||
openDialog({ kind: "restore", target: scope.target, database: scope.database, serverVersion });
|
||||
},
|
||||
}),
|
||||
tree({
|
||||
id: "db.copyReference",
|
||||
title: "Copy Reference",
|
||||
|
||||
@@ -7,6 +7,8 @@ import { nodeTarget, objectOf, tableOf, type DbNode } from "./nodes";
|
||||
import { SCRIPT_LABELS, canDrop, canModifyTable, canRename, type ScriptKind } from "./objects";
|
||||
import { connectedTargets, openDialog, openSourceDialog, sourceById, useDatabase } from "./store";
|
||||
import { readClipboardPlainText } from "@/platform/clipboard";
|
||||
import { useRemote } from "@/platform/remote";
|
||||
import { dumpScope } from "./dumpTools";
|
||||
|
||||
const act = (
|
||||
action: string,
|
||||
@@ -81,6 +83,20 @@ function sqlScripts(n: DbNode, table: boolean): MenuItem {
|
||||
return { label: "SQL Scripts", submenu: [act("db.ddlToClipboard", n), act("db.ddlToConsole", n), sep, ...scripts] };
|
||||
}
|
||||
|
||||
/** Export with 'pg_dump' / Restore of a PostgreSQL node; the tools run on this machine, so not in remote windows. */
|
||||
function dumpItems(n: DbNode, restore: boolean): MenuItem[] {
|
||||
const source = sourceById(nodeTarget(n));
|
||||
if (useRemote.getState().session || !dumpScope(n, source)) return [];
|
||||
return [
|
||||
act("db.dumpWithPgDump", n),
|
||||
...(restore ? [act("db.restoreWithPgRestore", n, { disabled: !!source?.readOnly })] : []),
|
||||
];
|
||||
}
|
||||
|
||||
function importExport(items: MenuItem[]): MenuItem[] {
|
||||
return items.length ? [{ label: "Import/Export", submenu: items }] : [];
|
||||
}
|
||||
|
||||
/** Context menu of a tree node (`null`: the empty area). */
|
||||
export function nodeMenu(n: DbNode | null): MenuItem[] {
|
||||
if (!n) return [{ label: "New", icon: "plus", submenu: newMenu() }];
|
||||
@@ -118,6 +134,7 @@ export function nodeMenu(n: DbNode | null): MenuItem[] {
|
||||
sep,
|
||||
sqlScripts(n, false),
|
||||
act("db.compareSchemas", n),
|
||||
...importExport(dumpItems(n, true)),
|
||||
sep,
|
||||
act("db.drop", n, { label: "Remove Data Source…" }),
|
||||
];
|
||||
@@ -131,6 +148,7 @@ export function nodeMenu(n: DbNode | null): MenuItem[] {
|
||||
sep,
|
||||
navigation(),
|
||||
sqlScripts(n, false),
|
||||
...importExport(dumpItems(n, true)),
|
||||
];
|
||||
case "schema":
|
||||
case "folder":
|
||||
@@ -144,6 +162,7 @@ export function nodeMenu(n: DbNode | null): MenuItem[] {
|
||||
sep,
|
||||
sqlScripts(n, false),
|
||||
...(n.kind === "schema" ? [act("db.compareSchemas", n)] : []),
|
||||
...importExport(dumpItems(n, true)),
|
||||
...(n.kind === "schema" ? [sep, drop] : []),
|
||||
];
|
||||
case "table":
|
||||
@@ -177,7 +196,13 @@ export function nodeMenu(n: DbNode | null): MenuItem[] {
|
||||
sqlScripts(n, true),
|
||||
...(n.kind === "table" && !view
|
||||
? [
|
||||
{ label: "Import/Export", submenu: [act("db.importData", n, { disabled: driver === "clickhouse" })] },
|
||||
{
|
||||
label: "Import/Export",
|
||||
submenu: [
|
||||
act("db.importData", n, { disabled: driver === "clickhouse" }),
|
||||
...(dumpItems(n, false).length ? [sep, ...dumpItems(n, false)] : []),
|
||||
],
|
||||
},
|
||||
{ label: "Object Actions", submenu: [act("db.truncate", n)] },
|
||||
]
|
||||
: []),
|
||||
|
||||
@@ -277,6 +277,10 @@ describe("catalogs", () => {
|
||||
"Kotlin",
|
||||
"PHP",
|
||||
"SQL",
|
||||
"COPY",
|
||||
"INSERT",
|
||||
"pg_restore",
|
||||
"psql",
|
||||
"JavaScript",
|
||||
"TypeScript",
|
||||
"HTML",
|
||||
|
||||
@@ -2228,5 +2228,69 @@
|
||||
"Values can no longer be expanded after the program resumed": "Values can no longer be expanded after the program resumed",
|
||||
"Each step shows its first {limit} values": "Each step shows its first {limit} values",
|
||||
"Steps": "Steps",
|
||||
"No result": "No result"
|
||||
"No result": "No result",
|
||||
"Add CREATE DATABASE": "Add CREATE DATABASE",
|
||||
"Add DROP before CREATE": "Add DROP before CREATE",
|
||||
"Allowed substitution patterns:": "Allowed substitution patterns:",
|
||||
"COPY": "COPY",
|
||||
"Cannot run {program}": "Cannot run {program}",
|
||||
"Content:": "Content:",
|
||||
"Content": "Content",
|
||||
"Custom archive": "Custom archive",
|
||||
"Data only": "Data only",
|
||||
"Drop objects before recreating them": "Drop objects before recreating them",
|
||||
"Exit code {exit}. The Database tool window's Output tab has the command.": "Exit code {exit}. The Database tool window's Output tab has the command.",
|
||||
"Export with 'pg_dump' ({target})": "Export with 'pg_dump' ({target})",
|
||||
"Export with 'pg_dump'…": "Export with 'pg_dump'…",
|
||||
"Format:": "Format:",
|
||||
"Format": "Format",
|
||||
"INSERT with column names": "INSERT with column names",
|
||||
"INSERT": "INSERT",
|
||||
"Output result to:": "Output result to:",
|
||||
"Output result to": "Output result to",
|
||||
"Parallel jobs:": "Parallel jobs:",
|
||||
"Parallel jobs": "Parallel jobs",
|
||||
"Path to dump:": "Path to dump:",
|
||||
"Path to dump": "Path to dump",
|
||||
"Path to executable:": "Path to executable:",
|
||||
"Path to {program}": "Path to {program}",
|
||||
"Plain SQL": "Plain SQL",
|
||||
"Restore tool": "Restore tool",
|
||||
"Restore with 'pg_restore'/'psql' ({target})": "Restore with 'pg_restore'/'psql' ({target})",
|
||||
"Restore with 'pg_restore'/'psql'…": "Restore with 'pg_restore'/'psql'…",
|
||||
"Schema and data": "Schema and data",
|
||||
"Schema only": "Schema only",
|
||||
"Schemas:": "Schemas:",
|
||||
"Skip ownership (--no-owner)": "Skip ownership (--no-owner)",
|
||||
"Skip privileges (--no-privileges)": "Skip privileges (--no-privileges)",
|
||||
"Statements:": "Statements:",
|
||||
"Stop on the first error": "Stop on the first error",
|
||||
"Tar archive": "Tar archive",
|
||||
"The data source is read-only.": "The data source is read-only.",
|
||||
"The data source is read-only": "The data source is read-only",
|
||||
"Use DROP … IF EXISTS": "Use DROP … IF EXISTS",
|
||||
"Wrap the restore in BEGIN … COMMIT": "Wrap the restore in BEGIN … COMMIT",
|
||||
"pg_restore reads custom, directory and tar archives.": "pg_restore reads custom, directory and tar archives.",
|
||||
"pg_restore": "pg_restore",
|
||||
"psql runs a plain SQL dump (.sql).": "psql runs a plain SQL dump (.sql).",
|
||||
"psql": "psql",
|
||||
"{program} failed: {label}": "{program} failed: {label}",
|
||||
"{program} finished: {label}": "{program} finished: {label}",
|
||||
"{program} was not found. Install the PostgreSQL client tools or choose the executable.": "{program} was not found. Install the PostgreSQL client tools or choose the executable.",
|
||||
"{version}: older than the server (PostgreSQL {server}), which pg_dump refuses. Choose version {server} or newer.": "{version}: older than the server (PostgreSQL {server}), which pg_dump refuses. Choose version {server} or newer.",
|
||||
"{version}: newer than the server (PostgreSQL {server}). What it writes may not load into PostgreSQL {server}. Install the PostgreSQL {server} client tools to restore into this server.": "{version}: newer than the server (PostgreSQL {server}). What it writes may not load into PostgreSQL {server}. Install the PostgreSQL {server} client tools to restore into this server.",
|
||||
"Copy Output": "Copy Output",
|
||||
"Running {tool}… {took}": "Running {tool}… {took}",
|
||||
"Finished in {took}": "Finished in {took}",
|
||||
"Stopped after {took}": "Stopped after {took}",
|
||||
"Failed with exit code {exit} after {took}": "Failed with exit code {exit} after {took}",
|
||||
"{program} finished with exit code {exit} in {took}": "{program} finished with exit code {exit} in {took}",
|
||||
"{route} in {took}": "{route} in {took}",
|
||||
"{program} stopped after {took}": "{program} stopped after {took}",
|
||||
"All databases": "All databases",
|
||||
"Choose File…": "Choose File…",
|
||||
"Choose Directory Archive…": "Choose Directory Archive…",
|
||||
"This is a MySQL dump: restore it into a MySQL data source.": "This is a MySQL dump: restore it into a MySQL data source.",
|
||||
"This is a pg_dump archive: restore it with pg_restore.": "This is a pg_dump archive: restore it with pg_restore.",
|
||||
"This is a plain SQL dump: restore it with psql.": "This is a plain SQL dump: restore it with psql."
|
||||
}
|
||||
|
||||
@@ -8615,5 +8615,67 @@
|
||||
"Values can no longer be expanded after the program resumed": "程序恢复运行后,值无法再展开",
|
||||
"Each step shows its first {limit} values": "每个步骤仅显示前 {limit} 个值",
|
||||
"Steps": "步骤",
|
||||
"No result": "无结果"
|
||||
"No result": "无结果",
|
||||
"Add CREATE DATABASE": "添加 CREATE DATABASE",
|
||||
"Add DROP before CREATE": "在 CREATE 之前添加 DROP",
|
||||
"Allowed substitution patterns:": "可用的替换模式:",
|
||||
"COPY": "COPY",
|
||||
"Cannot run {program}": "无法运行 {program}",
|
||||
"Content:": "内容:",
|
||||
"Content": "内容",
|
||||
"Custom archive": "自定义归档",
|
||||
"Data only": "仅数据",
|
||||
"Drop objects before recreating them": "重新创建前删除对象",
|
||||
"Exit code {exit}. The Database tool window's Output tab has the command.": "退出代码 {exit}。命令见“数据库”工具窗口的“输出”标签页。",
|
||||
"Export with 'pg_dump' ({target})": "使用 'pg_dump' 导出({target})",
|
||||
"Export with 'pg_dump'…": "使用 'pg_dump' 导出…",
|
||||
"Format:": "格式:",
|
||||
"Format": "格式",
|
||||
"INSERT with column names": "带列名的 INSERT",
|
||||
"INSERT": "INSERT",
|
||||
"Output result to:": "输出结果到:",
|
||||
"Output result to": "输出结果到",
|
||||
"Parallel jobs:": "并行任务数:",
|
||||
"Parallel jobs": "并行任务数",
|
||||
"Path to dump:": "转储文件路径:",
|
||||
"Path to dump": "转储文件路径",
|
||||
"Path to executable:": "可执行文件路径:",
|
||||
"Path to {program}": "{program} 的路径",
|
||||
"Plain SQL": "纯 SQL",
|
||||
"Restore tool": "恢复工具",
|
||||
"Restore with 'pg_restore'/'psql' ({target})": "使用 'pg_restore'/'psql' 恢复({target})",
|
||||
"Restore with 'pg_restore'/'psql'…": "使用 'pg_restore'/'psql' 恢复…",
|
||||
"Schema and data": "结构和数据",
|
||||
"Schema only": "仅结构",
|
||||
"Schemas:": "模式:",
|
||||
"Skip ownership (--no-owner)": "跳过所有者(--no-owner)",
|
||||
"Skip privileges (--no-privileges)": "跳过权限(--no-privileges)",
|
||||
"Statements:": "语句:",
|
||||
"Stop on the first error": "遇到第一个错误时停止",
|
||||
"Tar archive": "Tar 归档",
|
||||
"The data source is read-only.": "该数据源为只读。",
|
||||
"The data source is read-only": "该数据源为只读",
|
||||
"Use DROP … IF EXISTS": "使用 DROP … IF EXISTS",
|
||||
"Wrap the restore in BEGIN … COMMIT": "将恢复包装在 BEGIN … COMMIT 中",
|
||||
"pg_restore reads custom, directory and tar archives.": "pg_restore 读取自定义、目录和 tar 归档。",
|
||||
"pg_restore": "pg_restore",
|
||||
"psql runs a plain SQL dump (.sql).": "psql 执行纯 SQL 转储(.sql)。",
|
||||
"psql": "psql",
|
||||
"{program} failed: {label}": "{program} 失败:{label}",
|
||||
"{program} finished: {label}": "{program} 已完成:{label}",
|
||||
"{program} was not found. Install the PostgreSQL client tools or choose the executable.": "未找到 {program}。请安装 PostgreSQL 客户端工具或选择可执行文件。",
|
||||
"{version}: older than the server (PostgreSQL {server}), which pg_dump refuses. Choose version {server} or newer.": "{version}:比服务器(PostgreSQL {server})旧,pg_dump 会拒绝运行。请选择 {server} 或更高版本。",
|
||||
"{version}: newer than the server (PostgreSQL {server}). What it writes may not load into PostgreSQL {server}. Install the PostgreSQL {server} client tools to restore into this server.": "{version}:比服务器(PostgreSQL {server})新。它写出的内容可能无法载入 PostgreSQL {server}。如需恢复到此服务器,请安装 PostgreSQL {server} 客户端工具。",
|
||||
"Copy Output": "复制输出",
|
||||
"Running {tool}… {took}": "正在运行 {tool}… {took}",
|
||||
"Finished in {took}": "已完成,用时 {took}",
|
||||
"Stopped after {took}": "已停止,运行了 {took}",
|
||||
"Failed with exit code {exit} after {took}": "运行 {took} 后失败,退出代码 {exit}",
|
||||
"{program} finished with exit code {exit} in {took}": "{program} 已结束,退出代码 {exit},用时 {took}",
|
||||
"{route} in {took}": "{route},用时 {took}",
|
||||
"{program} stopped after {took}": "{program} 运行 {took} 后已停止",
|
||||
"Choose Directory Archive…": "选择目录归档…",
|
||||
"This is a MySQL dump: restore it into a MySQL data source.": "这是 MySQL 转储:请恢复到 MySQL 数据源。",
|
||||
"This is a pg_dump archive: restore it with pg_restore.": "这是 pg_dump 归档:请使用 pg_restore 恢复。",
|
||||
"This is a plain SQL dump: restore it with psql.": "这是纯 SQL 转储:请使用 psql 恢复。"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user