Skip to main content

che_orm/
migration.rs

1use std::collections::{BTreeMap, BTreeSet};
2use std::fmt;
3
4use crate::{ColumnSchema, ColumnType, ForeignKey, SchemaSet, TableSchema};
5
6mod postgres_schema;
7#[cfg(feature = "sqlite")]
8pub mod sqlite_executor;
9pub use postgres_schema::{DeferredForeignKey, PostgresOperationPlan, PostgresSchemaEditor};
10#[cfg(feature = "postgres")]
11pub(crate) mod postgres_executor;
12
13/// An owned foreign-key definition in a migration state.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct ForeignKeyState {
16    pub target: String,
17    pub on_delete: Option<String>,
18}
19
20impl ForeignKeyState {
21    /// Returns the structured target stored in the migration snapshot.
22    /// A malformed legacy target returns `None` and is rejected by state
23    /// validation before a migration can be planned.
24    pub fn target_parts(&self) -> Option<(&str, &str)> {
25        parse_reference(&self.target)
26    }
27}
28
29/// An owned column definition in a migration state.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct ColumnState {
32    /// Rust field name. This is distinct from [`Self::name`] when a model
33    /// uses `#[orm(db_column = "...")]`.
34    pub field_name: String,
35    /// Physical SQL column name.
36    pub name: String,
37    pub column_type: ColumnType,
38    pub nullable: bool,
39    pub primary_key: bool,
40    pub unique: bool,
41    pub default: Option<String>,
42    pub check: Option<String>,
43    pub choices: Option<Vec<String>>,
44    pub references: Option<ForeignKeyState>,
45    pub auto_now_add: bool,
46    pub auto_now: bool,
47}
48
49/// An owned table definition in a migration state.
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct TableState {
52    pub name: String,
53    pub columns: Vec<ColumnState>,
54    pub unique_constraints: Vec<Vec<String>>,
55    pub indexes: Vec<Vec<String>>,
56}
57
58/// A named SQLite object which belongs to the migration state but is not an
59/// ORM model. Triggers and views are kept structurally so a fresh database can
60/// be recreated without embedding a whole-schema SQL baseline.
61#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
62pub enum SchemaObjectKind {
63    Table,
64    Trigger,
65    View,
66}
67
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct SchemaObjectState {
70    /// Owning application, used to generate per-app migrations.
71    pub app: String,
72    pub kind: SchemaObjectKind,
73    pub name: String,
74    /// Complete reviewed SQLite CREATE statement.
75    pub sql: String,
76}
77
78/// Stable identity of a model independently from its SQL table name.
79#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
80pub struct ModelKey {
81    pub app: String,
82    pub name: String,
83}
84
85/// A model and its current table definition in a historical project state.
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct ModelState {
88    pub key: ModelKey,
89    pub table: TableState,
90}
91
92/// A deterministic difference between historical and current model state.
93/// Field renames keep their physical SQL column when `db_column` preserves it.
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub enum StateChange {
96    CreateModel(ModelState),
97    DeleteModel(ModelState),
98    RenameTable {
99        key: ModelKey,
100        old_name: String,
101        new_name: String,
102    },
103    AddColumn {
104        key: ModelKey,
105        table: String,
106        column: ColumnState,
107    },
108    RemoveColumn {
109        key: ModelKey,
110        table: String,
111        column: ColumnState,
112    },
113    AlterColumn {
114        key: ModelKey,
115        table: String,
116        old_column: ColumnState,
117        new_column: ColumnState,
118    },
119    AddIndex {
120        key: ModelKey,
121        table: String,
122        columns: Vec<String>,
123    },
124    RemoveIndex {
125        key: ModelKey,
126        table: String,
127        columns: Vec<String>,
128    },
129    AddUniqueConstraint {
130        key: ModelKey,
131        table: String,
132        columns: Vec<String>,
133    },
134    RemoveUniqueConstraint {
135        key: ModelKey,
136        table: String,
137        columns: Vec<String>,
138    },
139    CreateSchemaObject(SchemaObjectState),
140    DeleteSchemaObject(SchemaObjectState),
141}
142
143impl StateChange {
144    /// Returns the application that owns the model being changed.
145    pub fn app(&self) -> &str {
146        match self {
147            Self::CreateModel(model) | Self::DeleteModel(model) => &model.key.app,
148            Self::RenameTable { key, .. }
149            | Self::AddColumn { key, .. }
150            | Self::RemoveColumn { key, .. }
151            | Self::AlterColumn { key, .. }
152            | Self::AddIndex { key, .. }
153            | Self::RemoveIndex { key, .. }
154            | Self::AddUniqueConstraint { key, .. }
155            | Self::RemoveUniqueConstraint { key, .. } => &key.app,
156            Self::CreateSchemaObject(object) | Self::DeleteSchemaObject(object) => &object.app,
157        }
158    }
159
160    /// Converts a state difference into the forward-only operation that
161    /// reproduces it. `ModelKey` is used while calculating the difference;
162    /// the resulting operation carries the database state it needs to apply.
163    pub fn into_operation(self) -> MigrationOperation {
164        match self {
165            Self::CreateModel(model) => MigrationOperation::CreateModel { model },
166            Self::DeleteModel(model) => MigrationOperation::DropTable {
167                name: model.table.name,
168            },
169            Self::RenameTable {
170                old_name, new_name, ..
171            } => MigrationOperation::RenameTable { old_name, new_name },
172            Self::AddColumn { table, column, .. } => {
173                MigrationOperation::AddColumn { table, column }
174            }
175            Self::RemoveColumn { table, column, .. } => MigrationOperation::RemoveColumn {
176                table,
177                column: column.name,
178            },
179            Self::AlterColumn {
180                table,
181                old_column,
182                new_column,
183                ..
184            } => MigrationOperation::AlterColumn {
185                table,
186                old_column,
187                new_column,
188            },
189            Self::AddIndex { table, columns, .. } => {
190                MigrationOperation::AddIndex { table, columns }
191            }
192            Self::RemoveIndex { table, columns, .. } => {
193                MigrationOperation::RemoveIndex { table, columns }
194            }
195            Self::AddUniqueConstraint { table, columns, .. } => {
196                MigrationOperation::AddUniqueConstraint { table, columns }
197            }
198            Self::RemoveUniqueConstraint { table, columns, .. } => {
199                MigrationOperation::RemoveUniqueConstraint { table, columns }
200            }
201            Self::CreateSchemaObject(object) => MigrationOperation::CreateSchemaObject { object },
202            Self::DeleteSchemaObject(object) => MigrationOperation::DropSchemaObject {
203                kind: object.kind,
204                name: object.name,
205            },
206        }
207    }
208}
209
210/// Converts a deterministic schema difference into forward migration
211/// operations, preserving the order chosen by [`ProjectState::diff`].
212pub fn operations_from_changes(
213    changes: impl IntoIterator<Item = StateChange>,
214) -> Vec<MigrationOperation> {
215    changes
216        .into_iter()
217        .map(StateChange::into_operation)
218        .collect()
219}
220
221/// The fully owned schema produced by replaying migrations.
222#[derive(Debug, Default, Clone, PartialEq, Eq)]
223pub struct ProjectState {
224    tables: Vec<TableState>,
225    model_keys: BTreeMap<String, ModelKey>,
226    objects: Vec<SchemaObjectState>,
227}
228
229impl ProjectState {
230    /// Builds migration state from model-generated schema metadata.
231    pub fn from_schema(schema: &SchemaSet) -> Self {
232        Self {
233            tables: schema.tables().iter().map(TableState::from).collect(),
234            model_keys: BTreeMap::new(),
235            objects: Vec::new(),
236        }
237    }
238
239    /// Builds a historical state for models owned by one application. Model
240    /// identity remains stable when a subsequent migration renames its table.
241    pub fn from_app_schema(app: impl Into<String>, schema: &SchemaSet) -> Self {
242        let app = app.into();
243        let tables: Vec<_> = schema.tables().iter().map(TableState::from).collect();
244        let model_keys = tables
245            .iter()
246            .zip(schema.model_names())
247            .map(|(table, model_name)| {
248                (
249                    table.name.clone(),
250                    ModelKey {
251                        app: app.clone(),
252                        name: (*model_name).into(),
253                    },
254                )
255            })
256            .collect();
257        Self {
258            tables,
259            model_keys,
260            objects: Vec::new(),
261        }
262    }
263
264    /// Combines schemas while retaining each application's model identity.
265    pub fn from_app_schemas<'app, 'schema>(
266        schemas: impl IntoIterator<Item = (&'app str, &'schema SchemaSet)>,
267    ) -> Result<Self, MigrationError> {
268        let mut state = Self::default();
269        let mut sync_registrations = Vec::new();
270        for (app, schema) in schemas {
271            sync_registrations.extend_from_slice(schema.sync_models());
272            let app_state = Self::from_app_schema(app, schema);
273            let mut keys: BTreeSet<_> = state.model_keys.values().cloned().collect();
274            for key in app_state.model_keys.values() {
275                if !keys.insert(key.clone()) {
276                    return Err(MigrationError::InvalidState(format!(
277                        "duplicate model {key:?}"
278                    )));
279                }
280            }
281            for table in app_state.tables {
282                if state
283                    .tables
284                    .iter()
285                    .any(|current| current.name == table.name)
286                {
287                    return Err(MigrationError::DuplicateTable(table.name));
288                }
289                state.tables.push(table);
290            }
291            state.model_keys.extend(app_state.model_keys);
292        }
293        for operation in crate::sync_migration_operations(&sync_registrations)? {
294            operation.state_forwards(&mut state)?;
295        }
296        Ok(state)
297    }
298
299    /// Returns tables in their current schema order.
300    pub fn tables(&self) -> &[TableState] {
301        &self.tables
302    }
303
304    /// Returns named non-model SQLite objects in migration order.
305    pub fn objects(&self) -> &[SchemaObjectState] {
306        &self.objects
307    }
308
309    /// Adds an application-owned SQLite object to this state.
310    pub fn add_schema_object(&mut self, object: SchemaObjectState) -> Result<(), MigrationError> {
311        MigrationOperation::CreateSchemaObject { object }.state_forwards(self)
312    }
313
314    /// Checks structural invariants without consulting a live database.
315    pub fn validate(&self) -> Result<(), MigrationError> {
316        let mut names = BTreeSet::new();
317        for table in &self.tables {
318            if !names.insert(&table.name) {
319                return Err(MigrationError::DuplicateTable(table.name.clone()));
320            }
321            let mut columns = BTreeSet::new();
322            for column in &table.columns {
323                if !columns.insert(&column.name) {
324                    return Err(MigrationError::DuplicateColumn {
325                        table: table.name.clone(),
326                        column: column.name.clone(),
327                    });
328                }
329                if let Some(reference) = &column.references
330                    && parse_reference(&reference.target).is_none()
331                {
332                    return Err(MigrationError::InvalidState(format!(
333                        "invalid reference {}",
334                        reference.target
335                    )));
336                }
337            }
338            for constraint in table.indexes.iter().chain(&table.unique_constraints) {
339                validate_constraint_columns(table, constraint)?;
340            }
341        }
342        let mut objects = BTreeSet::new();
343        for object in &self.objects {
344            validate_schema_object(object)?;
345            if object.kind == SchemaObjectKind::Table
346                && self.tables.iter().any(|table| table.name == object.name)
347            {
348                return Err(MigrationError::InvalidState(format!(
349                    "schema table {} conflicts with a model table",
350                    object.name
351                )));
352            }
353            if !objects.insert((object.kind, &object.name)) {
354                return Err(MigrationError::InvalidState(format!(
355                    "duplicate {:?} {}",
356                    object.kind, object.name
357                )));
358            }
359        }
360        Ok(())
361    }
362
363    /// Returns models in table order, omitting legacy tables with no known
364    /// application/model identity.
365    pub fn models(&self) -> Vec<ModelState> {
366        self.tables
367            .iter()
368            .filter_map(|table| {
369                self.model_keys
370                    .get(&table.name)
371                    .cloned()
372                    .map(|key| ModelState {
373                        key,
374                        table: table.clone(),
375                    })
376            })
377            .collect()
378    }
379
380    /// Compares historical state with current model metadata without touching
381    /// the database. Both states must have complete ModelKey coverage.
382    pub fn diff(&self, desired: &Self) -> Result<Vec<StateChange>, MigrationError> {
383        self.validate_model_coverage()?;
384        desired.validate_model_coverage()?;
385        let current: BTreeMap<_, _> = self
386            .models()
387            .into_iter()
388            .map(|model| (model.key.clone(), model))
389            .collect();
390        let wanted: BTreeMap<_, _> = desired
391            .models()
392            .into_iter()
393            .map(|model| (model.key.clone(), model))
394            .collect();
395        let mut model_changes = Vec::new();
396        for (key, model) in &current {
397            if !wanted.contains_key(key) {
398                model_changes.push(StateChange::DeleteModel(model.clone()));
399            }
400        }
401        for (key, model) in &wanted {
402            let Some(old) = current.get(key) else {
403                model_changes.push(StateChange::CreateModel(model.clone()));
404                continue;
405            };
406            let old_table = &old.table;
407            let new_table = &model.table;
408            if old_table.name != new_table.name {
409                model_changes.push(StateChange::RenameTable {
410                    key: key.clone(),
411                    old_name: old_table.name.clone(),
412                    new_name: new_table.name.clone(),
413                });
414            }
415            let old_columns: BTreeMap<_, _> = old_table
416                .columns
417                .iter()
418                .map(|column| (column.name.clone(), column))
419                .collect();
420            let new_columns: BTreeMap<_, _> = new_table
421                .columns
422                .iter()
423                .map(|column| (column.name.clone(), column))
424                .collect();
425            for (name, column) in &old_columns {
426                if !new_columns.contains_key(name) {
427                    model_changes.push(StateChange::RemoveColumn {
428                        key: key.clone(),
429                        table: new_table.name.clone(),
430                        column: (*column).clone(),
431                    });
432                }
433            }
434            for (name, column) in &new_columns {
435                match old_columns.get(name) {
436                    None => model_changes.push(StateChange::AddColumn {
437                        key: key.clone(),
438                        table: new_table.name.clone(),
439                        column: (*column).clone(),
440                    }),
441                    Some(old_column) if !old_column.same_database_definition(column) => {
442                        model_changes.push(StateChange::AlterColumn {
443                            key: key.clone(),
444                            table: new_table.name.clone(),
445                            old_column: (*old_column).clone(),
446                            new_column: (*column).clone(),
447                        })
448                    }
449                    _ => {}
450                }
451            }
452            diff_constraints(
453                &mut model_changes,
454                key,
455                &new_table.name,
456                &old_table.indexes,
457                &new_table.indexes,
458                true,
459            );
460            diff_constraints(
461                &mut model_changes,
462                key,
463                &new_table.name,
464                &old_table.unique_constraints,
465                &new_table.unique_constraints,
466                false,
467            );
468        }
469        let current_objects: BTreeMap<_, _> = self
470            .objects
471            .iter()
472            .map(|object| ((object.kind, object.name.clone()), object))
473            .collect();
474        let wanted_objects: BTreeMap<_, _> = desired
475            .objects
476            .iter()
477            .map(|object| ((object.kind, object.name.clone()), object))
478            .collect();
479        let changed_tables = changed_tables(&model_changes);
480        let mut before = Vec::new();
481        let mut after = Vec::new();
482        for (key, object) in &current_objects {
483            let changed = schema_object_depends_on(object, &changed_tables);
484            match wanted_objects.get(key) {
485                None => before.push(StateChange::DeleteSchemaObject((*object).clone())),
486                Some(_) if changed => {
487                    before.push(StateChange::DeleteSchemaObject((*object).clone()))
488                }
489                Some(wanted) if *wanted != *object => {
490                    before.push(StateChange::DeleteSchemaObject((*object).clone()))
491                }
492                _ => {}
493            }
494        }
495        for (key, object) in &wanted_objects {
496            let changed = schema_object_depends_on(object, &changed_tables);
497            match current_objects.get(key) {
498                None => after.push(StateChange::CreateSchemaObject((*object).clone())),
499                Some(_) if changed => {
500                    after.push(StateChange::CreateSchemaObject((*object).clone()))
501                }
502                Some(current) if *current != *object => {
503                    after.push(StateChange::CreateSchemaObject((*object).clone()))
504                }
505                _ => {}
506            }
507        }
508        let mut changes = before;
509        changes.extend(model_changes);
510        changes.extend(after);
511        Ok(changes)
512    }
513
514    fn validate_model_coverage(&self) -> Result<(), MigrationError> {
515        if self.model_keys.len() != self.tables.len() {
516            return Err(MigrationError::InvalidState(
517                "migration state is missing ModelKey metadata".into(),
518            ));
519        }
520        self.validate()
521    }
522}
523
524fn changed_tables(changes: &[StateChange]) -> BTreeSet<String> {
525    changes
526        .iter()
527        .flat_map(|change| match change {
528            StateChange::CreateModel(model) | StateChange::DeleteModel(model) => {
529                vec![model.table.name.clone()]
530            }
531            StateChange::RenameTable {
532                old_name, new_name, ..
533            } => vec![old_name.clone(), new_name.clone()],
534            StateChange::AddColumn { table, .. }
535            | StateChange::RemoveColumn { table, .. }
536            | StateChange::AlterColumn { table, .. }
537            | StateChange::AddIndex { table, .. }
538            | StateChange::RemoveIndex { table, .. }
539            | StateChange::AddUniqueConstraint { table, .. }
540            | StateChange::RemoveUniqueConstraint { table, .. } => vec![table.clone()],
541            StateChange::CreateSchemaObject(_) | StateChange::DeleteSchemaObject(_) => vec![],
542        })
543        .collect()
544}
545
546fn validate_schema_object(object: &SchemaObjectState) -> Result<(), MigrationError> {
547    if object.app.trim().is_empty() || object.name.trim().is_empty() || object.sql.trim().is_empty()
548    {
549        return Err(MigrationError::InvalidState(
550            "schema object app, name, and SQL must not be empty".into(),
551        ));
552    }
553    let (kind, name) = parse_schema_object_header(&object.sql).ok_or_else(|| {
554        MigrationError::InvalidState(format!(
555            "schema object {:?} {} SQL must begin with CREATE {:?} {}",
556            object.kind, object.name, object.kind, object.name
557        ))
558    })?;
559    if kind != object.kind || name != object.name {
560        return Err(MigrationError::InvalidState(format!(
561            "schema object SQL creates {:?} {name}, expected {:?} {}",
562            kind, object.kind, object.name
563        )));
564    }
565    Ok(())
566}
567
568fn schema_object_depends_on(object: &SchemaObjectState, tables: &BTreeSet<String>) -> bool {
569    tables.iter().any(|table| {
570        object
571            .sql
572            .split(|character: char| !character.is_ascii_alphanumeric() && character != '_')
573            .any(|identifier| identifier == table)
574    })
575}
576
577fn parse_schema_object_header(sql: &str) -> Option<(SchemaObjectKind, String)> {
578    let mut words = sql.split_whitespace();
579    if !words.next()?.eq_ignore_ascii_case("CREATE") {
580        return None;
581    }
582    let mut word = words.next()?;
583    if word.eq_ignore_ascii_case("TEMP") || word.eq_ignore_ascii_case("TEMPORARY") {
584        word = words.next()?;
585    }
586    let kind = if word.eq_ignore_ascii_case("TABLE") {
587        SchemaObjectKind::Table
588    } else if word.eq_ignore_ascii_case("TRIGGER") {
589        SchemaObjectKind::Trigger
590    } else if word.eq_ignore_ascii_case("VIEW") {
591        SchemaObjectKind::View
592    } else {
593        return None;
594    };
595    let mut name = words.next()?;
596    if name.eq_ignore_ascii_case("IF") {
597        if !words.next()?.eq_ignore_ascii_case("NOT")
598            || !words.next()?.eq_ignore_ascii_case("EXISTS")
599        {
600            return None;
601        }
602        name = words.next()?;
603    }
604    Some((
605        kind,
606        name.trim_matches(|character| matches!(character, '"' | '`' | '[' | ']'))
607            .to_owned(),
608    ))
609}
610
611fn diff_constraints(
612    changes: &mut Vec<StateChange>,
613    key: &ModelKey,
614    table: &str,
615    old: &[Vec<String>],
616    new: &[Vec<String>],
617    index: bool,
618) {
619    for columns in old {
620        if !new.contains(columns) {
621            changes.push(if index {
622                StateChange::RemoveIndex {
623                    key: key.clone(),
624                    table: table.into(),
625                    columns: columns.clone(),
626                }
627            } else {
628                StateChange::RemoveUniqueConstraint {
629                    key: key.clone(),
630                    table: table.into(),
631                    columns: columns.clone(),
632                }
633            });
634        }
635    }
636    for columns in new {
637        if !old.contains(columns) {
638            changes.push(if index {
639                StateChange::AddIndex {
640                    key: key.clone(),
641                    table: table.into(),
642                    columns: columns.clone(),
643                }
644            } else {
645                StateChange::AddUniqueConstraint {
646                    key: key.clone(),
647                    table: table.into(),
648                    columns: columns.clone(),
649                }
650            });
651        }
652    }
653}
654
655impl From<&SchemaSet> for ProjectState {
656    fn from(schema: &SchemaSet) -> Self {
657        Self::from_schema(schema)
658    }
659}
660
661impl From<&ColumnSchema> for ColumnState {
662    fn from(column: &ColumnSchema) -> Self {
663        Self {
664            field_name: column.field_name.into(),
665            name: column.name.into(),
666            column_type: column.column_type,
667            nullable: column.nullable,
668            primary_key: column.primary_key,
669            unique: column.unique,
670            default: column.default.map(Into::into),
671            check: column.check.map(Into::into),
672            choices: column
673                .choices
674                .as_ref()
675                .map(|choices| choices.iter().map(|choice| (*choice).into()).collect()),
676            references: column.references.as_ref().map(ForeignKeyState::from),
677            auto_now_add: column.auto_now_add,
678            auto_now: column.auto_now,
679        }
680    }
681}
682
683impl ColumnState {
684    fn same_database_definition(&self, other: &Self) -> bool {
685        self.name == other.name
686            && self.column_type == other.column_type
687            && self.nullable == other.nullable
688            && self.primary_key == other.primary_key
689            && self.unique == other.unique
690            && self.default == other.default
691            && self.check == other.check
692            && self.choices == other.choices
693            && self.references == other.references
694            && self.auto_now_add == other.auto_now_add
695            && self.auto_now == other.auto_now
696    }
697}
698
699impl From<&ForeignKey> for ForeignKeyState {
700    fn from(foreign_key: &ForeignKey) -> Self {
701        Self {
702            target: foreign_key.target().into(),
703            on_delete: foreign_key.on_delete().map(Into::into),
704        }
705    }
706}
707
708impl From<&TableSchema> for TableState {
709    fn from(table: &TableSchema) -> Self {
710        Self {
711            name: table.name.into(),
712            columns: table.columns.iter().map(ColumnState::from).collect(),
713            unique_constraints: table
714                .unique_constraints
715                .iter()
716                .map(|columns| columns.iter().map(|column| (*column).into()).collect())
717                .collect(),
718            indexes: table
719                .indexes
720                .iter()
721                .map(|columns| columns.iter().map(|column| (*column).into()).collect())
722                .collect(),
723        }
724    }
725}
726
727/// A stable migration name, scoped to an application.
728#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
729pub struct MigrationId {
730    pub app: String,
731    pub name: String,
732}
733
734/// A declarative schema or SQL migration.
735#[derive(Debug, Clone, PartialEq, Eq)]
736pub struct Migration {
737    pub id: MigrationId,
738    pub dependencies: Vec<MigrationId>,
739    pub operations: Vec<MigrationOperation>,
740    /// A legacy user-supplied checksum or a versioned checksum calculated by
741    /// [`Migration::new`]. New migrations should use the constructor.
742    pub checksum: String,
743}
744
745impl Migration {
746    /// Creates a migration with a checksum derived from its complete,
747    /// declarative definition.
748    pub fn new(
749        id: MigrationId,
750        dependencies: Vec<MigrationId>,
751        operations: Vec<MigrationOperation>,
752    ) -> Self {
753        let mut migration = Self {
754            id,
755            dependencies,
756            operations,
757            checksum: String::new(),
758        };
759        migration.checksum = migration.calculated_checksum();
760        migration
761    }
762
763    /// Returns the versioned checksum for this migration's id, dependencies
764    /// and operations. The encoding preserves operation order and normalizes
765    /// dependency order.
766    pub fn calculated_checksum(&self) -> String {
767        let version = if self.has_distinct_field_names() {
768            "v2"
769        } else {
770            "v1"
771        };
772        let mut bytes = Vec::new();
773        encode_string(&mut bytes, "che-orm-migration");
774        encode_string(&mut bytes, version);
775        encode_migration_id(&mut bytes, &self.id);
776        let mut dependencies = self.dependencies.clone();
777        dependencies.sort();
778        encode_usize(&mut bytes, dependencies.len());
779        for dependency in &dependencies {
780            encode_migration_id(&mut bytes, dependency);
781        }
782        encode_usize(&mut bytes, self.operations.len());
783        for operation in &self.operations {
784            encode_operation(&mut bytes, operation, version == "v2");
785        }
786        format!("{version}:{:016x}", fnv1a64(&bytes))
787    }
788
789    fn validates_checksum(&self) -> bool {
790        !matches!(self.checksum.get(..3), Some("v1:") | Some("v2:"))
791            || self.checksum == self.calculated_checksum()
792    }
793
794    fn has_distinct_field_names(&self) -> bool {
795        self.operations
796            .iter()
797            .any(operation_has_distinct_field_names)
798    }
799}
800
801/// Renders a self-contained Rust migration definition. The output deliberately
802/// calls [`Migration::new`], so its checksum always describes the source that
803/// will be compiled and registered.
804pub fn render_migration_rust(migration: &Migration) -> String {
805    let mut source = String::from(
806        "#[allow(unused_imports)]\nuse che_orm::{ColumnState, ColumnType, ForeignKeyState, Migration, MigrationId, MigrationOperation, ModelKey, ModelState, SchemaObjectKind, SchemaObjectState, TableState};\n\n",
807    );
808    source.push_str("pub fn migration() -> Migration {\n    Migration::new(\n");
809    source.push_str(&format!(
810        "        {},\n",
811        render_migration_id(&migration.id)
812    ));
813    source.push_str("        vec![");
814    for (index, dependency) in migration.dependencies.iter().enumerate() {
815        if index > 0 {
816            source.push_str(", ");
817        }
818        source.push_str(&render_migration_id(dependency));
819    }
820    source.push_str("],\n        vec![\n");
821    for operation in &migration.operations {
822        source.push_str("            ");
823        source.push_str(&render_operation(operation, 12));
824        source.push_str(",\n");
825    }
826    source.push_str("        ],\n    )\n}\n");
827    source
828}
829
830fn render_string(value: &str) -> String {
831    format!("{value:?}.into()")
832}
833
834fn render_migration_id(id: &MigrationId) -> String {
835    format!(
836        "MigrationId {{ app: {}, name: {} }}",
837        render_string(&id.app),
838        render_string(&id.name)
839    )
840}
841
842fn render_strings(values: &[String]) -> String {
843    format!(
844        "vec![{}]",
845        values
846            .iter()
847            .map(|value| render_string(value))
848            .collect::<Vec<_>>()
849            .join(", ")
850    )
851}
852
853fn render_column_type(column_type: ColumnType) -> &'static str {
854    match column_type {
855        ColumnType::Integer => "ColumnType::Integer",
856        ColumnType::Real => "ColumnType::Real",
857        ColumnType::Text => "ColumnType::Text",
858        ColumnType::Binary => "ColumnType::Binary",
859        ColumnType::Boolean => "ColumnType::Boolean",
860        ColumnType::DateTime => "ColumnType::DateTime",
861        ColumnType::Uuid => "ColumnType::Uuid",
862    }
863}
864
865fn render_option_string(value: &Option<String>) -> String {
866    match value {
867        Some(value) => format!("Some({})", render_string(value)),
868        None => "None".into(),
869    }
870}
871
872fn render_column(column: &ColumnState) -> String {
873    let choices = match &column.choices {
874        Some(choices) => format!("Some({})", render_strings(choices)),
875        None => "None".into(),
876    };
877    let references = match &column.references {
878        Some(reference) => format!(
879            "Some(ForeignKeyState {{ target: {}, on_delete: {} }})",
880            render_string(&reference.target),
881            render_option_string(&reference.on_delete)
882        ),
883        None => "None".into(),
884    };
885    format!(
886        "ColumnState {{ field_name: {}, name: {}, column_type: {}, nullable: {}, primary_key: {}, unique: {}, default: {}, check: {}, choices: {}, references: {}, auto_now_add: {}, auto_now: {} }}",
887        render_string(&column.field_name),
888        render_string(&column.name),
889        render_column_type(column.column_type),
890        column.nullable,
891        column.primary_key,
892        column.unique,
893        render_option_string(&column.default),
894        render_option_string(&column.check),
895        choices,
896        references,
897        column.auto_now_add,
898        column.auto_now,
899    )
900}
901
902fn render_table(table: &TableState) -> String {
903    let columns = table
904        .columns
905        .iter()
906        .map(render_column)
907        .collect::<Vec<_>>()
908        .join(", ");
909    let constraints = |constraints: &[Vec<String>]| {
910        format!(
911            "vec![{}]",
912            constraints
913                .iter()
914                .map(|columns| render_strings(columns))
915                .collect::<Vec<_>>()
916                .join(", ")
917        )
918    };
919    format!(
920        "TableState {{ name: {}, columns: vec![{}], unique_constraints: {}, indexes: {} }}",
921        render_string(&table.name),
922        columns,
923        constraints(&table.unique_constraints),
924        constraints(&table.indexes)
925    )
926}
927
928fn render_model(model: &ModelState) -> String {
929    format!(
930        "ModelState {{ key: ModelKey {{ app: {}, name: {} }}, table: {} }}",
931        render_string(&model.key.app),
932        render_string(&model.key.name),
933        render_table(&model.table)
934    )
935}
936
937fn render_schema_object_kind(kind: SchemaObjectKind) -> &'static str {
938    match kind {
939        SchemaObjectKind::Table => "SchemaObjectKind::Table",
940        SchemaObjectKind::Trigger => "SchemaObjectKind::Trigger",
941        SchemaObjectKind::View => "SchemaObjectKind::View",
942    }
943}
944
945fn render_schema_object(object: &SchemaObjectState) -> String {
946    format!(
947        "SchemaObjectState {{ app: {}, kind: {}, name: {}, sql: {} }}",
948        render_string(&object.app),
949        render_schema_object_kind(object.kind),
950        render_string(&object.name),
951        render_string(&object.sql),
952    )
953}
954
955fn render_operation(operation: &MigrationOperation, _indent: usize) -> String {
956    match operation {
957        MigrationOperation::CreateModel { model } => format!(
958            "MigrationOperation::CreateModel {{ model: {} }}",
959            render_model(model)
960        ),
961        MigrationOperation::CreateTable { table } => format!(
962            "MigrationOperation::CreateTable {{ table: {} }}",
963            render_table(table)
964        ),
965        MigrationOperation::DropTable { name } => format!(
966            "MigrationOperation::DropTable {{ name: {} }}",
967            render_string(name)
968        ),
969        MigrationOperation::AddColumn { table, column } => format!(
970            "MigrationOperation::AddColumn {{ table: {}, column: {} }}",
971            render_string(table),
972            render_column(column)
973        ),
974        MigrationOperation::RemoveColumn { table, column } => format!(
975            "MigrationOperation::RemoveColumn {{ table: {}, column: {} }}",
976            render_string(table),
977            render_string(column)
978        ),
979        MigrationOperation::RenameTable { old_name, new_name } => format!(
980            "MigrationOperation::RenameTable {{ old_name: {}, new_name: {} }}",
981            render_string(old_name),
982            render_string(new_name)
983        ),
984        MigrationOperation::RenameColumn {
985            table,
986            old_name,
987            new_name,
988        } => format!(
989            "MigrationOperation::RenameColumn {{ table: {}, old_name: {}, new_name: {} }}",
990            render_string(table),
991            render_string(old_name),
992            render_string(new_name)
993        ),
994        MigrationOperation::AlterColumn {
995            table,
996            old_column,
997            new_column,
998        } => format!(
999            "MigrationOperation::AlterColumn {{ table: {}, old_column: {}, new_column: {} }}",
1000            render_string(table),
1001            render_column(old_column),
1002            render_column(new_column)
1003        ),
1004        MigrationOperation::AddIndex { table, columns } => format!(
1005            "MigrationOperation::AddIndex {{ table: {}, columns: {} }}",
1006            render_string(table),
1007            render_strings(columns)
1008        ),
1009        MigrationOperation::RemoveIndex { table, columns } => format!(
1010            "MigrationOperation::RemoveIndex {{ table: {}, columns: {} }}",
1011            render_string(table),
1012            render_strings(columns)
1013        ),
1014        MigrationOperation::AddUniqueConstraint { table, columns } => format!(
1015            "MigrationOperation::AddUniqueConstraint {{ table: {}, columns: {} }}",
1016            render_string(table),
1017            render_strings(columns)
1018        ),
1019        MigrationOperation::RemoveUniqueConstraint { table, columns } => format!(
1020            "MigrationOperation::RemoveUniqueConstraint {{ table: {}, columns: {} }}",
1021            render_string(table),
1022            render_strings(columns)
1023        ),
1024        MigrationOperation::BackfillColumn {
1025            table,
1026            column,
1027            value,
1028        } => format!(
1029            "MigrationOperation::BackfillColumn {{ table: {}, column: {}, value: {} }}",
1030            render_string(table),
1031            render_string(column),
1032            render_string(value)
1033        ),
1034        MigrationOperation::CreateSchemaObject { object } => format!(
1035            "MigrationOperation::CreateSchemaObject {{ object: {} }}",
1036            render_schema_object(object)
1037        ),
1038        MigrationOperation::DropSchemaObject { kind, name } => format!(
1039            "MigrationOperation::DropSchemaObject {{ kind: {}, name: {} }}",
1040            render_schema_object_kind(*kind),
1041            render_string(name)
1042        ),
1043        MigrationOperation::RunSql {
1044            forward,
1045            reverse,
1046            state_operations,
1047        } => format!(
1048            "MigrationOperation::RunSql {{ forward: {}, reverse: {}, state_operations: vec![{}] }}",
1049            render_string(forward),
1050            render_option_string(reverse),
1051            state_operations
1052                .iter()
1053                .map(|operation| render_operation(operation, 0))
1054                .collect::<Vec<_>>()
1055                .join(", ")
1056        ),
1057    }
1058}
1059
1060/// A migration state transition.
1061#[derive(Debug, Clone, PartialEq, Eq)]
1062pub enum MigrationOperation {
1063    CreateModel {
1064        model: ModelState,
1065    },
1066    CreateTable {
1067        table: TableState,
1068    },
1069    DropTable {
1070        name: String,
1071    },
1072    AddColumn {
1073        table: String,
1074        column: ColumnState,
1075    },
1076    RemoveColumn {
1077        table: String,
1078        column: String,
1079    },
1080    RenameTable {
1081        old_name: String,
1082        new_name: String,
1083    },
1084    RenameColumn {
1085        table: String,
1086        old_name: String,
1087        new_name: String,
1088    },
1089    AlterColumn {
1090        table: String,
1091        old_column: ColumnState,
1092        new_column: ColumnState,
1093    },
1094    AddIndex {
1095        table: String,
1096        columns: Vec<String>,
1097    },
1098    RemoveIndex {
1099        table: String,
1100        columns: Vec<String>,
1101    },
1102    AddUniqueConstraint {
1103        table: String,
1104        columns: Vec<String>,
1105    },
1106    RemoveUniqueConstraint {
1107        table: String,
1108        columns: Vec<String>,
1109    },
1110    /// Updates only existing NULL values before a later schema constraint.
1111    /// `value` is a reviewed SQLite expression, for example `"'unknown'"`
1112    /// or `"coalesce(title, 'untitled')"`.
1113    BackfillColumn {
1114        table: String,
1115        column: String,
1116        value: String,
1117    },
1118    CreateSchemaObject {
1119        object: SchemaObjectState,
1120    },
1121    DropSchemaObject {
1122        kind: SchemaObjectKind,
1123        name: String,
1124    },
1125    RunSql {
1126        forward: String,
1127        reverse: Option<String>,
1128        state_operations: Vec<MigrationOperation>,
1129    },
1130}
1131
1132fn operation_has_distinct_field_names(operation: &MigrationOperation) -> bool {
1133    match operation {
1134        MigrationOperation::CreateModel { model } => model
1135            .table
1136            .columns
1137            .iter()
1138            .any(|column| column.field_name != column.name),
1139        MigrationOperation::CreateTable { table } => table
1140            .columns
1141            .iter()
1142            .any(|column| column.field_name != column.name),
1143        MigrationOperation::AddColumn { column, .. } => column.field_name != column.name,
1144        MigrationOperation::AlterColumn {
1145            old_column,
1146            new_column,
1147            ..
1148        } => old_column.field_name != old_column.name || new_column.field_name != new_column.name,
1149        MigrationOperation::RunSql {
1150            state_operations, ..
1151        } => state_operations
1152            .iter()
1153            .any(operation_has_distinct_field_names),
1154        MigrationOperation::DropTable { .. }
1155        | MigrationOperation::RemoveColumn { .. }
1156        | MigrationOperation::RenameTable { .. }
1157        | MigrationOperation::RenameColumn { .. }
1158        | MigrationOperation::AddIndex { .. }
1159        | MigrationOperation::RemoveIndex { .. }
1160        | MigrationOperation::AddUniqueConstraint { .. }
1161        | MigrationOperation::RemoveUniqueConstraint { .. }
1162        | MigrationOperation::BackfillColumn { .. }
1163        | MigrationOperation::CreateSchemaObject { .. }
1164        | MigrationOperation::DropSchemaObject { .. } => false,
1165    }
1166}
1167
1168fn encode_usize(bytes: &mut Vec<u8>, value: usize) {
1169    bytes.extend_from_slice(&(value as u64).to_be_bytes());
1170}
1171
1172fn encode_string(bytes: &mut Vec<u8>, value: &str) {
1173    encode_usize(bytes, value.len());
1174    bytes.extend_from_slice(value.as_bytes());
1175}
1176
1177fn encode_bool(bytes: &mut Vec<u8>, value: bool) {
1178    bytes.push(u8::from(value));
1179}
1180
1181fn encode_option_string(bytes: &mut Vec<u8>, value: &Option<String>) {
1182    match value {
1183        Some(value) => {
1184            bytes.push(1);
1185            encode_string(bytes, value);
1186        }
1187        None => bytes.push(0),
1188    }
1189}
1190
1191fn encode_migration_id(bytes: &mut Vec<u8>, id: &MigrationId) {
1192    encode_string(bytes, &id.app);
1193    encode_string(bytes, &id.name);
1194}
1195
1196fn encode_column(bytes: &mut Vec<u8>, column: &ColumnState, include_field_name: bool) {
1197    if include_field_name {
1198        encode_string(bytes, &column.field_name);
1199    }
1200    encode_string(bytes, &column.name);
1201    bytes.push(match column.column_type {
1202        ColumnType::Integer => 0,
1203        ColumnType::Text => 1,
1204        ColumnType::Binary => 2,
1205        ColumnType::Boolean => 3,
1206        ColumnType::DateTime => 4,
1207        ColumnType::Real => 5,
1208        ColumnType::Uuid => 6,
1209    });
1210    encode_bool(bytes, column.nullable);
1211    encode_bool(bytes, column.primary_key);
1212    encode_bool(bytes, column.unique);
1213    encode_option_string(bytes, &column.default);
1214    encode_option_string(bytes, &column.check);
1215    match &column.choices {
1216        Some(choices) => {
1217            bytes.push(1);
1218            encode_usize(bytes, choices.len());
1219            for choice in choices {
1220                encode_string(bytes, choice);
1221            }
1222        }
1223        None => bytes.push(0),
1224    }
1225    match &column.references {
1226        Some(reference) => {
1227            bytes.push(1);
1228            encode_string(bytes, &reference.target);
1229            encode_option_string(bytes, &reference.on_delete);
1230        }
1231        None => bytes.push(0),
1232    }
1233    encode_bool(bytes, column.auto_now_add);
1234    encode_bool(bytes, column.auto_now);
1235}
1236
1237fn encode_table(bytes: &mut Vec<u8>, table: &TableState, include_field_name: bool) {
1238    encode_string(bytes, &table.name);
1239    encode_usize(bytes, table.columns.len());
1240    for column in &table.columns {
1241        encode_column(bytes, column, include_field_name);
1242    }
1243    for constraints in [&table.unique_constraints, &table.indexes] {
1244        encode_usize(bytes, constraints.len());
1245        for columns in constraints {
1246            encode_usize(bytes, columns.len());
1247            for column in columns {
1248                encode_string(bytes, column);
1249            }
1250        }
1251    }
1252}
1253
1254fn encode_operation(bytes: &mut Vec<u8>, operation: &MigrationOperation, include_field_name: bool) {
1255    match operation {
1256        MigrationOperation::CreateModel { model } => {
1257            bytes.push(12);
1258            encode_string(bytes, &model.key.app);
1259            encode_string(bytes, &model.key.name);
1260            encode_table(bytes, &model.table, include_field_name);
1261        }
1262        MigrationOperation::CreateTable { table } => {
1263            bytes.push(0);
1264            encode_table(bytes, table, include_field_name);
1265        }
1266        MigrationOperation::DropTable { name } => {
1267            bytes.push(1);
1268            encode_string(bytes, name);
1269        }
1270        MigrationOperation::AddColumn { table, column } => {
1271            bytes.push(2);
1272            encode_string(bytes, table);
1273            encode_column(bytes, column, include_field_name);
1274        }
1275        MigrationOperation::RemoveColumn { table, column } => {
1276            bytes.push(3);
1277            encode_string(bytes, table);
1278            encode_string(bytes, column);
1279        }
1280        MigrationOperation::RenameTable { old_name, new_name } => {
1281            bytes.push(4);
1282            encode_string(bytes, old_name);
1283            encode_string(bytes, new_name);
1284        }
1285        MigrationOperation::RenameColumn {
1286            table,
1287            old_name,
1288            new_name,
1289        } => {
1290            bytes.push(5);
1291            encode_string(bytes, table);
1292            encode_string(bytes, old_name);
1293            encode_string(bytes, new_name);
1294        }
1295        MigrationOperation::RunSql {
1296            forward,
1297            reverse,
1298            state_operations,
1299        } => {
1300            bytes.push(6);
1301            encode_string(bytes, forward);
1302            encode_option_string(bytes, reverse);
1303            encode_usize(bytes, state_operations.len());
1304            for operation in state_operations {
1305                encode_operation(bytes, operation, include_field_name);
1306            }
1307        }
1308        MigrationOperation::AlterColumn {
1309            table,
1310            old_column,
1311            new_column,
1312        } => {
1313            bytes.push(7);
1314            encode_string(bytes, table);
1315            encode_column(bytes, old_column, include_field_name);
1316            encode_column(bytes, new_column, include_field_name);
1317        }
1318        MigrationOperation::AddIndex { table, columns } => {
1319            bytes.push(8);
1320            encode_string(bytes, table);
1321            encode_string_list(bytes, columns);
1322        }
1323        MigrationOperation::RemoveIndex { table, columns } => {
1324            bytes.push(9);
1325            encode_string(bytes, table);
1326            encode_string_list(bytes, columns);
1327        }
1328        MigrationOperation::AddUniqueConstraint { table, columns } => {
1329            bytes.push(10);
1330            encode_string(bytes, table);
1331            encode_string_list(bytes, columns);
1332        }
1333        MigrationOperation::RemoveUniqueConstraint { table, columns } => {
1334            bytes.push(11);
1335            encode_string(bytes, table);
1336            encode_string_list(bytes, columns);
1337        }
1338        MigrationOperation::BackfillColumn {
1339            table,
1340            column,
1341            value,
1342        } => {
1343            bytes.push(13);
1344            encode_string(bytes, table);
1345            encode_string(bytes, column);
1346            encode_string(bytes, value);
1347        }
1348        MigrationOperation::CreateSchemaObject { object } => {
1349            bytes.push(14);
1350            bytes.push(match object.kind {
1351                SchemaObjectKind::Trigger => 0,
1352                SchemaObjectKind::View => 1,
1353                SchemaObjectKind::Table => 2,
1354            });
1355            encode_string(bytes, &object.app);
1356            encode_string(bytes, &object.name);
1357            encode_string(bytes, &object.sql);
1358        }
1359        MigrationOperation::DropSchemaObject { kind, name } => {
1360            bytes.push(15);
1361            bytes.push(match kind {
1362                SchemaObjectKind::Trigger => 0,
1363                SchemaObjectKind::View => 1,
1364                SchemaObjectKind::Table => 2,
1365            });
1366            encode_string(bytes, name);
1367        }
1368    }
1369}
1370
1371fn encode_string_list(bytes: &mut Vec<u8>, values: &[String]) {
1372    encode_usize(bytes, values.len());
1373    for value in values {
1374        encode_string(bytes, value);
1375    }
1376}
1377
1378fn fnv1a64(bytes: &[u8]) -> u64 {
1379    let mut hash = 0xcbf29ce484222325_u64;
1380    for byte in bytes {
1381        hash ^= u64::from(*byte);
1382        hash = hash.wrapping_mul(0x100000001b3);
1383    }
1384    hash
1385}
1386
1387impl MigrationOperation {
1388    /// Applies this operation to the in-memory schema state.
1389    pub fn state_forwards(&self, state: &mut ProjectState) -> Result<(), MigrationError> {
1390        match self {
1391            Self::CreateModel { model } => {
1392                if state.model_keys.values().any(|key| key == &model.key) {
1393                    return Err(MigrationError::InvalidState(format!(
1394                        "duplicate model {:?}",
1395                        model.key
1396                    )));
1397                }
1398                Self::CreateTable {
1399                    table: model.table.clone(),
1400                }
1401                .state_forwards(state)?;
1402                state
1403                    .model_keys
1404                    .insert(model.table.name.clone(), model.key.clone());
1405            }
1406            Self::CreateTable { table } => {
1407                if state
1408                    .tables
1409                    .iter()
1410                    .any(|current| current.name == table.name)
1411                {
1412                    return Err(MigrationError::DuplicateTable(table.name.clone()));
1413                }
1414                state.tables.push(table.clone());
1415            }
1416            Self::DropTable { name } => {
1417                if let Some((table, column)) = inbound_reference(state, name, None) {
1418                    return Err(MigrationError::DependentTable {
1419                        table: name.clone(),
1420                        dependent: format!("{table}.{column}"),
1421                    });
1422                }
1423                let index = table_index(state, name)?;
1424                state.tables.remove(index);
1425                state.model_keys.remove(name);
1426            }
1427            Self::AddColumn { table, column } => {
1428                let table = table_mut(state, table)?;
1429                if table
1430                    .columns
1431                    .iter()
1432                    .any(|current| current.name == column.name)
1433                {
1434                    return Err(MigrationError::DuplicateColumn {
1435                        table: table.name.clone(),
1436                        column: column.name.clone(),
1437                    });
1438                }
1439                table.columns.push(column.clone());
1440            }
1441            Self::RemoveColumn { table, column } => {
1442                if let Some((dependent_table, dependent_column)) =
1443                    inbound_reference(state, table, Some(column))
1444                {
1445                    return Err(MigrationError::DependentColumn {
1446                        table: table.clone(),
1447                        column: column.clone(),
1448                        dependent: format!("{dependent_table}.{dependent_column}"),
1449                    });
1450                }
1451                let table = table_mut(state, table)?;
1452                if table
1453                    .unique_constraints
1454                    .iter()
1455                    .chain(table.indexes.iter())
1456                    .any(|columns| columns.iter().any(|current| current == column))
1457                {
1458                    return Err(MigrationError::DependentColumn {
1459                        table: table.name.clone(),
1460                        column: column.clone(),
1461                        dependent: "an index or unique constraint".into(),
1462                    });
1463                }
1464                let index = column_index(table, column)?;
1465                table.columns.remove(index);
1466            }
1467            Self::RenameTable { old_name, new_name } => {
1468                if state.tables.iter().any(|table| table.name == *new_name) {
1469                    return Err(MigrationError::DuplicateTable(new_name.clone()));
1470                }
1471                table_mut(state, old_name)?.name = new_name.clone();
1472                if let Some(model_key) = state.model_keys.remove(old_name) {
1473                    state.model_keys.insert(new_name.clone(), model_key);
1474                }
1475                for table in &mut state.tables {
1476                    for column in &mut table.columns {
1477                        if let Some(reference) = &mut column.references {
1478                            reference.target =
1479                                rename_reference_table(&reference.target, old_name, new_name);
1480                        }
1481                    }
1482                }
1483            }
1484            Self::RenameColumn {
1485                table,
1486                old_name,
1487                new_name,
1488            } => {
1489                let existing = &state.tables[table_index(state, table)?];
1490                if existing.columns.iter().any(|column| column.check.is_some()) {
1491                    return Err(MigrationError::InvalidState(format!(
1492                        "cannot rename a column of {table} with opaque CHECK expressions; use explicit RunSql and matching state operations"
1493                    )));
1494                }
1495                let table_name = {
1496                    let table = table_mut(state, table)?;
1497                    if table.columns.iter().any(|column| column.name == *new_name) {
1498                        return Err(MigrationError::DuplicateColumn {
1499                            table: table.name.clone(),
1500                            column: new_name.clone(),
1501                        });
1502                    }
1503                    let index = column_index(table, old_name)?;
1504                    table.columns[index].name = new_name.clone();
1505                    rename_constraint_columns(&mut table.unique_constraints, old_name, new_name);
1506                    rename_constraint_columns(&mut table.indexes, old_name, new_name);
1507                    table.name.clone()
1508                };
1509                for dependent_table in &mut state.tables {
1510                    for column in &mut dependent_table.columns {
1511                        if let Some(reference) = &mut column.references {
1512                            reference.target = rename_reference_column(
1513                                &reference.target,
1514                                &table_name,
1515                                old_name,
1516                                new_name,
1517                            );
1518                        }
1519                    }
1520                }
1521            }
1522            Self::AlterColumn {
1523                table,
1524                old_column,
1525                new_column,
1526            } => {
1527                if old_column.name != new_column.name {
1528                    return Err(MigrationError::AlteredColumnRenamed {
1529                        table: table.clone(),
1530                        old_name: old_column.name.clone(),
1531                        new_name: new_column.name.clone(),
1532                    });
1533                }
1534                let table = table_mut(state, table)?;
1535                let index = column_index(table, &old_column.name)?;
1536                if table.columns[index] != *old_column {
1537                    return Err(MigrationError::UnexpectedColumnState {
1538                        table: table.name.clone(),
1539                        column: old_column.name.clone(),
1540                    });
1541                }
1542                table.columns[index] = new_column.clone();
1543            }
1544            Self::AddIndex { table, columns } => {
1545                let table = table_mut(state, table)?;
1546                validate_constraint_columns(table, columns)?;
1547                if table.indexes.iter().any(|index| index == columns) {
1548                    return Err(MigrationError::DuplicateIndex {
1549                        table: table.name.clone(),
1550                        columns: columns.clone(),
1551                    });
1552                }
1553                table.indexes.push(columns.clone());
1554            }
1555            Self::RemoveIndex { table, columns } => {
1556                let table = table_mut(state, table)?;
1557                remove_constraint(&mut table.indexes, columns, &table.name, true)?;
1558            }
1559            Self::AddUniqueConstraint { table, columns } => {
1560                let table = table_mut(state, table)?;
1561                validate_constraint_columns(table, columns)?;
1562                if table
1563                    .unique_constraints
1564                    .iter()
1565                    .any(|constraint| constraint == columns)
1566                {
1567                    return Err(MigrationError::DuplicateUniqueConstraint {
1568                        table: table.name.clone(),
1569                        columns: columns.clone(),
1570                    });
1571                }
1572                table.unique_constraints.push(columns.clone());
1573            }
1574            Self::RemoveUniqueConstraint { table, columns } => {
1575                let table = table_mut(state, table)?;
1576                remove_constraint(&mut table.unique_constraints, columns, &table.name, false)?;
1577            }
1578            Self::BackfillColumn {
1579                table,
1580                column,
1581                value,
1582            } => {
1583                if value.trim().is_empty()
1584                    || value.contains(';')
1585                    || value.contains("--")
1586                    || value.contains("/*")
1587                    || value.contains("*/")
1588                {
1589                    return Err(MigrationError::InvalidState(format!(
1590                        "backfill value for {table}.{column} must be one non-empty SQLite expression without statements or comments"
1591                    )));
1592                }
1593                let table = table_mut(state, table)?;
1594                column_index(table, column)?;
1595            }
1596            Self::CreateSchemaObject { object } => {
1597                validate_schema_object(object)?;
1598                if state
1599                    .objects
1600                    .iter()
1601                    .any(|current| current.kind == object.kind && current.name == object.name)
1602                {
1603                    return Err(MigrationError::InvalidState(format!(
1604                        "duplicate {:?} {}",
1605                        object.kind, object.name
1606                    )));
1607                }
1608                state.objects.push(object.clone());
1609            }
1610            Self::DropSchemaObject { kind, name } => {
1611                let index = state
1612                    .objects
1613                    .iter()
1614                    .position(|object| object.kind == *kind && object.name == *name)
1615                    .ok_or_else(|| {
1616                        MigrationError::InvalidState(format!("missing {kind:?} {name}"))
1617                    })?;
1618                state.objects.remove(index);
1619            }
1620            Self::RunSql {
1621                state_operations, ..
1622            } => {
1623                for operation in state_operations {
1624                    operation.state_forwards(state)?;
1625                }
1626            }
1627        }
1628        Ok(())
1629    }
1630}
1631
1632fn table_index(state: &ProjectState, name: &str) -> Result<usize, MigrationError> {
1633    state
1634        .tables
1635        .iter()
1636        .position(|table| table.name == name)
1637        .ok_or_else(|| MigrationError::MissingTable(name.into()))
1638}
1639
1640fn table_mut<'a>(
1641    state: &'a mut ProjectState,
1642    name: &str,
1643) -> Result<&'a mut TableState, MigrationError> {
1644    let index = table_index(state, name)?;
1645    Ok(&mut state.tables[index])
1646}
1647
1648fn column_index(table: &TableState, name: &str) -> Result<usize, MigrationError> {
1649    table
1650        .columns
1651        .iter()
1652        .position(|column| column.name == name)
1653        .ok_or_else(|| MigrationError::MissingColumn {
1654            table: table.name.clone(),
1655            column: name.into(),
1656        })
1657}
1658
1659fn inbound_reference(
1660    state: &ProjectState,
1661    target_table: &str,
1662    target_column: Option<&str>,
1663) -> Option<(String, String)> {
1664    state.tables.iter().find_map(|table| {
1665        table.columns.iter().find_map(|column| {
1666            let reference = column.references.as_ref()?;
1667            let (table_name, column_name) = parse_reference(&reference.target)?;
1668            (table_name == target_table
1669                && target_column.is_none_or(|target_column| column_name == target_column))
1670            .then(|| (table.name.clone(), column.name.clone()))
1671        })
1672    })
1673}
1674
1675fn parse_reference(reference: &str) -> Option<(&str, &str)> {
1676    let (table, column) = reference.split_once('(')?;
1677    column.strip_suffix(')').map(|column| (table, column))
1678}
1679
1680fn rename_reference_table(reference: &str, old_name: &str, new_name: &str) -> String {
1681    let Some((table, column)) = parse_reference(reference) else {
1682        return reference.into();
1683    };
1684    if table == old_name {
1685        format!("{new_name}({column})")
1686    } else {
1687        reference.into()
1688    }
1689}
1690
1691fn rename_reference_column(
1692    reference: &str,
1693    table_name: &str,
1694    old_name: &str,
1695    new_name: &str,
1696) -> String {
1697    let Some((table, column)) = parse_reference(reference) else {
1698        return reference.into();
1699    };
1700    if table == table_name && column == old_name {
1701        format!("{table_name}({new_name})")
1702    } else {
1703        reference.into()
1704    }
1705}
1706
1707fn rename_constraint_columns(constraints: &mut [Vec<String>], old_name: &str, new_name: &str) {
1708    for columns in constraints {
1709        for column in columns {
1710            if column == old_name {
1711                *column = new_name.into();
1712            }
1713        }
1714    }
1715}
1716
1717fn validate_constraint_columns(
1718    table: &TableState,
1719    columns: &[String],
1720) -> Result<(), MigrationError> {
1721    if columns.is_empty() {
1722        return Err(MigrationError::EmptyConstraint(table.name.clone()));
1723    }
1724    for column in columns {
1725        column_index(table, column)?;
1726    }
1727    Ok(())
1728}
1729
1730fn remove_constraint(
1731    constraints: &mut Vec<Vec<String>>,
1732    columns: &[String],
1733    table: &str,
1734    index: bool,
1735) -> Result<(), MigrationError> {
1736    let Some(position) = constraints
1737        .iter()
1738        .position(|constraint| constraint == columns)
1739    else {
1740        return Err(if index {
1741            MigrationError::MissingIndex {
1742                table: table.into(),
1743                columns: columns.to_vec(),
1744            }
1745        } else {
1746            MigrationError::MissingUniqueConstraint {
1747                table: table.into(),
1748                columns: columns.to_vec(),
1749            }
1750        });
1751    };
1752    constraints.remove(position);
1753    Ok(())
1754}
1755
1756/// Errors while validating or replaying migrations.
1757#[derive(Debug, Clone, PartialEq, Eq)]
1758pub enum MigrationError {
1759    InvalidState(String),
1760    DuplicateMigration(MigrationId),
1761    InvalidChecksum(MigrationId),
1762    MissingDependency {
1763        migration: MigrationId,
1764        dependency: MigrationId,
1765    },
1766    CyclicDependency(Vec<MigrationId>),
1767    UnknownTarget(MigrationId),
1768    UnknownApplied(MigrationId),
1769    InconsistentAppliedHistory {
1770        migration: MigrationId,
1771        dependency: MigrationId,
1772    },
1773    DuplicateTable(String),
1774    MissingTable(String),
1775    DuplicateColumn {
1776        table: String,
1777        column: String,
1778    },
1779    MissingColumn {
1780        table: String,
1781        column: String,
1782    },
1783    AlteredColumnRenamed {
1784        table: String,
1785        old_name: String,
1786        new_name: String,
1787    },
1788    UnexpectedColumnState {
1789        table: String,
1790        column: String,
1791    },
1792    EmptyConstraint(String),
1793    DuplicateIndex {
1794        table: String,
1795        columns: Vec<String>,
1796    },
1797    MissingIndex {
1798        table: String,
1799        columns: Vec<String>,
1800    },
1801    DuplicateUniqueConstraint {
1802        table: String,
1803        columns: Vec<String>,
1804    },
1805    MissingUniqueConstraint {
1806        table: String,
1807        columns: Vec<String>,
1808    },
1809    DependentTable {
1810        table: String,
1811        dependent: String,
1812    },
1813    DependentColumn {
1814        table: String,
1815        column: String,
1816        dependent: String,
1817    },
1818}
1819
1820impl fmt::Display for MigrationError {
1821    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1822        match self {
1823            Self::InvalidState(message) => formatter.write_str(message),
1824            Self::DuplicateMigration(id) => {
1825                write!(formatter, "duplicate migration {}:{}", id.app, id.name)
1826            }
1827            Self::InvalidChecksum(id) => {
1828                write!(
1829                    formatter,
1830                    "migration {}:{} has an invalid checksum",
1831                    id.app, id.name
1832                )
1833            }
1834            Self::MissingDependency {
1835                migration,
1836                dependency,
1837            } => write!(
1838                formatter,
1839                "migration {}:{} depends on missing {}:{}",
1840                migration.app, migration.name, dependency.app, dependency.name
1841            ),
1842            Self::CyclicDependency(ids) => {
1843                write!(formatter, "cyclic migration dependencies: {ids:?}")
1844            }
1845            Self::UnknownTarget(id) => {
1846                write!(formatter, "unknown migration target {}:{}", id.app, id.name)
1847            }
1848            Self::UnknownApplied(id) => {
1849                write!(
1850                    formatter,
1851                    "unknown applied migration {}:{}",
1852                    id.app, id.name
1853                )
1854            }
1855            Self::InconsistentAppliedHistory {
1856                migration,
1857                dependency,
1858            } => write!(
1859                formatter,
1860                "applied migration {}:{} is missing dependency {}:{}",
1861                migration.app, migration.name, dependency.app, dependency.name
1862            ),
1863            Self::DuplicateTable(name) => write!(formatter, "duplicate table {name}"),
1864            Self::MissingTable(name) => write!(formatter, "missing table {name}"),
1865            Self::DuplicateColumn { table, column } => {
1866                write!(formatter, "duplicate column {table}.{column}")
1867            }
1868            Self::MissingColumn { table, column } => {
1869                write!(formatter, "missing column {table}.{column}")
1870            }
1871            Self::AlteredColumnRenamed {
1872                table,
1873                old_name,
1874                new_name,
1875            } => write!(
1876                formatter,
1877                "AlterColumn for {table} cannot rename {old_name} to {new_name}; use RenameColumn"
1878            ),
1879            Self::UnexpectedColumnState { table, column } => {
1880                write!(
1881                    formatter,
1882                    "unexpected historical state for column {table}.{column}"
1883                )
1884            }
1885            Self::EmptyConstraint(table) => write!(formatter, "empty constraint on table {table}"),
1886            Self::DuplicateIndex { table, columns } => {
1887                write!(
1888                    formatter,
1889                    "duplicate index on {table}({})",
1890                    columns.join(", ")
1891                )
1892            }
1893            Self::MissingIndex { table, columns } => {
1894                write!(
1895                    formatter,
1896                    "missing index on {table}({})",
1897                    columns.join(", ")
1898                )
1899            }
1900            Self::DuplicateUniqueConstraint { table, columns } => write!(
1901                formatter,
1902                "duplicate unique constraint on {table}({})",
1903                columns.join(", ")
1904            ),
1905            Self::MissingUniqueConstraint { table, columns } => write!(
1906                formatter,
1907                "missing unique constraint on {table}({})",
1908                columns.join(", ")
1909            ),
1910            Self::DependentTable { table, dependent } => {
1911                write!(
1912                    formatter,
1913                    "cannot remove table {table}; it is referenced by {dependent}"
1914                )
1915            }
1916            Self::DependentColumn {
1917                table,
1918                column,
1919                dependent,
1920            } => write!(
1921                formatter,
1922                "cannot remove column {table}.{column}; it is used by {dependent}"
1923            ),
1924        }
1925    }
1926}
1927
1928impl std::error::Error for MigrationError {}
1929
1930/// A validated migration dependency graph with deterministic replay order.
1931#[derive(Debug, Clone)]
1932pub struct MigrationGraph {
1933    migrations: BTreeMap<MigrationId, Migration>,
1934    order: Vec<MigrationId>,
1935}
1936
1937/// One migration selected by the planner. SQL rendering and execution are
1938/// deliberately separate from dependency planning.
1939#[derive(Debug, Clone, PartialEq, Eq)]
1940pub struct MigrationPlanStep {
1941    pub id: MigrationId,
1942}
1943
1944impl MigrationGraph {
1945    /// Validates migrations and computes their lexically deterministic topological order.
1946    pub fn new(migrations: Vec<Migration>) -> Result<Self, MigrationError> {
1947        let mut by_id = BTreeMap::new();
1948        for migration in migrations {
1949            let id = migration.id.clone();
1950            if by_id.insert(id.clone(), migration).is_some() {
1951                return Err(MigrationError::DuplicateMigration(id));
1952            }
1953        }
1954        for migration in by_id.values() {
1955            if !migration.validates_checksum() {
1956                return Err(MigrationError::InvalidChecksum(migration.id.clone()));
1957            }
1958            for dependency in &migration.dependencies {
1959                if !by_id.contains_key(dependency) {
1960                    return Err(MigrationError::MissingDependency {
1961                        migration: migration.id.clone(),
1962                        dependency: dependency.clone(),
1963                    });
1964                }
1965            }
1966        }
1967
1968        let mut remaining: BTreeMap<_, BTreeSet<_>> = by_id
1969            .iter()
1970            .map(|(id, migration)| (id.clone(), migration.dependencies.iter().cloned().collect()))
1971            .collect();
1972        let mut order = Vec::with_capacity(by_id.len());
1973        while !remaining.is_empty() {
1974            let ready: Vec<_> = remaining
1975                .iter()
1976                .filter(|(_, dependencies)| dependencies.is_empty())
1977                .map(|(id, _)| id.clone())
1978                .collect();
1979            if ready.is_empty() {
1980                return Err(MigrationError::CyclicDependency(
1981                    remaining.into_keys().collect(),
1982                ));
1983            }
1984            for id in ready {
1985                remaining.remove(&id);
1986                for dependencies in remaining.values_mut() {
1987                    dependencies.remove(&id);
1988                }
1989                order.push(id);
1990            }
1991        }
1992        Ok(Self {
1993            migrations: by_id,
1994            order,
1995        })
1996    }
1997
1998    /// Returns migrations in deterministic dependency order.
1999    pub fn ordered(&self) -> impl Iterator<Item = &Migration> {
2000        self.order.iter().map(|id| &self.migrations[id])
2001    }
2002
2003    /// Returns the terminal migrations for one application. Multiple heads
2004    /// indicate a branch that needs a merge migration before release.
2005    pub fn app_heads(&self, app: &str) -> Vec<&Migration> {
2006        let depended_on: BTreeSet<_> = self
2007            .migrations
2008            .values()
2009            .filter(|migration| migration.id.app == app)
2010            .flat_map(|migration| migration.dependencies.iter())
2011            .filter(|dependency| dependency.app == app)
2012            .collect();
2013        self.ordered()
2014            .filter(|migration| migration.id.app == app && !depended_on.contains(&migration.id))
2015            .collect()
2016    }
2017
2018    /// Builds and validates a forward-only plan. Independent applied migrations
2019    /// are retained. A target already passed in its app is rejected.
2020    pub fn plan_to(
2021        &self,
2022        applied: &BTreeSet<MigrationId>,
2023        target: Option<&MigrationId>,
2024    ) -> Result<Vec<MigrationPlanStep>, MigrationError> {
2025        self.plan_forward(applied, target)
2026    }
2027
2028    /// Returns pending steps only, after checking graph and historical state.
2029    pub fn plan_forward(
2030        &self,
2031        applied: &BTreeSet<MigrationId>,
2032        target: Option<&MigrationId>,
2033    ) -> Result<Vec<MigrationPlanStep>, MigrationError> {
2034        self.validate_applied(applied)?;
2035        self.validate_heads()?;
2036        if let Some(target) = target {
2037            if target.name == "zero" {
2038                return Err(MigrationError::InvalidState(
2039                    "migration rollback is not supported".into(),
2040                ));
2041            }
2042            for id in applied
2043                .iter()
2044                .filter(|id| id.app == target.app && *id != target)
2045            {
2046                if self.target_closure(id)?.contains(target) {
2047                    return Err(MigrationError::InvalidState(
2048                        "migration rollback is not supported".into(),
2049                    ));
2050                }
2051            }
2052        }
2053        let desired = match target {
2054            Some(target) => self.target_closure(target)?,
2055            None => self.migrations.keys().cloned().collect(),
2056        };
2057
2058        let mut plan = Vec::new();
2059        let mut state = self.replay_selected(ProjectState::default(), applied)?;
2060        for migration in self.ordered() {
2061            if desired.contains(&migration.id) && !applied.contains(&migration.id) {
2062                for operation in &migration.operations {
2063                    operation.state_forwards(&mut state)?;
2064                    state.validate()?;
2065                }
2066                plan.push(MigrationPlanStep {
2067                    id: migration.id.clone(),
2068                });
2069            }
2070        }
2071        Ok(plan)
2072    }
2073
2074    pub fn validate_heads(&self) -> Result<(), MigrationError> {
2075        let apps: BTreeSet<_> = self.migrations.keys().map(|id| id.app.as_str()).collect();
2076        for app in apps {
2077            if self.app_heads(app).len() > 1 {
2078                return Err(MigrationError::InvalidState(format!(
2079                    "conflicting migration heads in {app}; merge required"
2080                )));
2081            }
2082        }
2083        Ok(())
2084    }
2085
2086    fn target_closure(
2087        &self,
2088        target: &MigrationId,
2089    ) -> Result<BTreeSet<MigrationId>, MigrationError> {
2090        if !self.migrations.contains_key(target) {
2091            return Err(MigrationError::UnknownTarget(target.clone()));
2092        }
2093        let mut desired = BTreeSet::new();
2094        let mut pending = vec![target.clone()];
2095        while let Some(id) = pending.pop() {
2096            if !desired.insert(id.clone()) {
2097                continue;
2098            }
2099            pending.extend(self.migrations[&id].dependencies.iter().cloned());
2100        }
2101        Ok(desired)
2102    }
2103
2104    fn validate_applied(&self, applied: &BTreeSet<MigrationId>) -> Result<(), MigrationError> {
2105        for id in applied {
2106            let migration = self
2107                .migrations
2108                .get(id)
2109                .ok_or_else(|| MigrationError::UnknownApplied(id.clone()))?;
2110            for dependency in &migration.dependencies {
2111                if !applied.contains(dependency) {
2112                    return Err(MigrationError::InconsistentAppliedHistory {
2113                        migration: id.clone(),
2114                        dependency: dependency.clone(),
2115                    });
2116                }
2117            }
2118        }
2119        Ok(())
2120    }
2121
2122    /// Replays every migration from an initial schema state.
2123    pub fn replay(&self, mut state: ProjectState) -> Result<ProjectState, MigrationError> {
2124        for migration in self.ordered() {
2125            for operation in &migration.operations {
2126                operation.state_forwards(&mut state)?;
2127                state.validate()?;
2128            }
2129        }
2130        Ok(state)
2131    }
2132
2133    /// Replays a closed subset of migrations in graph order. Executors use
2134    /// this to reconstruct the state immediately before pending migrations.
2135    pub fn replay_selected(
2136        &self,
2137        mut state: ProjectState,
2138        selected: &BTreeSet<MigrationId>,
2139    ) -> Result<ProjectState, MigrationError> {
2140        self.validate_applied(selected)?;
2141        for migration in self.ordered() {
2142            if selected.contains(&migration.id) {
2143                for operation in &migration.operations {
2144                    operation.state_forwards(&mut state)?;
2145                    state.validate()?;
2146                }
2147            }
2148        }
2149        Ok(state)
2150    }
2151}
2152
2153/// SQL and resulting historical state for one SQLite schema operation.
2154#[derive(Debug, Clone, PartialEq, Eq)]
2155pub struct SqliteOperationPlan {
2156    pub statements: Vec<String>,
2157    pub resulting_state: ProjectState,
2158}
2159
2160/// Renders declarative migration operations for SQLite. Operations that alter
2161/// a table's shape use SQLite's reliable create-copy-drop-rename workflow.
2162#[derive(Debug, Default, Clone, Copy)]
2163pub struct SqliteSchemaEditor;
2164
2165impl SqliteSchemaEditor {
2166    /// Returns the affected table for operations implemented through a table
2167    /// rebuild. Callers can inspect database-owned triggers before execution.
2168    pub fn rebuild_table_name(operation: &MigrationOperation) -> Option<&str> {
2169        match operation {
2170            MigrationOperation::AddIndex { table, .. }
2171            | MigrationOperation::RemoveIndex { table, .. }
2172            | MigrationOperation::AddUniqueConstraint { table, .. }
2173            | MigrationOperation::RemoveUniqueConstraint { table, .. }
2174            | MigrationOperation::AddColumn { table, .. }
2175            | MigrationOperation::RemoveColumn { table, .. }
2176            | MigrationOperation::AlterColumn { table, .. } => Some(table),
2177            _ => None,
2178        }
2179    }
2180
2181    pub fn plan(
2182        operation: &MigrationOperation,
2183        before: &ProjectState,
2184    ) -> Result<SqliteOperationPlan, MigrationError> {
2185        let mut resulting_state = before.clone();
2186        operation.state_forwards(&mut resulting_state)?;
2187        let statements = match operation {
2188            MigrationOperation::CreateModel { model } => create_table_statements(&model.table),
2189            MigrationOperation::CreateTable { table } => create_table_statements(table),
2190            MigrationOperation::DropTable { name } => {
2191                vec![format!("DROP TABLE {}", quote_identifier(name))]
2192            }
2193            MigrationOperation::RenameTable { old_name, new_name } => vec![format!(
2194                "ALTER TABLE {} RENAME TO {}",
2195                quote_identifier(old_name),
2196                quote_identifier(new_name)
2197            )],
2198            MigrationOperation::RenameColumn {
2199                table,
2200                old_name,
2201                new_name,
2202            } => vec![format!(
2203                "ALTER TABLE {} RENAME COLUMN {} TO {}",
2204                quote_identifier(table),
2205                quote_identifier(old_name),
2206                quote_identifier(new_name)
2207            )],
2208            MigrationOperation::BackfillColumn {
2209                table,
2210                column,
2211                value,
2212            } => vec![format!(
2213                "UPDATE {} SET {} = {} WHERE {} IS NULL",
2214                quote_identifier(table),
2215                quote_identifier(column),
2216                value,
2217                quote_identifier(column),
2218            )],
2219            MigrationOperation::CreateSchemaObject { object } => vec![object.sql.clone()],
2220            MigrationOperation::DropSchemaObject { kind, name } => vec![format!(
2221                "DROP {} {}",
2222                match kind {
2223                    SchemaObjectKind::Table => "TABLE",
2224                    SchemaObjectKind::Trigger => "TRIGGER",
2225                    SchemaObjectKind::View => "VIEW",
2226                },
2227                quote_identifier(name)
2228            )],
2229            _ if Self::rebuild_table_name(operation).is_some() => rebuild_table_statements(
2230                Self::rebuild_table_name(operation).expect("operation requires a rebuild"),
2231                before,
2232                &resulting_state,
2233                operation,
2234            )?,
2235            MigrationOperation::RunSql { forward, .. } => vec![forward.clone()],
2236            _ => unreachable!("all non-rebuild migration operations are handled above"),
2237        };
2238        Ok(SqliteOperationPlan {
2239            statements,
2240            resulting_state,
2241        })
2242    }
2243}
2244
2245fn rebuild_table_statements(
2246    table_name: &str,
2247    before: &ProjectState,
2248    after: &ProjectState,
2249    operation: &MigrationOperation,
2250) -> Result<Vec<String>, MigrationError> {
2251    let old_table = &before.tables[table_index(before, table_name)?];
2252    let new_table = &after.tables[table_index(after, table_name)?];
2253    let temporary_name = format!("__che_migration_new_{table_name}");
2254    let mut temporary = new_table.clone();
2255    temporary.name = temporary_name.clone();
2256    let mut statements = vec![create_table_sql(&temporary)];
2257
2258    let old_columns: BTreeSet<_> = old_table
2259        .columns
2260        .iter()
2261        .map(|column| column.name.as_str())
2262        .collect();
2263    let copied_columns: Vec<_> = new_table
2264        .columns
2265        .iter()
2266        .filter_map(|column| {
2267            copy_source_column(column, operation, &old_columns)
2268                .map(|source| (column.name.as_str(), source))
2269        })
2270        .collect();
2271    if !copied_columns.is_empty() {
2272        let destinations = copied_columns
2273            .iter()
2274            .map(|(destination, _)| quote_identifier(destination))
2275            .collect::<Vec<_>>()
2276            .join(", ");
2277        let sources = copied_columns
2278            .iter()
2279            .map(|(_, source)| quote_identifier(source))
2280            .collect::<Vec<_>>()
2281            .join(", ");
2282        statements.push(format!(
2283            "INSERT INTO {} ({destinations}) SELECT {sources} FROM {}",
2284            quote_identifier(&temporary_name),
2285            quote_identifier(table_name)
2286        ));
2287    }
2288    statements.push(format!("DROP TABLE {}", quote_identifier(table_name)));
2289    statements.push(format!(
2290        "ALTER TABLE {} RENAME TO {}",
2291        quote_identifier(&temporary_name),
2292        quote_identifier(table_name)
2293    ));
2294    statements.extend(index_statements(new_table));
2295    Ok(statements)
2296}
2297
2298fn copy_source_column<'a>(
2299    new_column: &'a ColumnState,
2300    operation: &'a MigrationOperation,
2301    old_columns: &BTreeSet<&str>,
2302) -> Option<&'a str> {
2303    if let MigrationOperation::RenameColumn {
2304        old_name, new_name, ..
2305    } = operation
2306        && new_column.name == *new_name
2307        && old_columns.contains(old_name.as_str())
2308    {
2309        return Some(old_name);
2310    }
2311    old_columns
2312        .contains(new_column.name.as_str())
2313        .then_some(new_column.name.as_str())
2314}
2315
2316fn create_table_statements(table: &TableState) -> Vec<String> {
2317    let mut statements = vec![create_table_sql(table)];
2318    statements.extend(index_statements(table));
2319    statements
2320}
2321
2322fn create_table_sql(table: &TableState) -> String {
2323    let columns = table
2324        .columns
2325        .iter()
2326        .map(column_definition_sql)
2327        .chain(table.unique_constraints.iter().map(|columns| {
2328            format!(
2329                "UNIQUE ({})",
2330                columns
2331                    .iter()
2332                    .map(|column| quote_identifier(column))
2333                    .collect::<Vec<_>>()
2334                    .join(", ")
2335            )
2336        }))
2337        .collect::<Vec<_>>()
2338        .join(", ");
2339    format!("CREATE TABLE {} ({columns})", quote_identifier(&table.name))
2340}
2341
2342fn index_statements(table: &TableState) -> Vec<String> {
2343    let mut statements = Vec::new();
2344    for (position, columns) in table.indexes.iter().enumerate() {
2345        statements.push(format!(
2346            "CREATE INDEX {} ON {} ({})",
2347            quote_identifier(&index_name(&table.name, position)),
2348            quote_identifier(&table.name),
2349            columns
2350                .iter()
2351                .map(|column| quote_identifier(column))
2352                .collect::<Vec<_>>()
2353                .join(", ")
2354        ));
2355    }
2356    statements
2357}
2358
2359fn index_name(table: &str, position: usize) -> String {
2360    format!("{table}_idx_{position}")
2361}
2362
2363fn column_definition_sql(column: &ColumnState) -> String {
2364    let mut sql = format!(
2365        "{} {}",
2366        quote_identifier(&column.name),
2367        sqlite_column_type(column.column_type)
2368    );
2369    if column.primary_key {
2370        sql.push_str(" PRIMARY KEY");
2371    }
2372    if !column.nullable && (!column.primary_key || column.column_type == ColumnType::Uuid) {
2373        sql.push_str(" NOT NULL");
2374    }
2375    if column.unique {
2376        sql.push_str(" UNIQUE");
2377    }
2378    if (column.auto_now_add || column.auto_now) && column.default.is_none() {
2379        sql.push_str(" DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))");
2380    }
2381    if let Some(default) = &column.default {
2382        sql.push_str(" DEFAULT ");
2383        sql.push_str(default);
2384    }
2385    if let Some(check) = &column.check {
2386        sql.push_str(" CHECK (");
2387        sql.push_str(check);
2388        sql.push(')');
2389    }
2390    if let Some(choices) = &column.choices {
2391        sql.push_str(" CHECK (");
2392        sql.push_str(&quote_identifier(&column.name));
2393        sql.push_str(" IN (");
2394        sql.push_str(
2395            &choices
2396                .iter()
2397                .map(|choice| format!("'{}'", choice.replace('\'', "''")))
2398                .collect::<Vec<_>>()
2399                .join(", "),
2400        );
2401        sql.push_str("))");
2402    }
2403    if let Some(reference) = &column.references {
2404        sql.push_str(" REFERENCES ");
2405        sql.push_str(&reference.target);
2406        if let Some(on_delete) = &reference.on_delete {
2407            sql.push_str(" ON DELETE ");
2408            sql.push_str(&on_delete.to_ascii_uppercase());
2409        }
2410    }
2411    sql
2412}
2413
2414fn sqlite_column_type(column_type: ColumnType) -> &'static str {
2415    match column_type {
2416        ColumnType::Integer => "INTEGER",
2417        ColumnType::Real => "REAL",
2418        ColumnType::Text => "TEXT",
2419        ColumnType::Binary => "BLOB",
2420        ColumnType::Boolean => "INTEGER",
2421        ColumnType::DateTime => "TEXT",
2422        ColumnType::Uuid => "BLOB",
2423    }
2424}
2425
2426fn quote_identifier(identifier: &str) -> String {
2427    format!("\"{}\"", identifier.replace('"', "\"\""))
2428}
2429
2430#[cfg(test)]
2431mod tests {
2432    use super::*;
2433
2434    fn id(app: &str, name: &str) -> MigrationId {
2435        MigrationId {
2436            app: app.into(),
2437            name: name.into(),
2438        }
2439    }
2440
2441    fn migration(id: MigrationId, dependencies: Vec<MigrationId>) -> Migration {
2442        Migration {
2443            id,
2444            dependencies,
2445            operations: vec![],
2446            checksum: "test".into(),
2447        }
2448    }
2449
2450    fn column(name: &str) -> ColumnState {
2451        ColumnState {
2452            field_name: name.into(),
2453            name: name.into(),
2454            column_type: ColumnType::Integer,
2455            nullable: false,
2456            primary_key: false,
2457            unique: false,
2458            default: None,
2459            check: None,
2460            choices: None,
2461            references: None,
2462            auto_now_add: false,
2463            auto_now: false,
2464        }
2465    }
2466
2467    #[derive(Debug, crate::Model)]
2468    #[orm(table = "legacy_diff")]
2469    struct LegacyDiffModel {
2470        #[orm(primary_key)]
2471        id: i64,
2472    }
2473
2474    #[derive(Debug, crate::Model)]
2475    #[orm(table = "ordered_diff")]
2476    struct OrderedDiffModel {
2477        #[orm(primary_key)]
2478        id: i64,
2479    }
2480
2481    #[test]
2482    fn app_schema_diff_ignores_registration_order_and_foreign_keys_expose_target_parts() {
2483        let legacy = SchemaSet::new().model::<LegacyDiffModel>();
2484        let ordered = SchemaSet::new().model::<OrderedDiffModel>();
2485        let first = ProjectState::from_app_schemas([("z", &legacy), ("a", &ordered)]).unwrap();
2486        let second = ProjectState::from_app_schemas([("a", &ordered), ("z", &legacy)]).unwrap();
2487        assert!(first.diff(&second).unwrap().is_empty());
2488        assert!(second.diff(&first).unwrap().is_empty());
2489        let reference = ForeignKeyState {
2490            target: "ordered_diff(id)".into(),
2491            on_delete: Some("cascade".into()),
2492        };
2493        assert_eq!(reference.target_parts(), Some(("ordered_diff", "id")));
2494    }
2495
2496    #[test]
2497    fn named_schema_objects_are_replayable_and_affect_checksums() {
2498        let trigger = SchemaObjectState {
2499            app: "items".into(),
2500            kind: SchemaObjectKind::Trigger,
2501            name: "items_assign_key".into(),
2502            sql: "CREATE TRIGGER items_assign_key AFTER INSERT ON items BEGIN SELECT 1; END".into(),
2503        };
2504        let migration = Migration::new(
2505            MigrationId {
2506                app: "items".into(),
2507                name: "0001_trigger".into(),
2508            },
2509            vec![],
2510            vec![MigrationOperation::CreateSchemaObject {
2511                object: trigger.clone(),
2512            }],
2513        );
2514        let graph = MigrationGraph::new(vec![migration.clone()]).unwrap();
2515        assert_eq!(
2516            graph.replay(Default::default()).unwrap().objects(),
2517            &[trigger]
2518        );
2519        assert!(render_migration_rust(&migration).contains("CreateSchemaObject"));
2520        let mut changed = migration;
2521        let MigrationOperation::CreateSchemaObject { object } = &mut changed.operations[0] else {
2522            unreachable!();
2523        };
2524        object.sql.push_str(" -- changed");
2525        assert_ne!(changed.calculated_checksum(), changed.checksum);
2526    }
2527
2528    #[test]
2529    fn schema_object_diff_recreates_changed_trigger_in_its_app() {
2530        let old = SchemaObjectState {
2531            app: "items".into(),
2532            kind: SchemaObjectKind::Trigger,
2533            name: "items_guard".into(),
2534            sql: "CREATE TRIGGER items_guard BEFORE INSERT ON items BEGIN SELECT 1; END".into(),
2535        };
2536        let mut current = ProjectState::default();
2537        current.add_schema_object(old.clone()).unwrap();
2538        let mut desired = ProjectState::default();
2539        let mut changed = old.clone();
2540        changed.sql =
2541            "CREATE TRIGGER items_guard BEFORE INSERT ON items BEGIN SELECT 2; END".into();
2542        desired.add_schema_object(changed.clone()).unwrap();
2543        assert_eq!(
2544            current.diff(&desired).unwrap(),
2545            vec![
2546                StateChange::DeleteSchemaObject(old),
2547                StateChange::CreateSchemaObject(changed),
2548            ]
2549        );
2550    }
2551
2552    #[test]
2553    fn schema_objects_are_removed_and_restored_around_a_table_change() {
2554        let table = TableState {
2555            name: "items".into(),
2556            columns: vec![column("id")],
2557            unique_constraints: vec![],
2558            indexes: vec![],
2559        };
2560        let key = ModelKey {
2561            app: "items".into(),
2562            name: "Item".into(),
2563        };
2564        let trigger = SchemaObjectState {
2565            app: "items".into(),
2566            kind: SchemaObjectKind::Trigger,
2567            name: "items_guard".into(),
2568            sql: "CREATE TRIGGER items_guard BEFORE INSERT ON items BEGIN SELECT 1; END".into(),
2569        };
2570        let before = ProjectState {
2571            tables: vec![table.clone()],
2572            model_keys: BTreeMap::from([("items".into(), key.clone())]),
2573            objects: vec![trigger.clone()],
2574        };
2575        let mut changed_table = table;
2576        changed_table.columns.push(column("title"));
2577        let after = ProjectState {
2578            tables: vec![changed_table],
2579            model_keys: BTreeMap::from([("items".into(), key)]),
2580            objects: vec![trigger.clone()],
2581        };
2582        let changes = before.diff(&after).unwrap();
2583        assert!(
2584            matches!(changes[0], StateChange::DeleteSchemaObject(ref object) if object == &trigger)
2585        );
2586        assert!(matches!(changes[1], StateChange::AddColumn { ref table, .. } if table == "items"));
2587        assert!(
2588            matches!(changes[2], StateChange::CreateSchemaObject(ref object) if object == &trigger)
2589        );
2590    }
2591
2592    #[test]
2593    fn schema_object_sql_must_match_its_declared_kind_and_name() {
2594        let error = ProjectState::default()
2595            .add_schema_object(SchemaObjectState {
2596                app: "items".into(),
2597                kind: SchemaObjectKind::View,
2598                name: "items_view".into(),
2599                sql: "CREATE TRIGGER items_view BEFORE INSERT ON items BEGIN SELECT 1; END".into(),
2600            })
2601            .unwrap_err()
2602            .to_string();
2603        assert!(error.contains("expected View items_view"));
2604    }
2605
2606    #[test]
2607    fn graph_orders_and_replays_migrations_deterministically() {
2608        let graph = MigrationGraph::new(vec![
2609            Migration {
2610                id: id("blog", "0002_title"),
2611                dependencies: vec![id("blog", "0001_initial")],
2612                checksum: "b".into(),
2613                operations: vec![MigrationOperation::AddColumn {
2614                    table: "posts".into(),
2615                    column: ColumnState {
2616                        field_name: "title".into(),
2617                        name: "title".into(),
2618                        column_type: ColumnType::Text,
2619                        nullable: false,
2620                        primary_key: false,
2621                        unique: false,
2622                        default: None,
2623                        check: None,
2624                        choices: None,
2625                        references: None,
2626                        auto_now_add: false,
2627                        auto_now: false,
2628                    },
2629                }],
2630            },
2631            Migration {
2632                id: id("blog", "0001_initial"),
2633                dependencies: vec![],
2634                checksum: "a".into(),
2635                operations: vec![MigrationOperation::CreateTable {
2636                    table: TableState {
2637                        name: "posts".into(),
2638                        columns: vec![],
2639                        unique_constraints: vec![],
2640                        indexes: vec![],
2641                    },
2642                }],
2643            },
2644        ])
2645        .unwrap();
2646        assert_eq!(
2647            graph
2648                .ordered()
2649                .map(|migration| migration.id.name.as_str())
2650                .collect::<Vec<_>>(),
2651            ["0001_initial", "0002_title"]
2652        );
2653        assert_eq!(
2654            graph.replay(ProjectState::default()).unwrap().tables()[0].columns[0].name,
2655            "title"
2656        );
2657    }
2658
2659    #[test]
2660    fn graph_rejects_invalid_dependencies() {
2661        assert!(matches!(
2662            MigrationGraph::new(vec![migration(id("a", "one"), vec![id("a", "missing")])]),
2663            Err(MigrationError::MissingDependency { .. })
2664        ));
2665        assert!(matches!(
2666            MigrationGraph::new(vec![
2667                migration(id("a", "one"), vec![id("a", "two")]),
2668                migration(id("a", "two"), vec![id("a", "one")])
2669            ]),
2670            Err(MigrationError::CyclicDependency(_))
2671        ));
2672        assert!(matches!(
2673            MigrationGraph::new(vec![
2674                migration(id("a", "one"), vec![]),
2675                migration(id("a", "one"), vec![])
2676            ]),
2677            Err(MigrationError::DuplicateMigration(_))
2678        ));
2679    }
2680
2681    #[test]
2682    fn run_sql_applies_its_state_operations() {
2683        let mut state = ProjectState::default();
2684        MigrationOperation::RunSql {
2685            forward: "CREATE TABLE notes".into(),
2686            reverse: Some("DROP TABLE notes".into()),
2687            state_operations: vec![MigrationOperation::CreateTable {
2688                table: TableState {
2689                    name: "notes".into(),
2690                    columns: vec![],
2691                    unique_constraints: vec![],
2692                    indexes: vec![],
2693                },
2694            }],
2695        }
2696        .state_forwards(&mut state)
2697        .unwrap();
2698        assert_eq!(state.tables()[0].name, "notes");
2699    }
2700
2701    #[test]
2702    fn renames_keep_constraints_and_foreign_keys_in_sync() {
2703        let mut parent_id = column("id");
2704        parent_id.primary_key = true;
2705        let mut child_parent_id = column("parent_id");
2706        child_parent_id.references = Some(ForeignKeyState {
2707            target: "parents(id)".into(),
2708            on_delete: Some("cascade".into()),
2709        });
2710        let mut state = ProjectState {
2711            tables: vec![
2712                TableState {
2713                    name: "parents".into(),
2714                    columns: vec![parent_id, column("title")],
2715                    unique_constraints: vec![vec!["title".into()]],
2716                    indexes: vec![vec!["title".into()]],
2717                },
2718                TableState {
2719                    name: "children".into(),
2720                    columns: vec![column("id"), child_parent_id],
2721                    unique_constraints: vec![],
2722                    indexes: vec![],
2723                },
2724            ],
2725            model_keys: BTreeMap::new(),
2726            objects: vec![],
2727        };
2728
2729        MigrationOperation::RenameTable {
2730            old_name: "parents".into(),
2731            new_name: "accounts".into(),
2732        }
2733        .state_forwards(&mut state)
2734        .unwrap();
2735        MigrationOperation::RenameColumn {
2736            table: "accounts".into(),
2737            old_name: "id".into(),
2738            new_name: "account_id".into(),
2739        }
2740        .state_forwards(&mut state)
2741        .unwrap();
2742        MigrationOperation::RenameColumn {
2743            table: "accounts".into(),
2744            old_name: "title".into(),
2745            new_name: "name".into(),
2746        }
2747        .state_forwards(&mut state)
2748        .unwrap();
2749
2750        assert_eq!(state.tables()[0].unique_constraints, vec![vec!["name"]]);
2751        assert_eq!(state.tables()[0].indexes, vec![vec!["name"]]);
2752        assert_eq!(
2753            state.tables()[1].columns[1]
2754                .references
2755                .as_ref()
2756                .unwrap()
2757                .target,
2758            "accounts(account_id)"
2759        );
2760    }
2761
2762    #[test]
2763    fn removal_rejects_referenced_tables_and_columns() {
2764        let mut child_parent_id = column("parent_id");
2765        child_parent_id.references = Some(ForeignKeyState {
2766            target: "parents(id)".into(),
2767            on_delete: None,
2768        });
2769        let mut state = ProjectState {
2770            tables: vec![
2771                TableState {
2772                    name: "parents".into(),
2773                    columns: vec![column("id")],
2774                    unique_constraints: vec![],
2775                    indexes: vec![],
2776                },
2777                TableState {
2778                    name: "children".into(),
2779                    columns: vec![child_parent_id],
2780                    unique_constraints: vec![],
2781                    indexes: vec![],
2782                },
2783            ],
2784            model_keys: BTreeMap::new(),
2785            objects: vec![],
2786        };
2787
2788        assert!(matches!(
2789            MigrationOperation::RemoveColumn {
2790                table: "parents".into(),
2791                column: "id".into(),
2792            }
2793            .state_forwards(&mut state),
2794            Err(MigrationError::DependentColumn { .. })
2795        ));
2796        assert!(matches!(
2797            MigrationOperation::DropTable {
2798                name: "parents".into(),
2799            }
2800            .state_forwards(&mut state),
2801            Err(MigrationError::DependentTable { .. })
2802        ));
2803    }
2804
2805    #[test]
2806    fn calculated_checksum_detects_modified_migration_content() {
2807        let mut migration = Migration::new(
2808            id("tasks", "0001_initial"),
2809            vec![],
2810            vec![MigrationOperation::RunSql {
2811                forward: "CREATE TABLE tasks (id INTEGER PRIMARY KEY);".into(),
2812                reverse: Some("DROP TABLE tasks;".into()),
2813                state_operations: vec![],
2814            }],
2815        );
2816        assert!(migration.checksum.starts_with("v1:"));
2817        assert!(MigrationGraph::new(vec![migration.clone()]).is_ok());
2818
2819        let MigrationOperation::RunSql { forward, .. } = &mut migration.operations[0] else {
2820            unreachable!();
2821        };
2822        *forward = "CREATE TABLE tasks (id INTEGER PRIMARY KEY, name TEXT);".into();
2823        assert!(matches!(
2824            MigrationGraph::new(vec![migration]),
2825            Err(MigrationError::InvalidChecksum(_))
2826        ));
2827    }
2828
2829    #[test]
2830    fn review_regressions_forward_plan_and_state() {
2831        let a = migration(id("a", "0001"), vec![]);
2832        let b = migration(id("b", "0001"), vec![a.id.clone()]);
2833        let graph = MigrationGraph::new(vec![a.clone(), b.clone()]).unwrap();
2834        assert_eq!(graph.app_heads("a").len(), 1);
2835        assert!(
2836            graph
2837                .plan_to(&BTreeSet::from([a.id.clone(), b.id.clone()]), Some(&a.id))
2838                .unwrap()
2839                .is_empty()
2840        );
2841        let independent = migration(id("c", "0001"), vec![]);
2842        let graph = MigrationGraph::new(vec![a.clone(), independent.clone()]).unwrap();
2843        assert!(
2844            graph
2845                .plan_forward(
2846                    &BTreeSet::from([a.id.clone(), independent.id.clone()]),
2847                    Some(&a.id)
2848                )
2849                .unwrap()
2850                .is_empty()
2851        );
2852        let graph = MigrationGraph::new(vec![Migration::new(
2853            id("bad", "0001"),
2854            vec![],
2855            vec![MigrationOperation::DropTable {
2856                name: "missing".into(),
2857            }],
2858        )])
2859        .unwrap();
2860        assert!(graph.plan_to(&BTreeSet::new(), None).is_err());
2861        let mut checked = column("value");
2862        checked.check = Some("value > 0".into());
2863        let model = ModelState {
2864            key: ModelKey {
2865                app: "a".into(),
2866                name: "Item".into(),
2867            },
2868            table: TableState {
2869                name: "items".into(),
2870                columns: vec![checked],
2871                indexes: vec![],
2872                unique_constraints: vec![],
2873            },
2874        };
2875        let mut state = ProjectState::default();
2876        MigrationOperation::CreateModel {
2877            model: model.clone(),
2878        }
2879        .state_forwards(&mut state)
2880        .unwrap();
2881        assert_eq!(state.models(), vec![model.clone()]);
2882        let graph = MigrationGraph::new(vec![Migration::new(
2883            id("a", "0001"),
2884            vec![],
2885            vec![MigrationOperation::CreateModel {
2886                model: model.clone(),
2887            }],
2888        )])
2889        .unwrap();
2890        assert_eq!(
2891            graph.replay(ProjectState::default()).unwrap().models(),
2892            state.models()
2893        );
2894        assert!(
2895            MigrationOperation::RenameColumn {
2896                table: "items".into(),
2897                old_name: "value".into(),
2898                new_name: "renamed".into()
2899            }
2900            .state_forwards(&mut state)
2901            .is_err()
2902        );
2903        assert_eq!(state.models(), vec![model]);
2904    }
2905
2906    #[test]
2907    fn diff_uses_model_keys_and_does_not_guess_field_renames() {
2908        let key = ModelKey {
2909            app: "items".into(),
2910            name: "Item".into(),
2911        };
2912        let before_model = ModelState {
2913            key: key.clone(),
2914            table: TableState {
2915                name: "items".into(),
2916                columns: vec![column("id"), column("old_title")],
2917                indexes: vec![],
2918                unique_constraints: vec![],
2919            },
2920        };
2921        let after_model = ModelState {
2922            key: key.clone(),
2923            table: TableState {
2924                name: "catalog_items".into(),
2925                columns: vec![column("id"), column("title")],
2926                indexes: vec![vec!["title".into()]],
2927                unique_constraints: vec![],
2928            },
2929        };
2930        let create = MigrationOperation::CreateModel {
2931            model: before_model.clone(),
2932        };
2933        let mut before = ProjectState::default();
2934        create.state_forwards(&mut before).unwrap();
2935        let mut after = ProjectState::default();
2936        MigrationOperation::CreateModel {
2937            model: after_model.clone(),
2938        }
2939        .state_forwards(&mut after)
2940        .unwrap();
2941        assert_eq!(
2942            before.diff(&after).unwrap(),
2943            vec![
2944                StateChange::RenameTable {
2945                    key: key.clone(),
2946                    old_name: "items".into(),
2947                    new_name: "catalog_items".into()
2948                },
2949                StateChange::RemoveColumn {
2950                    key: key.clone(),
2951                    table: "catalog_items".into(),
2952                    column: column("old_title")
2953                },
2954                StateChange::AddColumn {
2955                    key: key.clone(),
2956                    table: "catalog_items".into(),
2957                    column: column("title")
2958                },
2959                StateChange::AddIndex {
2960                    key,
2961                    table: "catalog_items".into(),
2962                    columns: vec!["title".into()]
2963                },
2964            ]
2965        );
2966        let legacy = ProjectState::from_schema(&SchemaSet::new().model::<LegacyDiffModel>());
2967        assert!(legacy.diff(&after).is_err());
2968    }
2969
2970    #[test]
2971    fn diff_ignores_rust_field_rename_when_physical_column_is_preserved() {
2972        let key = ModelKey {
2973            app: "items".into(),
2974            name: "Item".into(),
2975        };
2976        let mut before_column = column("display_title");
2977        before_column.field_name = "title".into();
2978        let mut after_column = before_column.clone();
2979        after_column.field_name = "headline".into();
2980        let state = |column| {
2981            let mut state = ProjectState::default();
2982            MigrationOperation::CreateModel {
2983                model: ModelState {
2984                    key: key.clone(),
2985                    table: TableState {
2986                        name: "items".into(),
2987                        columns: vec![column],
2988                        unique_constraints: vec![],
2989                        indexes: vec![],
2990                    },
2991                },
2992            }
2993            .state_forwards(&mut state)
2994            .unwrap();
2995            state
2996        };
2997        assert!(
2998            state(before_column)
2999                .diff(&state(after_column))
3000                .unwrap()
3001                .is_empty()
3002        );
3003    }
3004
3005    #[test]
3006    fn distinct_field_names_use_a_v2_checksum() {
3007        let mut display_title = column("display_title");
3008        display_title.field_name = "title".into();
3009        let migration = Migration::new(
3010            id("items", "0001_initial"),
3011            vec![],
3012            vec![MigrationOperation::AddColumn {
3013                table: "items".into(),
3014                column: display_title,
3015            }],
3016        );
3017        assert!(migration.checksum.starts_with("v2:"));
3018        assert!(migration.validates_checksum());
3019        let mut changed = migration.clone();
3020        let MigrationOperation::AddColumn { column, .. } = &mut changed.operations[0] else {
3021            unreachable!();
3022        };
3023        column.field_name = "headline".into();
3024        assert!(!changed.validates_checksum());
3025        assert!(render_migration_rust(&migration).contains("field_name: \"title\".into()"));
3026    }
3027
3028    #[test]
3029    fn alter_column_and_constraints_update_historical_state() {
3030        let initial = TableState {
3031            name: "tasks".into(),
3032            columns: vec![column("id"), column("title")],
3033            unique_constraints: vec![],
3034            indexes: vec![],
3035        };
3036        let old_title = column("title");
3037        let mut new_title = old_title.clone();
3038        new_title.nullable = true;
3039        let graph = MigrationGraph::new(vec![Migration::new(
3040            id("tasks", "0001_initial"),
3041            vec![],
3042            vec![
3043                MigrationOperation::CreateTable { table: initial },
3044                MigrationOperation::AddIndex {
3045                    table: "tasks".into(),
3046                    columns: vec!["title".into()],
3047                },
3048                MigrationOperation::AddUniqueConstraint {
3049                    table: "tasks".into(),
3050                    columns: vec!["title".into()],
3051                },
3052                MigrationOperation::AlterColumn {
3053                    table: "tasks".into(),
3054                    old_column: old_title,
3055                    new_column: new_title,
3056                },
3057            ],
3058        )])
3059        .unwrap();
3060        let state = graph.replay(ProjectState::default()).unwrap();
3061        let table = &state.tables()[0];
3062        assert!(table.columns[1].nullable);
3063        assert_eq!(table.indexes, vec![vec!["title"]]);
3064        assert_eq!(table.unique_constraints, vec![vec!["title"]]);
3065    }
3066
3067    #[test]
3068    fn planner_only_moves_forward_and_rejects_branches() {
3069        let first = migration(id("tasks", "0001_initial"), vec![]);
3070        let second = migration(id("tasks", "0002_title"), vec![first.id.clone()]);
3071        let branch = migration(id("tasks", "0002_description"), vec![first.id.clone()]);
3072        let graph =
3073            MigrationGraph::new(vec![first.clone(), second.clone(), branch.clone()]).unwrap();
3074        let applied = BTreeSet::from([first.id.clone()]);
3075        assert!(graph.plan_to(&applied, Some(&second.id)).is_err());
3076        assert_eq!(graph.app_heads("tasks").len(), 2);
3077        let graph = MigrationGraph::new(vec![first.clone(), second.clone()]).unwrap();
3078
3079        assert_eq!(
3080            graph
3081                .plan_to(&applied, Some(&second.id))
3082                .unwrap()
3083                .iter()
3084                .map(|step| step.id.name.as_str())
3085                .collect::<Vec<_>>(),
3086            vec!["0002_title"]
3087        );
3088        assert!(
3089            graph
3090                .plan_to(
3091                    &BTreeSet::from([first.id.clone(), second.id.clone()]),
3092                    Some(&first.id)
3093                )
3094                .is_err()
3095        );
3096        assert_eq!(
3097            graph
3098                .app_heads("tasks")
3099                .iter()
3100                .map(|migration| migration.id.name.as_str())
3101                .collect::<Vec<_>>(),
3102            vec!["0002_title"]
3103        );
3104    }
3105
3106    #[test]
3107    fn planner_rejects_inconsistent_history_and_irreversible_rollback() {
3108        let first = migration(id("tasks", "0001_initial"), vec![]);
3109        let irreversible = Migration::new(
3110            id("tasks", "0002_data"),
3111            vec![first.id.clone()],
3112            vec![MigrationOperation::RunSql {
3113                forward: "UPDATE tasks SET title = trim(title);".into(),
3114                reverse: None,
3115                state_operations: vec![],
3116            }],
3117        );
3118        let graph = MigrationGraph::new(vec![first.clone(), irreversible.clone()]).unwrap();
3119        assert!(matches!(
3120            graph.plan_to(&BTreeSet::from([irreversible.id.clone()]), None),
3121            Err(MigrationError::InconsistentAppliedHistory { .. })
3122        ));
3123        assert!(matches!(
3124            graph.plan_to(
3125                &BTreeSet::from([first.id.clone(), irreversible.id.clone()]),
3126                Some(&first.id)
3127            ),
3128            Err(MigrationError::InvalidState(_))
3129        ));
3130    }
3131
3132    #[test]
3133    #[cfg(feature = "sqlite")]
3134    fn sqlite_editor_rebuilds_a_table_and_preserves_existing_rows() {
3135        let create = MigrationOperation::CreateTable {
3136            table: TableState {
3137                name: "items".into(),
3138                columns: vec![column("id")],
3139                unique_constraints: vec![],
3140                indexes: vec![],
3141            },
3142        };
3143        let initial = SqliteSchemaEditor::plan(&create, &ProjectState::default()).unwrap();
3144        let connection = crate::rusqlite::Connection::open_in_memory().unwrap();
3145        connection
3146            .execute_batch(&(initial.statements.join("; ") + ";"))
3147            .unwrap();
3148        connection
3149            .execute("INSERT INTO items (id) VALUES (7)", [])
3150            .unwrap();
3151
3152        let mut name = column("name");
3153        name.column_type = ColumnType::Text;
3154        name.default = Some("'unknown'".into());
3155        let add_column = MigrationOperation::AddColumn {
3156            table: "items".into(),
3157            column: name,
3158        };
3159        let rebuild = SqliteSchemaEditor::plan(&add_column, &initial.resulting_state).unwrap();
3160        assert!(rebuild
3161            .statements
3162            .iter()
3163            .any(|statement| statement.starts_with("INSERT INTO \"__che_migration_new_items\"")));
3164        connection
3165            .execute_batch(&(rebuild.statements.join("; ") + ";"))
3166            .unwrap();
3167        let row: (i64, String) = connection
3168            .query_row("SELECT id, name FROM items", [], |row| {
3169                Ok((row.get(0)?, row.get(1)?))
3170            })
3171            .unwrap();
3172        assert_eq!(row, (7, "unknown".into()));
3173    }
3174
3175    #[test]
3176    fn backfill_rejects_sql_comments_that_could_change_its_null_filter() {
3177        let mut state = ProjectState::default();
3178        MigrationOperation::CreateTable {
3179            table: TableState {
3180                name: "items".into(),
3181                columns: vec![column("title")],
3182                unique_constraints: vec![],
3183                indexes: vec![],
3184            },
3185        }
3186        .state_forwards(&mut state)
3187        .unwrap();
3188
3189        for value in ["0 WHERE 1=1 --", "0 /* WHERE title IS NULL */"] {
3190            assert!(matches!(
3191                (MigrationOperation::BackfillColumn {
3192                    table: "items".into(),
3193                    column: "title".into(),
3194                    value: value.into(),
3195                })
3196                .state_forwards(&mut state.clone()),
3197                Err(MigrationError::InvalidState(_))
3198            ));
3199        }
3200    }
3201
3202    #[test]
3203    fn state_changes_become_replayable_forward_operations() {
3204        let key = ModelKey {
3205            app: "tasks".into(),
3206            name: "Task".into(),
3207        };
3208        let model = ModelState {
3209            key: key.clone(),
3210            table: TableState {
3211                name: "tasks".into(),
3212                columns: vec![column("id")],
3213                unique_constraints: vec![],
3214                indexes: vec![],
3215            },
3216        };
3217        let mut expected = model.clone();
3218        expected.table.columns.push(column("title"));
3219        let changes = vec![
3220            StateChange::CreateModel(model.clone()),
3221            StateChange::AddColumn {
3222                key,
3223                table: "tasks".into(),
3224                column: column("title"),
3225            },
3226        ];
3227        let operations = operations_from_changes(changes);
3228        let mut state = ProjectState::default();
3229        for operation in &operations {
3230            operation.state_forwards(&mut state).unwrap();
3231        }
3232        assert_eq!(state.models(), vec![expected]);
3233        assert_eq!(state.tables()[0].columns[1].name, "title");
3234    }
3235
3236    #[test]
3237    fn rust_writer_is_deterministic_and_uses_migration_constructor() {
3238        let mut title = column("title");
3239        title.default = Some("'quoted value'".into());
3240        title.references = Some(ForeignKeyState {
3241            target: "accounts(id)".into(),
3242            on_delete: Some("cascade".into()),
3243        });
3244        let migration = Migration::new(
3245            id("tasks", "0002_title"),
3246            vec![id("accounts", "0001_initial")],
3247            vec![
3248                MigrationOperation::RunSql {
3249                    forward: "UPDATE tasks SET title = 'ready';".into(),
3250                    reverse: None,
3251                    state_operations: vec![MigrationOperation::AddColumn {
3252                        table: "tasks".into(),
3253                        column: title,
3254                    }],
3255                },
3256                MigrationOperation::BackfillColumn {
3257                    table: "tasks".into(),
3258                    column: "title".into(),
3259                    value: "'untitled'".into(),
3260                },
3261            ],
3262        );
3263        let source = render_migration_rust(&migration);
3264        assert_eq!(source, render_migration_rust(&migration));
3265        assert!(source.contains("Migration::new("));
3266        assert!(source.contains("accounts"));
3267        assert!(source.contains("UPDATE tasks SET title"));
3268        assert!(source.contains("MigrationOperation::BackfillColumn"));
3269        assert!(!source.contains(&migration.checksum));
3270    }
3271}