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