use crate::command::get::get_property;
use crate::command::install::install_extension;
use crate::command::start::start_postgres;
use crate::command::stop::stop_postgres;
use crate::manifest::{get_package_manifest, pg_config_and_version};
use crate::profile::CargoProfile;
use crate::CommandExecute;
use eyre::eyre;
use owo_colors::OwoColorize;
use pgx_pg_config::{createdb, PgConfig, Pgx};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::Command;
#[derive(clap::Args, Debug)]
#[clap(author)]
pub(crate) struct Run {
#[clap(env = "PG_VERSION")]
pg_version: Option<String>,
dbname: Option<String>,
#[clap(long, short)]
package: Option<String>,
#[clap(long)]
manifest_path: Option<String>,
#[clap(long, short)]
release: bool,
#[clap(long)]
profile: Option<String>,
#[clap(flatten)]
features: clap_cargo::Features,
#[clap(from_global, action = ArgAction::Count)]
verbose: u8,
#[clap(env = "PGX_PGCLI", long)]
pgcli: bool,
}
impl CommandExecute for Run {
#[tracing::instrument(level = "error", skip(self))]
fn execute(mut self) -> eyre::Result<()> {
let pgx = Pgx::from_config()?;
let (package_manifest, package_manifest_path) = get_package_manifest(
&self.features,
self.package.as_ref(),
self.manifest_path.as_ref(),
)?;
let (pg_config, _pg_version) = pg_config_and_version(
&pgx,
&package_manifest,
self.pg_version.clone(),
Some(&mut self.features),
true,
)?;
let dbname = match self.dbname {
Some(dbname) => dbname,
None => get_property(&package_manifest_path, "extname")?
.ok_or(eyre!("could not determine extension name"))?,
};
let profile = CargoProfile::from_flags(
self.profile.as_deref(),
self.release.then_some(CargoProfile::Release).unwrap_or(CargoProfile::Dev),
)?;
run(
pg_config,
self.manifest_path.as_ref(),
self.package.as_ref(),
package_manifest_path,
&dbname,
&profile,
self.pgcli,
&self.features,
)
}
}
#[tracing::instrument(level = "error", skip_all, fields(
pg_version = %pg_config.version()?,
dbname,
profile = ?profile,
))]
pub(crate) fn run(
pg_config: &PgConfig,
user_manifest_path: Option<impl AsRef<Path>>,
user_package: Option<&String>,
package_manifest_path: impl AsRef<Path>,
dbname: &str,
profile: &CargoProfile,
pgcli: bool,
features: &clap_cargo::Features,
) -> eyre::Result<()> {
stop_postgres(pg_config)?;
install_extension(
user_manifest_path,
user_package,
package_manifest_path,
pg_config,
profile,
false,
None,
features,
)?;
start_postgres(pg_config)?;
if !createdb(pg_config, dbname, false, true)? {
println!("{} existing database {}", " Re-using".bold().cyan(), dbname);
}
exec_psql(pg_config, dbname, pgcli)
}
pub(crate) fn exec_psql(pg_config: &PgConfig, dbname: &str, pgcli: bool) -> eyre::Result<()> {
let mut command = Command::new(match pgcli {
false => pg_config.psql_path()?.into_os_string(),
true => "pgcli".to_string().into(),
});
command
.env_remove("PGDATABASE")
.env_remove("PGHOST")
.env_remove("PGPORT")
.env_remove("PGUSER")
.arg("-h")
.arg(pg_config.host())
.arg("-p")
.arg(pg_config.port()?.to_string())
.arg(dbname);
panic!("{}", command.exec());
}