feat: add managed database capture and operator restore
PostgreSQL and MySQL backups run through paired outposts, with operator sign-in, off-site copies, and legacy import. Retention stays on each orbit, so the unused retention console is removed. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
mod jobs;
|
||||
mod restore;
|
||||
pub(crate) use restore::restore_target_mismatch;
|
||||
|
||||
use anyhow::Result;
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tessera_core::decay::RetentionPolicy;
|
||||
use tessera_core::{decay::RetentionPolicy, OutpostSummary};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -10,7 +14,7 @@ pub struct Db {
|
||||
conn: Arc<Mutex<Connection>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, utoipa::ToSchema)]
|
||||
#[derive(Clone)]
|
||||
pub struct OrbitRow {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
@@ -18,7 +22,6 @@ pub struct OrbitRow {
|
||||
pub provider_config: String,
|
||||
pub schedule: Option<String>,
|
||||
pub decay_preset: String,
|
||||
#[serde(default)]
|
||||
pub retention: Option<RetentionPolicy>,
|
||||
pub dry_run: bool,
|
||||
pub last_status: String,
|
||||
@@ -91,12 +94,24 @@ impl Db {
|
||||
"INTEGER NOT NULL DEFAULT 0",
|
||||
)?;
|
||||
ensure_column(&conn, "orbits", "retention", "TEXT")?;
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO outposts(id, name, pairing_state)
|
||||
SELECT outpost_id, outpost_name, 'active' FROM wormhole_tokens WHERE state = 'active'",
|
||||
[],
|
||||
)?;
|
||||
jobs::migrate(&conn)?;
|
||||
restore::migrate(&conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn list_orbits(&self) -> Result<Vec<OrbitRow>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT id, name, provider, provider_config, schedule, decay_preset, dry_run, last_status, retention FROM orbits ORDER BY name")?;
|
||||
let mut stmt = conn.prepare("SELECT o.id, o.name, o.provider, o.provider_config, o.schedule, o.decay_preset, o.dry_run,
|
||||
CASE WHEN p.application_id IS NULL THEN o.last_status ELSE COALESCE((
|
||||
SELECT j.status FROM capture_jobs j WHERE j.application_id = p.application_id
|
||||
ORDER BY j.created_at DESC, j.rowid DESC LIMIT 1
|
||||
), 'idle') END, o.retention
|
||||
FROM orbits o LEFT JOIN capture_profiles p ON p.orbit_id = o.id ORDER BY o.name")?;
|
||||
let rows = stmt.query_map([], |r| {
|
||||
Ok(OrbitRow {
|
||||
id: Uuid::parse_str(&r.get::<_, String>(0)?).unwrap_or_else(|_| Uuid::new_v4()),
|
||||
@@ -233,26 +248,48 @@ impl Db {
|
||||
outpost_name: &str,
|
||||
now: &str,
|
||||
) -> Result<Option<(String, String)>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let changed = conn.execute(
|
||||
"UPDATE wormhole_tokens SET state = 'active', outpost_name = ?2 WHERE code = ?1 AND state = 'code_shown' AND expires_at > ?3",
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction()?;
|
||||
let claimed: Option<(String, String)> = tx.query_row(
|
||||
"UPDATE wormhole_tokens SET state = 'active', outpost_name = ?2
|
||||
WHERE token = (SELECT token FROM wormhole_tokens
|
||||
WHERE code = ?1 AND state = 'code_shown' AND expires_at > ?3
|
||||
ORDER BY expires_at DESC LIMIT 1)
|
||||
RETURNING token, outpost_id",
|
||||
params![code, outpost_name, now],
|
||||
)?;
|
||||
if changed != 1 {
|
||||
return Ok(None);
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
).optional()?;
|
||||
if let Some((_, outpost)) = &claimed {
|
||||
tx.execute(
|
||||
"INSERT INTO outposts(id, name, last_seen, pairing_state) VALUES (?1, ?2, ?3, 'active')
|
||||
ON CONFLICT(id) DO UPDATE SET name = excluded.name, last_seen = excluded.last_seen, pairing_state = 'active'",
|
||||
params![outpost, outpost_name, now],
|
||||
)?;
|
||||
}
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT token, outpost_id FROM wormhole_tokens WHERE code = ?1 AND state = 'active'",
|
||||
)?;
|
||||
let mut rows = stmt.query(params![code])?;
|
||||
let Some(row) = rows.next()? else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some((row.get(0)?, row.get(1)?)))
|
||||
tx.commit()?;
|
||||
Ok(claimed)
|
||||
}
|
||||
|
||||
pub fn upload_token_active(&self, token: &str) -> Result<bool> {
|
||||
Ok(self.upload_token_outpost(token)?.is_some())
|
||||
pub fn list_outposts(&self) -> Result<Vec<OutpostSummary>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT id, name, last_seen, pairing_state FROM outposts ORDER BY name, id")?;
|
||||
let rows = stmt.query_map([], |row| {
|
||||
let id = Uuid::parse_str(&row.get::<_, String>(0)?).map_err(|err| {
|
||||
rusqlite::Error::FromSqlConversionFailure(0, rusqlite::types::Type::Text, Box::new(err))
|
||||
})?;
|
||||
Ok(OutpostSummary { id, name: row.get(1)?, last_seen: row.get(2)?, pairing_state: row.get(3)? })
|
||||
})?;
|
||||
Ok(rows.collect::<rusqlite::Result<_>>()?)
|
||||
}
|
||||
|
||||
pub fn touch_outpost(&self, id: Uuid) -> Result<()> {
|
||||
let now = chrono::Utc::now();
|
||||
let recent = (now - chrono::Duration::seconds(30)).to_rfc3339();
|
||||
self.conn.lock().unwrap().execute(
|
||||
"UPDATE outposts SET last_seen = ?2 WHERE id = ?1 AND (last_seen IS NULL OR last_seen < ?3)",
|
||||
params![id.to_string(), now.to_rfc3339(), recent],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn upload_token_outpost(&self, token: &str) -> Result<Option<String>> {
|
||||
|
||||
Reference in New Issue
Block a user