Skip to content

Commit

Permalink
Use pgdsn for 6.x psql connections (#1374)
Browse files Browse the repository at this point in the history
Fix \psql and pgaddr commands to work with 6.x servers.
  • Loading branch information
mmastrac authored Oct 3, 2024
1 parent 74b0030 commit 4e4cd39
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 49 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/commands/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::connect::Connection;
use edgedb_tokio::server_params::PostgresAddress;
use edgedb_tokio::server_params::{PostgresAddress, PostgresDsn};

use crate::analyze;
use crate::commands::parser::{Common, DatabaseCmd, DescribeCmd, ListCmd};
Expand Down Expand Up @@ -67,10 +67,17 @@ pub async fn common(
}
Pgaddr => match cli.get_server_param::<PostgresAddress>() {
Some(addr) => {
// < 6.x
println!("{}", serde_json::to_string_pretty(addr)?);
}
None => {
print::error("pgaddr requires EdgeDB to run in DEV mode");
// >= 6.x
match cli.get_server_param::<PostgresDsn>() {
Some(addr) => {
println!("{}", addr.0);
}
None => print::error("pgaddr requires EdgeDB to run in DEV mode"),
}
}
},
Psql => {
Expand Down
91 changes: 49 additions & 42 deletions src/commands/psql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,70 @@ use std::process::Command;

use crate::connect::Connection;
use anyhow::Context;
use edgedb_tokio::server_params::PostgresAddress;
use edgedb_tokio::server_params::{PostgresAddress, PostgresDsn};

use crate::commands::Options;
use crate::interrupt;
use crate::print;

pub async fn psql<'x>(cli: &mut Connection, _options: &Options) -> Result<(), anyhow::Error> {
match cli.get_server_param::<PostgresAddress>() {
Some(addr) => {
let mut cmd = Command::new("psql");
let path = if cfg!(feature = "dev_mode") {
use std::iter;
use std::path::{Path, PathBuf};
let mut cmd = Command::new("psql");
let path = if cfg!(feature = "dev_mode") {
use std::iter;
use std::path::{Path, PathBuf};

if let Some(dir) = option_env!("PSQL_DEFAULT_PATH") {
let psql_path = Path::new(dir).join("psql");
if !psql_path.exists() {
eprintln!("WARNING: {} does not exist", psql_path.display());
}
let npath = if let Some(path) = env::var_os("PATH") {
env::join_paths(
iter::once(PathBuf::from(dir)).chain(env::split_paths(&path)),
)
.unwrap_or_else(|e| {
eprintln!("PSQL_DEFAULT_PATH error: {}", e);
path
})
} else {
dir.into()
};
Some(npath)
} else {
env::var_os("PATH")
}
if let Some(dir) = option_env!("PSQL_DEFAULT_PATH") {
let psql_path = Path::new(dir).join("psql");
if !psql_path.exists() {
eprintln!("WARNING: {} does not exist", psql_path.display());
}
let npath = if let Some(path) = env::var_os("PATH") {
env::join_paths(iter::once(PathBuf::from(dir)).chain(env::split_paths(&path)))
.unwrap_or_else(|e| {
eprintln!("PSQL_DEFAULT_PATH error: {}", e);
path
})
} else {
env::var_os("PATH")
dir.into()
};
Some(npath)
} else {
env::var_os("PATH")
}
} else {
env::var_os("PATH")
};

match cli.get_server_param::<PostgresAddress>() {
Some(addr) => {
cmd.arg("-h").arg(&addr.host);
cmd.arg("-U").arg(&addr.user);
cmd.arg("-p").arg(addr.port.to_string());
cmd.arg("-d").arg(&addr.database);
if let Some(path) = path.as_ref() {
cmd.env("PATH", path);
}
None => match cli.get_server_param::<PostgresDsn>() {
Some(addr) => {
cmd.arg("--");
cmd.arg(&addr.0);
}
None => {
print::error("EdgeDB must be run in DEV mode to use psql.");
return Ok(());
}
},
}

let _trap = interrupt::Trap::new(&[interrupt::Signal::Interrupt]);
cmd.status().with_context(|| {
format!(
"Error running {:?} (path: {:?})",
cmd,
path.unwrap_or_else(OsString::new)
)
})?;
}
None => {
print::error("EdgeDB must be run in DEV mode to use psql.");
}
if let Some(path) = path.as_ref() {
cmd.env("PATH", path);
}

let _trap = interrupt::Trap::new(&[interrupt::Signal::Interrupt]);
cmd.status().with_context(|| {
format!(
"Error running {:?} (path: {:?})",
cmd,
path.unwrap_or_else(OsString::new)
)
})?;
Ok(())
}

0 comments on commit 4e4cd39

Please sign in to comment.