Skip to main content

drizzle_types/
migration.rs

1use crate::alloc_prelude::*;
2
3/// Identifier casing strategy for inferred names.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7pub enum Casing {
8    /// `camelCase` (e.g. `userId`, `createdAt`).
9    #[default]
10    #[cfg_attr(feature = "serde", serde(rename = "camelCase"))]
11    CamelCase,
12    /// `snake_case` (e.g. `user_id`, `created_at`).
13    #[cfg_attr(feature = "serde", serde(rename = "snake_case"))]
14    SnakeCase,
15}
16
17impl Casing {
18    #[must_use]
19    pub const fn as_str(self) -> &'static str {
20        match self {
21            Self::CamelCase => "camelCase",
22            Self::SnakeCase => "snake_case",
23        }
24    }
25}
26
27impl core::fmt::Display for Casing {
28    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29        f.write_str(self.as_str())
30    }
31}
32
33#[cfg(any(feature = "std", feature = "alloc"))]
34impl core::str::FromStr for Casing {
35    type Err = crate::alloc_prelude::String;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        match s {
39            "camelCase" | "camel" => Ok(Self::CamelCase),
40            "snake_case" | "snake" => Ok(Self::SnakeCase),
41            _ => Err(format!(
42                "invalid casing '{s}', expected 'camelCase' or 'snake_case'"
43            )),
44        }
45    }
46}
47
48/// Shared migration metadata configuration.
49///
50/// This contains only tracking metadata and can be reused by higher-level crates
51/// (CLI, runtime migrator, etc.) without pulling in migration runtime logic.
52#[derive(Debug, Clone, PartialEq, Eq, Hash)]
53pub struct MigrationTracking {
54    /// Migrations tracking table name.
55    pub table: Cow<'static, str>,
56    /// Optional schema name for the tracking table (`PostgreSQL`).
57    pub schema: Option<Cow<'static, str>>,
58}
59
60impl MigrationTracking {
61    /// Default `SQLite` migration tracking metadata.
62    pub const SQLITE: Self = Self {
63        table: Cow::Borrowed("__drizzle_migrations"),
64        schema: None,
65    };
66
67    /// Default `PostgreSQL` migration tracking metadata.
68    pub const POSTGRES: Self = Self {
69        table: Cow::Borrowed("__drizzle_migrations"),
70        schema: Some(Cow::Borrowed("drizzle")),
71    };
72
73    /// Create tracking metadata from table/schema values.
74    pub fn new(
75        table: impl Into<Cow<'static, str>>,
76        schema: Option<impl Into<Cow<'static, str>>>,
77    ) -> Self {
78        Self {
79            table: table.into(),
80            schema: schema.map(Into::into),
81        }
82    }
83
84    /// Override table name while preserving schema.
85    #[must_use]
86    pub fn table(mut self, table: impl Into<Cow<'static, str>>) -> Self {
87        self.table = table.into();
88        self
89    }
90
91    /// Override schema while preserving table name.
92    #[must_use]
93    pub fn schema(mut self, schema: impl Into<Cow<'static, str>>) -> Self {
94        self.schema = Some(schema.into());
95        self
96    }
97
98    /// Clear the schema while preserving table name.
99    #[must_use]
100    pub fn without_schema(mut self) -> Self {
101        self.schema = None;
102        self
103    }
104}
105impl Default for MigrationTracking {
106    fn default() -> Self {
107        Self::SQLITE
108    }
109}
110
111/// A value that's either a literal string or an env-var reference.
112///
113/// In TOML this deserializes from `"literal"` or `{ env = "VAR_NAME" }` — the
114/// same shape `drizzle-kit` and the CLI accept for `dbCredentials.url`. Used
115/// anywhere a config value can be either inline or pulled from the
116/// environment at runtime.
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub enum EnvOr {
119    /// Literal value taken from the config file.
120    Value(String),
121    /// Name of the environment variable to resolve.
122    Env(String),
123}
124
125#[cfg(feature = "std")]
126impl EnvOr {
127    /// Resolve to a concrete string, reading the environment if needed.
128    ///
129    /// # Errors
130    ///
131    /// Returns [`EnvOrError::NotPresent`] if this is an [`EnvOr::Env`] pointing
132    /// to a variable that is not set, or [`EnvOrError::NotUnicode`] if the
133    /// variable is set but contains invalid UTF-8.
134    pub fn resolve(&self) -> Result<String, EnvOrError> {
135        match self {
136            Self::Value(v) => Ok(v.clone()),
137            Self::Env(var) => match std::env::var(var) {
138                Ok(v) => Ok(v),
139                Err(std::env::VarError::NotPresent) => Err(EnvOrError::NotPresent(var.clone())),
140                Err(std::env::VarError::NotUnicode(_)) => Err(EnvOrError::NotUnicode(var.clone())),
141            },
142        }
143    }
144
145    /// Resolve to an optional value (returns `None` when an `Env` var is unset).
146    ///
147    /// # Errors
148    ///
149    /// Returns [`EnvOrError::NotUnicode`] if the env var is set but contains
150    /// invalid UTF-8. Missing env vars resolve to `Ok(None)`.
151    pub fn resolve_optional(&self) -> Result<Option<String>, EnvOrError> {
152        match self {
153            Self::Value(v) => Ok(Some(v.clone())),
154            Self::Env(var) => match std::env::var(var) {
155                Ok(v) => Ok(Some(v)),
156                Err(std::env::VarError::NotPresent) => Ok(None),
157                Err(std::env::VarError::NotUnicode(_)) => Err(EnvOrError::NotUnicode(var.clone())),
158            },
159        }
160    }
161}
162
163/// Failure resolving an [`EnvOr::Env`] reference.
164#[cfg(feature = "std")]
165#[derive(Debug, Clone, PartialEq, Eq)]
166pub enum EnvOrError {
167    /// The named environment variable is not set in the process.
168    NotPresent(String),
169    /// The named environment variable is set but contains non-UTF-8 bytes.
170    NotUnicode(String),
171}
172
173#[cfg(feature = "std")]
174impl core::fmt::Display for EnvOrError {
175    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
176        match self {
177            Self::NotPresent(var) => write!(f, "env var `{var}` not set"),
178            Self::NotUnicode(var) => write!(f, "env var `{var}` contains invalid unicode"),
179        }
180    }
181}
182
183#[cfg(feature = "std")]
184impl std::error::Error for EnvOrError {}
185
186#[cfg(feature = "serde")]
187impl<'de> serde::Deserialize<'de> for EnvOr {
188    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
189    where
190        D: serde::Deserializer<'de>,
191    {
192        use serde::de::{self, MapAccess, Visitor};
193
194        struct EnvOrVisitor;
195
196        impl<'de> Visitor<'de> for EnvOrVisitor {
197            type Value = EnvOr;
198
199            fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
200                formatter.write_str("a string or { env = \"VAR_NAME\" }")
201            }
202
203            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
204            where
205                E: de::Error,
206            {
207                Ok(EnvOr::Value(value.to_string()))
208            }
209
210            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
211            where
212                M: MapAccess<'de>,
213            {
214                let mut env_var: Option<String> = None;
215
216                while let Some(key) = map.next_key::<String>()? {
217                    if key == "env" {
218                        env_var = Some(map.next_value()?);
219                    } else {
220                        return Err(de::Error::unknown_field(&key, &["env"]));
221                    }
222                }
223
224                env_var
225                    .map(EnvOr::Env)
226                    .ok_or_else(|| de::Error::missing_field("env"))
227            }
228        }
229
230        deserializer.deserialize_any(EnvOrVisitor)
231    }
232}
233
234#[cfg(feature = "schemars")]
235impl schemars::JsonSchema for EnvOr {
236    fn schema_name() -> Cow<'static, str> {
237        "EnvOr".into()
238    }
239
240    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
241        use schemars::json_schema;
242
243        // EnvOr accepts either a plain string or { env: "VAR_NAME" }
244        json_schema!({
245            "oneOf": [
246                generator.subschema_for::<String>(),
247                {
248                    "type": "object",
249                    "properties": {
250                        "env": { "type": "string" }
251                    },
252                    "required": ["env"],
253                    "additionalProperties": false
254                }
255            ]
256        })
257    }
258}