vespertide-core 0.2.1

Data models for tables, columns, constraints, indexes, and migration actions
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use serde::{Deserialize, Serialize};

use crate::schema::{
    foreign_key::ForeignKeySyntax,
    names::ColumnName,
    primary_key::PrimaryKeySyntax,
    str_or_bool::{StrOrBoolOrArray, StringOrBool},
};

/// Definition of a single table column, including its type, nullability, and inline constraints.
///
/// Inline constraints (`primary_key`, `unique`, `index`, `foreign_key`) are the preferred way to
/// declare constraints in model JSON files. Call [`TableDef::normalize`] to convert them into
/// table-level [`TableConstraint`] entries before diffing or SQL generation.
///
/// Use [`ColumnDef::new`] to construct a column programmatically, then chain the setter methods
/// (`.primary_key()`, `.unique()`, `.index()`, `.foreign_key()`, `.default()`, `.comment()`) to
/// attach optional fields.
///
/// [`TableDef::normalize`]: crate::schema::TableDef::normalize
/// [`TableConstraint`]: crate::schema::TableConstraint
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub struct ColumnDef {
    pub name: ColumnName,
    pub r#type: ColumnType,
    pub nullable: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<StringOrBool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_key: Option<PrimaryKeySyntax>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unique: Option<StrOrBoolOrArray>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<StrOrBoolOrArray>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub foreign_key: Option<ForeignKeySyntax>,
}

/// The SQL type of a column, either a parameter-free simple type or a parameterised complex type.
///
/// In JSON model files a simple type is written as a plain string (`"integer"`, `"text"`, etc.)
/// while a complex type is written as an object with a `"kind"` discriminant
/// (`{"kind": "varchar", "length": 255}`).
///
/// Always construct via the wrapped variants:
/// ```
/// use vespertide_core::{ColumnType, SimpleColumnType, ComplexColumnType};
/// let t1 = ColumnType::Simple(SimpleColumnType::Integer);
/// let t2 = ColumnType::Complex(ComplexColumnType::Varchar { length: 255 });
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case", untagged)]
pub enum ColumnType {
    /// A parameter-free SQL type such as `INTEGER`, `TEXT`, or `UUID`.
    Simple(SimpleColumnType),
    /// A parameterised SQL type such as `VARCHAR(n)`, `NUMERIC(p,s)`, or a named enum.
    Complex(ComplexColumnType),
}

impl ColumnType {
    /// Returns true if this type supports `auto_increment` (integer types only)
    pub fn supports_auto_increment(&self) -> bool {
        match self {
            ColumnType::Simple(ty) => ty.supports_auto_increment(),
            ColumnType::Complex(_) => false,
        }
    }

    /// Check if two column types require a migration.
    /// For integer enums, no migration is ever needed because the underlying DB type is always INTEGER.
    /// The enum name and values only affect code generation (`SeaORM` entities), not the database schema.
    pub fn requires_migration(&self, other: &ColumnType) -> bool {
        match (self, other) {
            (
                ColumnType::Complex(ComplexColumnType::Enum {
                    values: values1, ..
                }),
                ColumnType::Complex(ComplexColumnType::Enum {
                    values: values2, ..
                }),
            ) => {
                // Both are integer enums - never require migration (DB type is always INTEGER)
                if values1.is_integer() && values2.is_integer() {
                    false
                } else {
                    // String enums: compare only values, not name.
                    // The enum name is a user-facing label; the actual DB type name
                    // is auto-generated with a table prefix at SQL generation time.
                    // Different labels with identical values don't require a migration.
                    values1 != values2
                }
            }
            _ => self != other,
        }
    }

