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 config value that is either inline 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 ConfigValue {
119    /// Value written inline in the config file.
120    Inline(String),
121    /// Name of the environment variable to resolve.
122    Env(String),
123}
124
125#[cfg(feature = "std")]
126impl ConfigValue {
127    /// Resolve to a concrete string, reading the environment if needed.
128    ///
129    /// # Errors
130    ///
131    /// Returns [`ConfigValueError::NotPresent`] if this is a [`ConfigValue::Env`] pointing
132    /// to a variable that is not set, or [`ConfigValueError::NotUnicode`] if the
133    /// variable is set but contains invalid UTF-8.
134    pub fn resolve(&self) -> Result<String, ConfigValueError> {
135        match self {
136            Self::Inline(v) => Ok(v.clone()),
137            Self::Env(var) => match std::env::var(var) {
138                Ok(v) => Ok(v),
139                Err(std::env::VarError::NotPresent) => {
140                    Err(ConfigValueError::NotPresent(var.clone()))
141                }
142                Err(std::env::VarError::NotUnicode(_)) => {
143                    Err(ConfigValueError::NotUnicode(var.clone()))
144                }
145            },
146        }
147    }
148
149    /// Resolve to an optional value (returns `None` when an `Env` var is unset).
150    ///
151    /// # Errors
152    ///
153    /// Returns [`ConfigValueError::NotUnicode`] if the env var is set but contains
154    /// invalid UTF-8. Missing env vars resolve to `Ok(None)`.
155    pub fn resolve_optional(&self) -> Result<Option<String>, ConfigValueError> {
156        match self {
157            Self::Inline(v) => Ok(Some(v.clone())),
158            Self::Env(var) => match std::env::var(var) {
159                Ok(v) => Ok(Some(v)),
160                Err(std::env::VarError::NotPresent) => Ok(None),
161                Err(std::env::VarError::NotUnicode(_)) => {
162                    Err(ConfigValueError::NotUnicode(var.clone()))
163                }
164            },
165        }
166    }
167}
168
169/// Failure resolving a [`ConfigValue::Env`] reference.
170#[cfg(feature = "std")]
171#[derive(Debug, Clone, PartialEq, Eq)]
172pub enum ConfigValueError {
173    /// The named environment variable is not set in the process.
174    NotPresent(String),
175    /// The named environment variable is set but contains non-UTF-8 bytes.
176    NotUnicode(String),
177}
178
179#[cfg(feature = "std")]
180impl core::fmt::Display for ConfigValueError {
181    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
182        match self {
183            Self::NotPresent(var) => write!(f, "env var `{var}` not set"),
184            Self::NotUnicode(var) => write!(f, "env var `{var}` contains invalid unicode"),
185        }
186    }
187}
188
189#[cfg(feature = "std")]
190impl std::error::Error for ConfigValueError {}
191
192#[cfg(feature = "serde")]
193impl<'de> serde::Deserialize<'de> for ConfigValue {
194    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
195    where
196        D: serde::Deserializer<'de>,
197    {
198        use serde::de::{self, MapAccess, Visitor};
199
200        struct ConfigValueVisitor;
201
202        impl<'de> Visitor<'de> for ConfigValueVisitor {
203            type Value = ConfigValue;
204
205            fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
206                formatter.write_str("a string or { env = \"VAR_NAME\" }")
207            }
208
209            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
210            where
211                E: de::Error,
212            {
213                Ok(ConfigValue::Inline(value.to_string()))
214            }
215
216            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
217            where
218                M: MapAccess<'de>,
219            {
220                let mut env_var: Option<String> = None;
221
222                while let Some(key) = map.next_key::<String>()? {
223                    if key == "env" {
224                        env_var = Some(map.next_value()?);
225                    } else {
226                        return Err(de::Error::unknown_field(&key, &["env"]));
227                    }
228                }
229
230                env_var
231                    .map(ConfigValue::Env)
232                    .ok_or_else(|| de::Error::missing_field("env"))
233            }
234        }
235
236        deserializer.deserialize_any(ConfigValueVisitor)
237    }
238}
239
240#[cfg(feature = "schemars")]
241impl schemars::JsonSchema for ConfigValue {
242    fn schema_name() -> Cow<'static, str> {
243        "ConfigValue".into()
244    }
245
246    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
247        use schemars::json_schema;
248
249        // ConfigValue accepts either a plain string or { env: "VAR_NAME" }
250        json_schema!({
251            "oneOf": [
252                generator.subschema_for::<String>(),
253                {
254                    "type": "object",
255                    "properties": {
256                        "env": { "type": "string" }
257                    },
258                    "required": ["env"],
259                    "additionalProperties": false
260                }
261            ]
262        })
263    }
264}