shuttle_common/
lib.rs

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
pub mod constants;
#[cfg(feature = "models")]
pub mod models;
pub mod secrets;
#[cfg(feature = "tables")]
pub mod tables;
pub mod templates;

use serde::{Deserialize, Serialize};

////// Resource Input/Output types

/// The input given to Shuttle DB resources
#[derive(Clone, Deserialize, Serialize, Default)]
pub struct DbInput {
    pub local_uri: Option<String>,
    /// Override the default db name. Only applies to RDS.
    pub db_name: Option<String>,
}

/// The output produced by Shuttle DB resources
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub enum DatabaseResource {
    ConnectionString(String),
    Info(DatabaseInfo),
}

/// Holds the data for building a database connection string.
#[derive(Clone, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct DatabaseInfo {
    engine: String,
    role_name: String,
    role_password: String,
    database_name: String,
    port: String,
    hostname: String,
    /// The RDS instance name, which is required for deleting provisioned RDS instances, it's
    /// optional because it isn't needed for shared PG deletion.
    instance_name: Option<String>,
}

impl DatabaseInfo {
    pub fn new(
        engine: String,
        role_name: String,
        role_password: String,
        database_name: String,
        port: String,
        hostname: String,
        instance_name: Option<String>,
    ) -> Self {
        Self {
            engine,
            role_name,
            role_password,
            database_name,
            port,
            hostname,
            instance_name,
        }
    }

    /// For connecting to the database.
    pub fn connection_string(&self, show_password: bool) -> String {
        format!(
            "{}://{}:{}@{}:{}/{}",
            self.engine,
            self.role_name,
            if show_password {
                &self.role_password
            } else {
                "********"
            },
            self.hostname,
            self.port,
            self.database_name,
        )
    }

    pub fn role_name(&self) -> String {
        self.role_name.to_string()
    }

    pub fn database_name(&self) -> String {
        self.database_name.to_string()
    }

    pub fn instance_name(&self) -> Option<String> {
        self.instance_name.clone()
    }
}

// Don't leak password in Debug
impl std::fmt::Debug for DatabaseInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "DatabaseInfo {{ {:?} }}", self.connection_string(false))
    }
}

/// Used to request a container from the local run provisioner
#[derive(Serialize, Deserialize)]
pub struct ContainerRequest {
    pub project_name: String,
    /// Type of container, used in the container name. ex "qdrant"
    pub container_name: String,
    /// ex. "qdrant/qdrant:latest"
    pub image: String,
    /// The internal port that the container should expose. ex. "6334/tcp"
    pub port: String,
    /// list of "KEY=value" strings
    pub env: Vec<String>,
}

/// Response from requesting a container from the local run provisioner
#[derive(Serialize, Deserialize)]
pub struct ContainerResponse {
    /// The port that the container exposes to the host.
    /// Is a string for parity with the Docker respose.
    pub host_port: String,
}

/// Check if two versions are compatible based on the rule used by cargo:
/// "Versions `a` and `b` are compatible if their left-most nonzero digit is the same."
pub fn semvers_are_compatible(a: &semver::Version, b: &semver::Version) -> bool {
    if a.major != 0 || b.major != 0 {
        a.major == b.major
    } else if a.minor != 0 || b.minor != 0 {
        a.minor == b.minor
    } else {
        a.patch == b.patch
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    #[test]
    fn semver_compatibility_check_works() {
        let semver_tests = &[
            ("1.0.0", "1.0.0", true),
            ("1.8.0", "1.0.0", true),
            ("0.1.0", "0.2.1", false),
            ("0.9.0", "0.2.0", false),
        ];
        for (version_a, version_b, are_compatible) in semver_tests {
            let version_a = semver::Version::from_str(version_a).unwrap();
            let version_b = semver::Version::from_str(version_b).unwrap();
            assert_eq!(
                super::semvers_are_compatible(&version_a, &version_b),
                *are_compatible
            );
        }
    }
}