    /// Convert column type to Rust type string (for `SeaORM` entity generation)
    pub fn to_rust_type(&self, nullable: bool) -> String {
        let base = match self {
            ColumnType::Simple(ty) => match ty {
                SimpleColumnType::SmallInt => "i16".to_string(),
                SimpleColumnType::Integer => "i32".to_string(),
                SimpleColumnType::BigInt => "i64".to_string(),
                SimpleColumnType::Real => "f32".to_string(),
                SimpleColumnType::DoublePrecision => "f64".to_string(),
                SimpleColumnType::Text
                | SimpleColumnType::Interval
                | SimpleColumnType::Inet
                | SimpleColumnType::Cidr
                | SimpleColumnType::Macaddr
                | SimpleColumnType::Xml => "String".to_string(),
                SimpleColumnType::Boolean => "bool".to_string(),
                SimpleColumnType::Date => "Date".to_string(),
                SimpleColumnType::Time => "Time".to_string(),
                SimpleColumnType::Timestamp => "DateTime".to_string(),
                SimpleColumnType::Timestamptz => "DateTimeWithTimeZone".to_string(),
                SimpleColumnType::Bytea => "Vec<u8>".to_string(),
                SimpleColumnType::Uuid => "Uuid".to_string(),
                SimpleColumnType::Json => "Json".to_string(),
            },
            ColumnType::Complex(ty) => match ty {
                ComplexColumnType::Numeric { .. } => "Decimal".to_string(),
                ComplexColumnType::Varchar { .. }
                | ComplexColumnType::Char { .. }
                | ComplexColumnType::Custom { .. }
                | ComplexColumnType::Enum { .. } => "String".to_string(),
            },
        };

        if nullable {
            format!("Option<{base}>")
        } else {
            base
        }
    }

    /// Convert column type to human-readable display string (for CLI prompts)
    /// Examples: "integer", "text", "varchar(255)", "numeric(10,2)"
    pub fn to_display_string(&self) -> String {
        match self {
            ColumnType::Simple(ty) => ty.to_display_string(),
            ColumnType::Complex(ty) => ty.to_display_string(),
        }
    }

    /// Get the default fill value for this column type (for CLI prompts)
    /// Returns None if no sensible default exists for the type
    pub fn default_fill_value(&self) -> &'static str {
        match self {
            ColumnType::Simple(ty) => ty.default_fill_value(),
            ColumnType::Complex(ty) => ty.default_fill_value(),
        }
    }

    /// Get enum variant names if this is an enum type
    /// Returns None if not an enum, Some(names) otherwise
    pub fn enum_variant_names(&self) -> Option<Vec<String>> {
        match self {
            ColumnType::Complex(ComplexColumnType::Enum { values, .. }) => Some(
                values
                    .variant_names()
                    .into_iter()
                    .map(String::from)
                    .collect(),
            ),
            _ => None,
        }
    }
}

impl ColumnDef {
    /// Construct a new column with required fields only.
    /// Use the `.primary_key()`, `.unique()`, `.index()`, `.foreign_key()`,
    /// `.default()`, `.comment()` setters to add optional fields.
    ///
    /// # Examples
    /// ```
    /// use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType};
    /// let id = ColumnDef::new("id", ColumnType::Simple(SimpleColumnType::Integer), false);
    /// ```
    #[must_use]
    pub fn new(name: impl Into<ColumnName>, r#type: ColumnType, nullable: bool) -> Self {
        Self {
            name: name.into(),
            r#type,
            nullable,
            default: None,
            comment: None,
            primary_key: None,
            unique: None,
            index: None,
            foreign_key: None,
        }
    }

    /// Mark this column as part of the primary key.
    #[must_use]
    pub fn primary_key(mut self, pk: PrimaryKeySyntax) -> Self {
        self.primary_key = Some(pk);
        self
    }

    /// Add a unique constraint to this column.
    #[must_use]
    pub fn unique(mut self, unique: StrOrBoolOrArray) -> Self {
        self.unique = Some(unique);
        self
    }

    /// Add an index on this column.
    #[must_use]
    pub fn index(mut self, index: StrOrBoolOrArray) -> Self {
        self.index = Some(index);
        self
    }

    /// Add a foreign key reference from this column.
    #[must_use]
    pub fn foreign_key(mut self, fk: ForeignKeySyntax) -> Self {
        self.foreign_key = Some(fk);
        self
    }

