Skip to main content

rust_ef/migration/
types.rs

1//! Snapshot and migration data types.
2
3/// Represents a single migration with up/down SQL scripts.
4#[derive(Debug, Clone)]
5pub struct Migration {
6    pub id: String,
7    pub description: String,
8    pub up_sql: String,
9    pub down_sql: String,
10}
11
12/// A snapshot of the entity model at a point in time.
13/// Corresponds to EFCore's `ModelSnapshot`.
14#[derive(Debug, Clone)]
15pub struct ModelSnapshot {
16    pub migration_id: String,
17    pub entity_types: Vec<SnapshotEntityType>,
18}
19
20/// Serialized form of EntityTypeMeta for snapshot storage.
21#[derive(Debug, Clone, Default)]
22pub struct SnapshotEntityType {
23    pub type_name: String,
24    pub table_name: String,
25    pub columns: Vec<SnapshotColumn>,
26    /// Declarative indexes (composite / unique / partial / expression).
27    pub indexes: Vec<SnapshotIndex>,
28    /// Composite foreign keys (single-column FKs live on the column).
29    pub foreign_keys: Vec<SnapshotForeignKey>,
30    /// Table-level CHECK constraints.
31    pub checks: Vec<SnapshotCheck>,
32    /// MySQL table options.
33    pub table_options: SnapshotTableOptions,
34    /// Maps to an existing view (no table DDL).
35    pub is_view: bool,
36    /// Keyless entity (no primary key).
37    pub is_keyless: bool,
38    /// Table comment.
39    pub comment: Option<String>,
40}
41
42/// One key part of a snapshot index.
43#[derive(Debug, Clone, PartialEq, Eq, Default)]
44pub struct SnapshotIndexColumn {
45    /// Column name or raw SQL expression.
46    pub value: String,
47    /// Whether `value` is a raw expression.
48    pub is_expression: bool,
49}
50
51/// A declared index in a model snapshot.
52#[derive(Debug, Clone, PartialEq, Eq, Default)]
53pub struct SnapshotIndex {
54    /// Explicit or derived index name.
55    pub name: String,
56    /// Key parts, in order.
57    pub columns: Vec<SnapshotIndexColumn>,
58    /// UNIQUE index.
59    pub is_unique: bool,
60    /// Rendered partial-index predicate (dialect-specific literals).
61    pub filter: Option<String>,
62    /// Non-key included columns (`INCLUDE`).
63    pub include: Vec<String>,
64}
65
66/// A composite foreign key in a model snapshot.
67#[derive(Debug, Clone, PartialEq, Eq, Default)]
68pub struct SnapshotForeignKey {
69    /// Explicit or derived constraint name.
70    pub name: String,
71    /// Dependent columns.
72    pub columns: Vec<String>,
73    /// Principal table.
74    pub referenced_table: String,
75    /// Principal columns.
76    pub referenced_columns: Vec<String>,
77    /// `ON DELETE` clause.
78    pub on_delete: Option<String>,
79}
80
81/// A CHECK constraint in a model snapshot.
82#[derive(Debug, Clone, PartialEq, Eq, Default)]
83pub struct SnapshotCheck {
84    pub name: String,
85    pub expression: String,
86}
87
88/// MySQL table options in a model snapshot.
89#[derive(Debug, Clone, PartialEq, Eq, Default)]
90pub struct SnapshotTableOptions {
91    pub engine: Option<String>,
92    pub charset: Option<String>,
93    pub collate: Option<String>,
94}
95
96impl SnapshotTableOptions {
97    pub fn is_empty(&self) -> bool {
98        self.engine.is_none() && self.charset.is_none() && self.collate.is_none()
99    }
100}
101
102/// Serialized form of PropertyMeta for snapshot storage.
103#[derive(Debug, Clone, PartialEq, Default)]
104pub struct SnapshotColumn {
105    pub field_name: String,
106    pub column_name: String,
107    pub type_name: String,
108    pub is_primary_key: bool,
109    pub is_required: bool,
110    pub is_foreign_key: bool,
111    pub max_length: Option<usize>,
112    pub is_auto_increment: bool,
113    /// Whether this column is backed by a database sequence (PostgreSQL).
114    pub is_sequence: bool,
115    /// The sequence name when `is_sequence` is true.
116    pub sequence_name: Option<String>,
117    /// Referenced table when `is_foreign_key` is true.
118    pub fk_referenced_table: Option<String>,
119    /// Referenced column when `is_foreign_key` is true.
120    pub fk_referenced_column: Option<String>,
121    /// Non-unique index on this column.
122    pub has_index: bool,
123    /// Unique constraint/index on this column.
124    pub is_unique: bool,
125    /// ON DELETE clause for the FK constraint (e.g. "CASCADE", "SET NULL").
126    /// `None` means no explicit clause (DB default applies).
127    pub fk_on_delete: Option<String>,
128    /// Raw SQL DEFAULT expression (`None` = no default).
129    pub default_value: Option<String>,
130    /// Explicit database type (`HasColumnType`), overrides dialect mapping.
131    pub column_type: Option<String>,
132    /// Decimal/temporal precision.
133    pub precision: Option<u32>,
134    /// Decimal scale.
135    pub scale: Option<u32>,
136    /// Fixed-length string type (`CHAR(n)`).
137    pub is_fixed_length: bool,
138    /// Column collation.
139    pub collation: Option<String>,
140    /// Column comment.
141    pub comment: Option<String>,
142    /// Computed/generated column expression.
143    pub computed_sql: Option<String>,
144    /// Persisted (`STORED`) computed column.
145    pub computed_stored: bool,
146}