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