    /// Set the column default value.
    #[must_use]
    pub fn default(mut self, default: StringOrBool) -> Self {
        self.default = Some(default);
        self
    }

    /// Add a column comment.
    #[must_use]
    pub fn comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }
}

/// Parameter-free SQL column types supported across all backends.
///
/// Each variant maps directly to a standard SQL type. Use these via
/// [`ColumnType::Simple`] when no length, precision, or scale is needed.
///
/// This enum is `#[non_exhaustive]`: new variants may be added in future releases.
/// Downstream `match` expressions should include a wildcard arm.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SimpleColumnType {
    /// 16-bit signed integer (`SMALLINT`).
    SmallInt,
    /// 32-bit signed integer (`INTEGER`). Supports `auto_increment`.
    Integer,
    /// 64-bit signed integer (`BIGINT`). Supports `auto_increment`.
    BigInt,
    /// 32-bit floating-point number (`REAL`).
    Real,
    /// 64-bit floating-point number (`DOUBLE PRECISION`).
    DoublePrecision,

    // Text types
    /// Unbounded Unicode text (`TEXT`).
    Text,

    // Boolean type
    /// Boolean true/false value (`BOOLEAN`).
    Boolean,

    // Date/Time types
    /// Calendar date without time (`DATE`).
    Date,
    /// Time of day without date (`TIME`).
    Time,
    /// Date and time without timezone (`TIMESTAMP`).
    Timestamp,
    /// Date and time with timezone (`TIMESTAMPTZ`). Prefer this over `Timestamp`.
    Timestamptz,
    /// Time span / duration (`INTERVAL`).
    Interval,

    // Binary type
    /// Variable-length binary data (`BYTEA`).
    Bytea,

    // UUID type
    /// Universally unique identifier (`UUID`).
    Uuid,

    // JSON types
    /// JSON value stored as text (`JSON`). Cross-backend compatible; prefer over `jsonb`.
    Json,

    // Network types
    /// IPv4 or IPv6 host address (`INET`). PostgreSQL-specific.
    Inet,
    /// IPv4 or IPv6 network address (`CIDR`). PostgreSQL-specific.
    Cidr,
    /// MAC address (`MACADDR`). PostgreSQL-specific.
    Macaddr,

    // XML type
    /// XML document (`XML`). PostgreSQL-specific.
    Xml,
}

