aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_server/database/mod.rs
blob: 845d67d71677355e37dc8246647dfc38743c6a21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
pub(crate) mod calendar;
pub(crate) mod db;
pub(crate) mod models;

use std::fmt::{Debug, Display};

use serde::{Deserialize, Serialize};
use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};

#[derive(Debug)]
pub(crate) enum DbError {
    NotFound,
    Other(eyre::Report),
}

impl Display for DbError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

impl From<time::error::ComponentRange> for DbError {
    fn from(error: time::error::ComponentRange) -> Self {
        DbError::Other(error.into())
    }
}

impl From<time::error::Error> for DbError {
    fn from(error: time::error::Error) -> Self {
        DbError::Other(error.into())
    }
}

impl From<sqlx::Error> for DbError {
    fn from(error: sqlx::Error) -> Self {
        match error {
            sqlx::Error::RowNotFound => DbError::NotFound,
            error => DbError::Other(error.into()),
        }
    }
}

impl std::error::Error for DbError {}

pub(crate) type DbResult<T> = Result<T, DbError>;

#[derive(Debug, PartialEq)]
pub(crate) enum DbType {
    Postgres,
    Unknown,
}

#[derive(Clone, Deserialize, Serialize)]
pub(crate) struct DbSettings {
    pub(crate) db_uri: String,
    /// Optional URI for read replicas. If set, read-only queries will use this connection.
    pub(crate) read_db_uri: Option<String>,
}

impl DbSettings {
    pub(crate) fn db_type(&self) -> DbType {
        if self.db_uri.starts_with("postgres://") || self.db_uri.starts_with("postgresql://") {
            DbType::Postgres
        } else {
            DbType::Unknown
        }
    }
}

fn redact_db_uri(uri: &str) -> String {
    url::Url::parse(uri)
        .map(|mut url| {
            let _ = url.set_password(Some("****"));
            url.to_string()
        })
        .unwrap_or_else(|_| uri.to_string())
}

// Do our best to redact passwords so they're not logged in the event of an error.
impl Debug for DbSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.db_type() == DbType::Postgres {
            let redacted_uri = redact_db_uri(&self.db_uri);
            let redacted_read_uri = self.read_db_uri.as_ref().map(|uri| redact_db_uri(uri));
            f.debug_struct("DbSettings")
                .field("db_uri", &redacted_uri)
                .field("read_db_uri", &redacted_read_uri)
                .finish()
        } else {
            f.debug_struct("DbSettings")
                .field("db_uri", &self.db_uri)
                .field("read_db_uri", &self.read_db_uri)
                .finish()
        }
    }
}

pub(crate) fn into_utc(x: OffsetDateTime) -> PrimitiveDateTime {
    let x = x.to_offset(UtcOffset::UTC);
    PrimitiveDateTime::new(x.date(), x.time())
}

#[cfg(test)]
mod tests {
    use time::macros::datetime;

    use super::into_utc;

    #[test]
    fn utc() {
        let dt = datetime!(2023-09-26 15:11:02 +05:30);
        assert_eq!(into_utc(dt), datetime!(2023-09-26 09:41:02));
        assert_eq!(into_utc(dt).assume_utc(), dt);

        let dt = datetime!(2023-09-26 15:11:02 -07:00);
        assert_eq!(into_utc(dt), datetime!(2023-09-26 22:11:02));
        assert_eq!(into_utc(dt).assume_utc(), dt);

        let dt = datetime!(2023-09-26 15:11:02 +00:00);
        assert_eq!(into_utc(dt), datetime!(2023-09-26 15:11:02));
        assert_eq!(into_utc(dt).assume_utc(), dt);
    }
}