Lines
98.15 %
Functions
50 %
Branches
100 %
//! The global directory's side of a fleet run: the inventory and each tenant's verdict.
use super::Outcome;
use crate::db::DBError;
use sqlx::types::Uuid;
use sqlx::types::chrono::{DateTime, Utc};
/// Writes one tenant's verdict to the global directory.
///
/// Errors are returned, never swallowed: a quarantine that fails to stick
/// leaves a tenant on the wrong schema still being served, which is the exact
/// state this module exists to prevent.
pub(super) async fn publish(
user_id: Uuid,
outcome: &Outcome,
run_started: DateTime<Utc>,
) -> Result<(), DBError> {
match outcome {
Outcome::Migrated | Outcome::JobFailed(_) => clear_quarantine(user_id, run_started).await,
Outcome::Quarantined(reason) => quarantine(user_id, reason, run_started).await,
// Nothing was learned about this tenant, so nothing is claimed about it.
Outcome::Unresolved => Ok(()),
}
/// Reads the wall clock of the database that records quarantines.
pub(super) async fn database_now() -> Result<DateTime<Utc>, DBError> {
let mut conn = crate::db::get_connection().await?;
sqlx::query_scalar!(r#"SELECT now() as "now!""#)
.fetch_one(&mut *conn)
.await
.map_err(DBError::Sqlx)
pub(crate) async fn inventory(admin_user_id: Uuid) -> Result<Vec<(Uuid, String)>, DBError> {
let rows = sqlx::query_file!(
"../server/sql/select/system/tenant_inventory.sql",
admin_user_id
)
.fetch_all(&mut *conn)
.map_err(DBError::Sqlx)?;
Ok(rows
.into_iter()
.map(|row| (row.id, row.database_name))
.collect())
async fn quarantine(
reason: &str,
let recorded = sqlx::query_file!(
"../server/sql/update/users/quarantine.sql",
user_id,
reason,
run_started
.execute(&mut *conn)
if recorded.rows_affected() == 0 {
tracing::info!("{user_id} already carries a verdict newer than this run");
Ok(())
/// Lifts a quarantine, but only one that predates this run.
/// The fence matters when two releases' migrators overlap: without it, an older
/// migrator finishing successfully against its own (older) migration set would
/// clear a quarantine a newer migrator had just written for failing on a newer
/// one — and the newest application would then serve a tenant known not to have
/// the schema it needs.
async fn clear_quarantine(user_id: Uuid, run_started: DateTime<Utc>) -> Result<(), DBError> {
let cleared = sqlx::query_file!(
"../server/sql/update/users/clear_quarantine.sql",
// A tenant that was never quarantined still matches the fence, so this is
// not the ordinary case — it means someone newer has already ruled.
if cleared.rows_affected() == 0 {
tracing::info!("{user_id} was not cleared: its quarantine is newer than this run");