impl SimpleColumnType {
    /// Returns the SQL type name for this simple column type.
    #[must_use]
    pub fn sql_type(&self) -> &'static str {
        match self {
            SimpleColumnType::SmallInt => "SMALLINT",
            SimpleColumnType::Integer => "INTEGER",
            SimpleColumnType::BigInt => "BIGINT",
            SimpleColumnType::Real => "REAL",
            SimpleColumnType::DoublePrecision => "DOUBLE PRECISION",
            SimpleColumnType::Text => "TEXT",
            SimpleColumnType::Boolean => "BOOLEAN",
            SimpleColumnType::Date => "DATE",
            SimpleColumnType::Time => "TIME",
            SimpleColumnType::Timestamp => "TIMESTAMP",
            SimpleColumnType::Timestamptz => "TIMESTAMPTZ",
            SimpleColumnType::Interval => "INTERVAL",
            SimpleColumnType::Bytea => "BYTEA",
            SimpleColumnType::Uuid => "UUID",
            SimpleColumnType::Json => "JSON",
            SimpleColumnType::Inet => "INET",
            SimpleColumnType::Cidr => "CIDR",
            SimpleColumnType::Macaddr => "MACADDR",
            SimpleColumnType::Xml => "XML",
        }
    }

    /// Returns true if this type supports `auto_increment` (integer types only)
    pub fn supports_auto_increment(&self) -> bool {
        matches!(
            self,
            SimpleColumnType::SmallInt | SimpleColumnType::Integer | SimpleColumnType::BigInt
        )
    }

    /// Convert to human-readable display string
    pub fn to_display_string(&self) -> String {
        match self {
            SimpleColumnType::SmallInt => "smallint".to_string(),
            SimpleColumnType::Integer => "integer".to_string(),
            SimpleColumnType::BigInt => "bigint".to_string(),
            SimpleColumnType::Real => "real".to_string(),
            SimpleColumnType::DoublePrecision => "double precision".to_string(),
            SimpleColumnType::Text => "text".to_string(),
            SimpleColumnType::Boolean => "boolean".to_string(),
            SimpleColumnType::Date => "date".to_string(),
            SimpleColumnType::Time => "time".to_string(),
            SimpleColumnType::Timestamp => "timestamp".to_string(),
            SimpleColumnType::Timestamptz => "timestamptz".to_string(),
            SimpleColumnType::Interval => "interval".to_string(),
            SimpleColumnType::Bytea => "bytea".to_string(),
            SimpleColumnType::Uuid => "uuid".to_string(),
            SimpleColumnType::Json => "json".to_string(),
            SimpleColumnType::Inet => "inet".to_string(),
            SimpleColumnType::Cidr => "cidr".to_string(),
            SimpleColumnType::Macaddr => "macaddr".to_string(),
            SimpleColumnType::Xml => "xml".to_string(),
        }
    }

    /// Get the default fill value for this type
    /// Returns None if no sensible default exists
    pub fn default_fill_value(&self) -> &'static str {
        match self {
            SimpleColumnType::SmallInt | SimpleColumnType::Integer | SimpleColumnType::BigInt => {
                "0"
            }
            SimpleColumnType::Real | SimpleColumnType::DoublePrecision => "0.0",
            SimpleColumnType::Boolean => "false",
            SimpleColumnType::Text | SimpleColumnType::Bytea => "''",
            SimpleColumnType::Date => "'1970-01-01'",
            SimpleColumnType::Time => "'00:00:00'",
            SimpleColumnType::Timestamp | SimpleColumnType::Timestamptz => "CURRENT_TIMESTAMP",
            SimpleColumnType::Interval => "'0'",
            SimpleColumnType::Uuid => "'00000000-0000-0000-0000-000000000000'",
            SimpleColumnType::Json => "'{}'",
            SimpleColumnType::Inet | SimpleColumnType::Cidr => "'0.0.0.0'",
            SimpleColumnType::Macaddr => "'00:00:00:00:00:00'",
            SimpleColumnType::Xml => "'<xml/>'",
        }
    }
}

/// A single variant of an integer-backed enum, pairing a Rust-friendly name with its stored value.
///
/// Used inside [`EnumValues::Integer`] to define enums that are stored as `INTEGER` in the
/// database. Leave gaps between values (e.g. 0, 10, 20) so new variants can be inserted later
/// without renumbering.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct NumValue {
    /// The variant name used in generated code (e.g. `"active"`).
    pub name: String,
    /// The integer value stored in the database column.
    pub value: i64,
}

/// The set of allowed values for an enum column, either string-based or integer-based.
///
/// **String enums** map to a native `PostgreSQL` `ENUM` type. Adding or removing values requires a
/// database migration (`ALTER TYPE`).
///
/// **Integer enums** are stored as `INTEGER`. New variants can be added to the model without any
/// database migration because the underlying column type never changes.
///
/// Choose integer enums for expandable value sets (roles, priorities) and string enums for
/// stable, human-readable status fields.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum EnumValues {
    /// String enum: each variant is a plain string stored in a native DB enum type.
    String(Vec<String>),
    /// Integer enum: each variant has an explicit numeric value stored as `INTEGER`.
    Integer(Vec<NumValue>),
}

impl EnumValues {
    /// Check if this is a string enum
    pub fn is_string(&self) -> bool {
        matches!(self, EnumValues::String(_))
    }

    /// Check if this is an integer enum
    pub fn is_integer(&self) -> bool {
        matches!(self, EnumValues::Integer(_))
    }

    /// Get all variant names
    pub fn variant_names(&self) -> Vec<&str> {
        match self {
            EnumValues::String(values) => values.iter().map(std::string::String::as_str).collect(),
            EnumValues::Integer(values) => values.iter().map(|v| v.name.as_str()).collect(),
        }
    }

