1
//! The global directory's side of a fleet run: the inventory and each tenant's verdict.
2

            
3
use super::Outcome;
4
use crate::db::DBError;
5
use sqlx::types::Uuid;
6
use sqlx::types::chrono::{DateTime, Utc};
7

            
8
/// Writes one tenant's verdict to the global directory.
9
///
10
/// Errors are returned, never swallowed: a quarantine that fails to stick
11
/// leaves a tenant on the wrong schema still being served, which is the exact
12
/// state this module exists to prevent.
13
8820
pub(super) async fn publish(
14
8820
    user_id: Uuid,
15
8820
    outcome: &Outcome,
16
8820
    run_started: DateTime<Utc>,
17
8820
) -> Result<(), DBError> {
18
8820
    match outcome {
19
7380
        Outcome::Migrated | Outcome::JobFailed(_) => clear_quarantine(user_id, run_started).await,
20
1440
        Outcome::Quarantined(reason) => quarantine(user_id, reason, run_started).await,
21
        // Nothing was learned about this tenant, so nothing is claimed about it.
22
        Outcome::Unresolved => Ok(()),
23
    }
24
8820
}
25

            
26
/// Reads the wall clock of the database that records quarantines.
27
///
28
1800
pub(super) async fn database_now() -> Result<DateTime<Utc>, DBError> {
29
1800
    let mut conn = crate::db::get_connection().await?;
30

            
31
1800
    sqlx::query_scalar!(r#"SELECT now() as "now!""#)
32
1800
        .fetch_one(&mut *conn)
33
1800
        .await
34
1800
        .map_err(DBError::Sqlx)
35
1800
}
36

            
37
4860
pub(crate) async fn inventory(admin_user_id: Uuid) -> Result<Vec<(Uuid, String)>, DBError> {
38
4860
    let mut conn = crate::db::get_connection().await?;
39

            
40
4860
    let rows = sqlx::query_file!(
41
4860
        "../server/sql/select/system/tenant_inventory.sql",
42
        admin_user_id
43
    )
44
4860
    .fetch_all(&mut *conn)
45
4860
    .await
46
4860
    .map_err(DBError::Sqlx)?;
47

            
48
4860
    Ok(rows
49
4860
        .into_iter()
50
17280
        .map(|row| (row.id, row.database_name))
51
        .collect())
52
4860
}
53

            
54
1440
async fn quarantine(
55
1440
    user_id: Uuid,
56
1440
    reason: &str,
57
1440
    run_started: DateTime<Utc>,
58
1440
) -> Result<(), DBError> {
59
1440
    let mut conn = crate::db::get_connection().await?;
60

            
61
1440
    let recorded = sqlx::query_file!(
62
        "../server/sql/update/users/quarantine.sql",
63
        user_id,
64
        reason,
65
        run_started
66
    )
67
1440
    .execute(&mut *conn)
68
1440
    .await
69
1440
    .map_err(DBError::Sqlx)?;
70

            
71
1440
    if recorded.rows_affected() == 0 {
72
1260
        tracing::info!("{user_id} already carries a verdict newer than this run");
73
180
    }
74

            
75
1440
    Ok(())
76
1440
}
77

            
78
/// Lifts a quarantine, but only one that predates this run.
79
///
80
/// The fence matters when two releases' migrators overlap: without it, an older
81
/// migrator finishing successfully against its own (older) migration set would
82
/// clear a quarantine a newer migrator had just written for failing on a newer
83
/// one — and the newest application would then serve a tenant known not to have
84
/// the schema it needs.
85
7380
async fn clear_quarantine(user_id: Uuid, run_started: DateTime<Utc>) -> Result<(), DBError> {
86
7380
    let mut conn = crate::db::get_connection().await?;
87

            
88
7380
    let cleared = sqlx::query_file!(
89
        "../server/sql/update/users/clear_quarantine.sql",
90
        user_id,
91
        run_started
92
    )
93
7380
    .execute(&mut *conn)
94
7380
    .await
95
7380
    .map_err(DBError::Sqlx)?;
96

            
97
    // A tenant that was never quarantined still matches the fence, so this is
98
    // not the ordinary case — it means someone newer has already ruled.
99
7380
    if cleared.rows_affected() == 0 {
100
1800
        tracing::info!("{user_id} was not cleared: its quarantine is newer than this run");
101
5580
    }
102

            
103
7380
    Ok(())
104
7380
}