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