Files
tessera/crates/tessera-core/src/restore.rs
Adnan Zahir 43709aeeab feat: say why a restore failed, and add the user_schemas reset mode
- the agent reports the client's primary ERROR/FATAL line with a failed restore step; the hub keeps it
  as failure_reason (administrators only), logs it and audits restore.failed. psql runs with
  VERBOSITY=terse and DETAIL/CONTEXT/statement text is dropped so row data never travels
- reset mode user_schemas empties every non-system schema in the restore transaction, so multi-schema
  databases restore without DROP DATABASE; proven against a real PostgreSQL server

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 12:29:48 +07:00

134 lines
3.7 KiB
Rust

use crate::job::CaptureFormat;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Engine {
Postgres,
Mysql,
}
impl CaptureFormat {
pub fn engine(self) -> Option<Engine> {
match self {
Self::PostgresCustom | Self::PostgresSql => Some(Engine::Postgres),
Self::MysqlSql => Some(Engine::Mysql),
Self::Filesystem => None,
}
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DatabaseIdentity {
pub instance: String,
pub database: String,
}
impl DatabaseIdentity {
pub fn valid(&self, engine: Engine) -> bool {
let instance_valid = match engine {
Engine::Postgres => self
.instance
.parse::<u64>()
.is_ok_and(|id| id.to_string() == self.instance),
Engine::Mysql => {
Uuid::parse_str(&self.instance).is_ok_and(|id| id.to_string() == self.instance)
}
};
instance_valid
&& (1..=255).contains(&self.database.len())
&& !self.database.chars().any(char::is_control)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RestoreTarget {
pub id: Uuid,
pub application_id: Uuid,
pub outpost_id: Uuid,
pub name: String,
pub engine: Engine,
pub identity: DatabaseIdentity,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RestoreRequest {
pub snapshot_id: Uuid,
pub target_id: Uuid,
pub confirmation: String,
}
impl RestoreRequest {
pub fn confirmation(snapshot: Uuid, target: Uuid) -> String {
format!("RESTORE {snapshot} TO {target}")
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RestoreJob {
pub id: Uuid,
pub snapshot_id: Uuid,
pub target: RestoreTarget,
pub actor: String,
pub status: String,
pub phase: String,
pub safety_snapshot_id: Option<Uuid>,
pub created_at: i64,
pub updated_at: i64,
/// Why the job failed, as the agent reported it (cleaned). Only administrators ever see it.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub failure_reason: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RestoreArtifact {
pub format: CaptureFormat,
pub identity: DatabaseIdentity,
pub sha256: String,
}
#[derive(Deserialize, Serialize)]
pub struct RestoreLease {
pub job: RestoreJob,
pub token: Uuid,
pub artifact: RestoreArtifact,
}
/// Header on a restore report that carries the reason for a failure.
pub const FAILURE_REASON_HEADER: &str = "x-tessera-failure-reason";
const FAILURE_REASON_MAX: usize = 300;
/// Make a failure reason safe to store, log and send in a header: printable ASCII on one line, at most 300
/// bytes. Anything else becomes a space (control characters) or `?` (non-ASCII). Empty input gives `None`.
pub fn clean_failure_reason(text: &str) -> Option<String> {
let mapped: String = text
.chars()
.map(|c| match c {
c if c.is_ascii_graphic() => c,
c if c.is_ascii() => ' ',
_ => '?',
})
.collect();
let line = mapped.split_whitespace().collect::<Vec<_>>().join(" ");
let line: String = line.chars().take(FAILURE_REASON_MAX).collect();
let line = line.trim_end().to_string();
(!line.is_empty()).then_some(line)
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RestoreProgress {
Heartbeat,
Checking,
Safety,
InvalidPayload,
WrongTarget,
SafetyFailed,
Applied,
ApplyFailed,
}