feat: add configurable GFS retention per orbit
This commit is contained in:
@@ -2,6 +2,7 @@ use anyhow::Result;
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tessera_core::decay::RetentionPolicy;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -17,6 +18,8 @@ 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,
|
||||
}
|
||||
@@ -81,13 +84,19 @@ impl Db {
|
||||
);
|
||||
"#,
|
||||
)?;
|
||||
ensure_column(&conn, "upload_sessions", "sealed", "INTEGER NOT NULL DEFAULT 0")?;
|
||||
ensure_column(
|
||||
&conn,
|
||||
"upload_sessions",
|
||||
"sealed",
|
||||
"INTEGER NOT NULL DEFAULT 0",
|
||||
)?;
|
||||
ensure_column(&conn, "orbits", "retention", "TEXT")?;
|
||||
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 FROM orbits ORDER BY name")?;
|
||||
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 rows = stmt.query_map([], |r| {
|
||||
Ok(OrbitRow {
|
||||
id: Uuid::parse_str(&r.get::<_, String>(0)?).unwrap_or_else(|_| Uuid::new_v4()),
|
||||
@@ -96,21 +105,34 @@ impl Db {
|
||||
provider_config: r.get(3)?,
|
||||
schedule: r.get(4)?,
|
||||
decay_preset: r.get(5)?,
|
||||
retention: r
|
||||
.get::<_, Option<String>>(8)?
|
||||
.map(|json| {
|
||||
serde_json::from_str(&json).map_err(|error| {
|
||||
rusqlite::Error::FromSqlConversionFailure(
|
||||
8,
|
||||
rusqlite::types::Type::Text,
|
||||
Box::new(error),
|
||||
)
|
||||
})
|
||||
})
|
||||
.transpose()?,
|
||||
dry_run: r.get::<_, i64>(6)? != 0,
|
||||
last_status: r.get(7)?,
|
||||
})
|
||||
})?;
|
||||
Ok(rows.filter_map(|r| r.ok()).collect())
|
||||
Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
|
||||
}
|
||||
|
||||
pub async fn upsert_orbit(&self, row: &OrbitRow) -> Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
r#"INSERT INTO orbits (id, name, provider, provider_config, schedule, decay_preset, dry_run, last_status)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
|
||||
r#"INSERT INTO orbits (id, name, provider, provider_config, schedule, decay_preset, dry_run, last_status, retention)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name=excluded.name, provider=excluded.provider, provider_config=excluded.provider_config,
|
||||
schedule=excluded.schedule, decay_preset=excluded.decay_preset, dry_run=excluded.dry_run"#,
|
||||
schedule=excluded.schedule, decay_preset=excluded.decay_preset, dry_run=excluded.dry_run,
|
||||
retention=excluded.retention"#,
|
||||
params![
|
||||
row.id.to_string(),
|
||||
row.name,
|
||||
@@ -120,11 +142,44 @@ impl Db {
|
||||
row.decay_preset,
|
||||
row.dry_run as i64,
|
||||
row.last_status,
|
||||
row.retention.as_ref().map(serde_json::to_string).transpose()?,
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_orbit_retention(
|
||||
&self,
|
||||
id: Uuid,
|
||||
preset: &str,
|
||||
policy: Option<&RetentionPolicy>,
|
||||
) -> Result<bool> {
|
||||
let json = policy.map(serde_json::to_string).transpose()?;
|
||||
let changed = self.conn.lock().unwrap().execute(
|
||||
"UPDATE orbits SET decay_preset = ?1, retention = ?2 WHERE id = ?3",
|
||||
params![preset, json, id.to_string()],
|
||||
)?;
|
||||
Ok(changed > 0)
|
||||
}
|
||||
|
||||
pub fn orbit_retention(&self, id: Uuid) -> Result<(String, Option<RetentionPolicy>)> {
|
||||
let row: Option<(String, Option<String>)> = self
|
||||
.conn
|
||||
.lock()
|
||||
.unwrap()
|
||||
.query_row(
|
||||
"SELECT decay_preset, retention FROM orbits WHERE id = ?1",
|
||||
[id.to_string()],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let (preset, json) = row.unwrap_or_else(|| ("week-of-dailies".into(), None));
|
||||
Ok((
|
||||
preset,
|
||||
json.as_deref().map(serde_json::from_str).transpose()?,
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn set_orbit_status(&self, id: Uuid, status: &str) -> Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
@@ -308,3 +363,60 @@ fn ensure_column(conn: &Connection, table: &str, column: &str, decl: &str) -> Re
|
||||
conn.execute(&format!("ALTER TABLE {table} ADD COLUMN {column} {decl}"), [])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn existing_orbit_can_switch_between_preset_and_custom_retention() {
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
let id = Uuid::new_v4();
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE orbits (
|
||||
id TEXT PRIMARY KEY, name TEXT, provider TEXT, provider_config TEXT,
|
||||
schedule TEXT, decay_preset TEXT, dry_run INTEGER, last_status TEXT
|
||||
)",
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO orbits VALUES (?1, 'archive', 'filesystem', '{}', NULL,
|
||||
'month-of-weeklies', 0, 'completed')",
|
||||
[id.to_string()],
|
||||
)
|
||||
.unwrap();
|
||||
let db = Db {
|
||||
conn: Arc::new(Mutex::new(conn)),
|
||||
};
|
||||
db.migrate().await.unwrap();
|
||||
assert_eq!(
|
||||
db.orbit_retention(id).unwrap(),
|
||||
("month-of-weeklies".into(), None)
|
||||
);
|
||||
|
||||
let policy = RetentionPolicy {
|
||||
keep_daily: 7,
|
||||
keep_weekly: 2,
|
||||
keep_monthly: 6,
|
||||
keep_yearly: 3,
|
||||
};
|
||||
assert!(db
|
||||
.set_orbit_retention(id, "week-of-dailies", Some(&policy))
|
||||
.unwrap());
|
||||
let orbit = db.list_orbits().await.unwrap().remove(0);
|
||||
assert_eq!(orbit.retention, Some(policy));
|
||||
assert_eq!(orbit.last_status, "completed");
|
||||
assert_eq!(db.orbit_retention(id).unwrap().1, Some(policy));
|
||||
|
||||
assert!(db
|
||||
.set_orbit_retention(id, "year-of-monthlies", None)
|
||||
.unwrap());
|
||||
assert_eq!(
|
||||
db.orbit_retention(id).unwrap(),
|
||||
("year-of-monthlies".into(), None)
|
||||
);
|
||||
assert!(!db
|
||||
.set_orbit_retention(Uuid::new_v4(), "year-of-monthlies", None)
|
||||
.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user