Scaffold Rust workspace, React canvas, vault engine, hub API, providers, agent wormhole path, and Docker compose per SPEC.
64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
use super::{BackupProvider, CaptureResult};
|
|
use anyhow::{Context, Result};
|
|
use async_trait::async_trait;
|
|
use serde::Deserialize;
|
|
use serde_json::json;
|
|
use std::process::Stdio;
|
|
use tessera_core::config::ProviderKind;
|
|
use tokio::process::Command;
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct PostgresConfig {
|
|
pub connection_url: String,
|
|
#[serde(default = "default_format")]
|
|
pub format: String,
|
|
}
|
|
|
|
fn default_format() -> String {
|
|
"custom".into()
|
|
}
|
|
|
|
pub struct PostgresProvider {
|
|
config: PostgresConfig,
|
|
}
|
|
|
|
impl PostgresProvider {
|
|
pub fn from_config(config: &serde_json::Value) -> Result<Self> {
|
|
let config: PostgresConfig = serde_json::from_value(config.clone())?;
|
|
Ok(Self { config })
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl BackupProvider for PostgresProvider {
|
|
fn kind(&self) -> ProviderKind {
|
|
ProviderKind::Postgres
|
|
}
|
|
|
|
async fn capture(&self) -> Result<CaptureResult> {
|
|
let mut cmd = Command::new("pg_dump");
|
|
cmd.arg(&self.config.connection_url);
|
|
match self.config.format.as_str() {
|
|
"custom" => {
|
|
cmd.arg("-Fc");
|
|
}
|
|
"plain" => {}
|
|
other => anyhow::bail!("unknown pg format: {other}"),
|
|
}
|
|
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
|
|
let output = cmd.output().await.context("pg_dump failed to start")?;
|
|
if !output.status.success() {
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
anyhow::bail!("pg_dump failed: {stderr}");
|
|
}
|
|
Ok(CaptureResult {
|
|
payload: output.stdout,
|
|
labels: vec!["postgres".into()],
|
|
restore_hints: json!({
|
|
"format": self.config.format,
|
|
"restore_command_hint": "pg_restore -d $DATABASE_URL dump.file"
|
|
}),
|
|
})
|
|
}
|
|
}
|