    /// Get the number of variants
    pub fn len(&self) -> usize {
        match self {
            EnumValues::String(values) => values.len(),
            EnumValues::Integer(values) => values.len(),
        }
    }

    /// Check if there are no variants
    pub fn is_empty(&self) -> bool {
        match self {
            EnumValues::String(values) => values.is_empty(),
            EnumValues::Integer(values) => values.is_empty(),
        }
    }

    /// Get SQL values for CREATE TYPE ENUM (only for string enums)
    /// Returns quoted strings like 'value1', 'value2'
    pub fn to_sql_values(&self) -> Vec<String> {
        match self {
            EnumValues::String(values) => values
                .iter()
                .map(|s| format!("'{}'", s.replace('\'', "''")))
                .collect(),
            EnumValues::Integer(values) => values.iter().map(|v| v.value.to_string()).collect(),
        }
    }
}

impl From<Vec<String>> for EnumValues {
    fn from(values: Vec<String>) -> Self {
        EnumValues::String(values)
    }
}

impl From<Vec<&str>> for EnumValues {
    fn from(values: Vec<&str>) -> Self {
        EnumValues::String(
            values
                .into_iter()
                .map(std::string::ToString::to_string)
                .collect(),
        )
    }
}

/// Parameterised SQL column types that require additional configuration beyond a simple keyword.
///
/// In JSON model files these are written as objects with a `"kind"` discriminant, for example
/// `{"kind": "varchar", "length": 255}` or `{"kind": "enum", "name": "status", "values": [...]}`.
///
/// Use these via [`ColumnType::Complex`].
///
/// This enum is `#[non_exhaustive]`: new variants may be added in future releases.
/// Downstream `match` expressions should include a wildcard arm.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case", tag = "kind")]
#[non_exhaustive]
pub enum ComplexColumnType {
    /// Variable-length character string with a maximum byte length (`VARCHAR(n)`).
    Varchar { length: u32 },
    /// Exact fixed-point number with configurable precision and scale (`NUMERIC(p, s)`).
    Numeric { precision: u32, scale: u32 },
    /// Fixed-length character string padded with spaces (`CHAR(n)`).
    Char { length: u32 },
    /// Escape hatch for database-specific types not covered by other variants.
    /// Breaks cross-database portability; avoid unless absolutely necessary.
    Custom { custom_type: String },
    /// Named enum type. String enums map to a native DB enum; integer enums store as `INTEGER`.
    /// See [`EnumValues`] for the distinction.
    Enum { name: String, values: EnumValues },
}

impl ComplexColumnType {
    /// Returns the base SQL type name for this complex column type, without parameters.
    #[must_use]
    pub fn sql_type(&self) -> &'static str {
        match self {
            ComplexColumnType::Varchar { .. } => "VARCHAR",
            ComplexColumnType::Numeric { .. } => "NUMERIC",
            ComplexColumnType::Char { .. } => "CHAR",
            ComplexColumnType::Custom { .. } => "CUSTOM",
            ComplexColumnType::Enum { .. } => "ENUM",
        }
    }

    /// Convert to human-readable display string
    pub fn to_display_string(&self) -> String {
        match self {
            ComplexColumnType::Varchar { length } => format!("varchar({length})"),
            ComplexColumnType::Numeric { precision, scale } => {
                format!("numeric({precision},{scale})")
            }
            ComplexColumnType::Char { length } => format!("char({length})"),
            ComplexColumnType::Custom { custom_type } => custom_type.to_lowercase(),
            ComplexColumnType::Enum { name, values } => {
                if values.is_integer() {
                    format!("enum<{name}> (integer)")
                } else {
                    format!("enum<{name}>")
                }
            }
        }
    }

    /// Get the default fill value for this type.
    pub fn default_fill_value(&self) -> &'static str {
        match self {
            ComplexColumnType::Numeric { .. } => "0",
            ComplexColumnType::Varchar { .. }
            | ComplexColumnType::Char { .. }
            | ComplexColumnType::Custom { .. }
            | ComplexColumnType::Enum { .. } => "''",
        }
    }
}