1use serde::{Deserialize, Serialize};
4use type_bridge_orm::_schema::info::{
5 AttributeSchemaEntry, EntitySchemaEntry, OwnedAttributeEntry, RelationSchemaEntry, RoleEntry,
6 SchemaInfo,
7};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct MigrationDependencySpec {
12 pub app_label: String,
14 pub migration_name: String,
16}
17
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub struct MigrationSpec {
21 pub app_label: String,
23 pub name: String,
25 #[serde(default)]
27 pub dependencies: Vec<MigrationDependencySpec>,
28 #[serde(default)]
30 pub operations: Vec<OperationSpec>,
31 #[serde(default)]
33 pub checksum: Option<String>,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub source_sha256: Option<String>,
37 pub reversible: bool,
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
45pub struct MigrationGraph {
46 pub migrations: Vec<MigrationSpec>,
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52#[serde(tag = "kind", rename_all = "snake_case")]
53pub enum OperationSpec {
54 DefineSchema {
56 schema: SchemaInfo,
58 },
59 AddAttribute {
61 attribute: AttributeSchemaEntry,
63 },
64 RemoveAttribute {
66 attr_name: String,
68 },
69 AddEntity {
71 entity: EntitySchemaEntry,
73 },
74 RemoveEntity {
76 type_name: String,
78 },
79 AddRelation {
81 relation: RelationSchemaEntry,
83 },
84 RemoveRelation {
86 type_name: String,
88 },
89 AddOwnership {
91 owner_type: String,
93 attribute: OwnedAttributeEntry,
95 },
96 RemoveOwnership {
98 owner_type: String,
100 attr_name: String,
102 },
103 ModifyOwnership {
105 owner_type: String,
107 attr_name: String,
109 old_annotations: String,
111 new_annotations: String,
113 },
114 ModifyTypeAnnotations {
117 type_name: String,
119 #[serde(default)]
121 old_doc: Option<String>,
122 #[serde(default)]
124 new_doc: Option<String>,
125 #[serde(default)]
127 old_meta: std::collections::BTreeMap<String, String>,
128 #[serde(default)]
130 new_meta: std::collections::BTreeMap<String, String>,
131 },
132 ModifyRoleAnnotations {
134 relation_type: String,
136 role_name: String,
138 #[serde(default)]
140 old_doc: Option<String>,
141 #[serde(default)]
143 new_doc: Option<String>,
144 #[serde(default)]
146 old_meta: std::collections::BTreeMap<String, String>,
147 #[serde(default)]
149 new_meta: std::collections::BTreeMap<String, String>,
150 },
151 AddRole {
154 relation_type: String,
156 role: RoleEntry,
158 },
159 RemoveRole {
161 relation_type: String,
163 role_name: String,
165 },
166 AddRolePlayer {
168 relation_type: String,
170 role_name: String,
172 player_type_name: String,
174 },
175 RemoveRolePlayer {
177 relation_type: String,
179 role_name: String,
181 player_type_name: String,
183 },
184 RunTypeql {
186 forward: String,
188 #[serde(default)]
190 reverse: Option<String>,
191 },
192 RenameAttribute {
194 old_name: String,
196 new_name: String,
198 value_type: String,
200 },
201 CopyAttribute {
215 #[serde(default, skip_serializing_if = "Option::is_none")]
217 owner: Option<String>,
218 #[serde(default, skip_serializing_if = "Option::is_none")]
220 source: Option<String>,
221 #[serde(default, skip_serializing_if = "Option::is_none")]
223 dest: Option<String>,
224 #[serde(default, skip_serializing_if = "Option::is_none")]
226 filter: Option<String>,
227 #[serde(default, skip_serializing_if = "Option::is_none")]
231 forward: Option<String>,
232 #[serde(default)]
235 reverse: Option<String>,
236 },
237}
238
239pub fn copy_attribute_typeql(op: &OperationSpec) -> crate::Result<(String, Option<String>)> {
251 let OperationSpec::CopyAttribute {
252 owner,
253 source,
254 dest,
255 filter,
256 forward,
257 reverse,
258 } = op
259 else {
260 return Err(crate::error::MigrationError::AuthoringInput {
261 message: "copy_attribute_typeql called on a non-copy_attribute operation".to_string(),
262 });
263 };
264 if let Some(forward) = forward {
265 return Ok((forward.clone(), reverse.clone()));
266 }
267 let (Some(owner), Some(source), Some(dest)) = (owner, source, dest) else {
268 return Err(crate::error::MigrationError::AuthoringInput {
269 message: "copy_attribute requires either forward TypeQL or the structured \
270 owner/source/dest fields"
271 .to_string(),
272 });
273 };
274 use type_bridge_core_lib::__internal as tql;
275 let mut patterns = vec![
276 tql::thing(
277 "x",
278 vec![tql::isa(owner, false)?, tql::has(source, tql::var("v")?)?],
279 )?,
280 tql::not(vec![tql::thing(
281 "x",
282 vec![tql::has(dest, tql::var("d")?)?],
283 )?]),
284 ];
285 if let Some(filter) = filter {
286 patterns.extend(tql::parse_patterns(filter)?);
287 }
288 let synthesized_forward = tql::render(&tql::pipeline(vec![
289 tql::match_stage(patterns),
290 tql::insert(vec![tql::thing(
291 "x",
292 vec![tql::has_comparison(dest, "==", tql::var("v")?)?],
293 )?]),
294 ]))?;
295 let synthesized_reverse = tql::render(&tql::pipeline(vec![
296 tql::match_stage(vec![tql::thing(
297 "x",
298 vec![tql::isa(owner, false)?, tql::has(dest, tql::var("v")?)?],
299 )?]),
300 tql::delete(vec![tql::delete_has("x", "v")?]),
301 ]))?;
302 Ok((
303 synthesized_forward,
304 Some(reverse.clone().unwrap_or(synthesized_reverse)),
305 ))
306}
307
308impl OperationSpec {
309 pub fn normalized(self) -> crate::Result<OperationSpec> {
317 if !matches!(self, OperationSpec::CopyAttribute { .. }) {
318 return Ok(self);
319 }
320 let (forward, reverse) = copy_attribute_typeql(&self)?;
321 let OperationSpec::CopyAttribute {
322 owner,
323 source,
324 dest,
325 filter,
326 ..
327 } = self
328 else {
329 unreachable!("guarded by the matches! check above");
330 };
331 Ok(OperationSpec::CopyAttribute {
332 owner,
333 source,
334 dest,
335 filter,
336 forward: Some(forward),
337 reverse,
338 })
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use std::collections::BTreeMap;
345
346 use super::*;
347 use type_bridge_orm::_entity::Annotation;
348 use type_bridge_orm::ValueType;
349
350 fn attribute(name: &str) -> AttributeSchemaEntry {
351 AttributeSchemaEntry::new(name, ValueType::String)
352 }
353
354 fn schema() -> SchemaInfo {
355 let mut schema = SchemaInfo::default();
356 schema
357 .attributes
358 .insert("name".to_string(), attribute("name"));
359 schema.entities.insert(
360 "person".to_string(),
361 EntitySchemaEntry {
362 type_name: "person".to_string(),
363 is_abstract: false,
364 parent_type: None,
365 owned_attributes: vec![OwnedAttributeEntry {
366 attr_name: "name".to_string(),
367 value_type: ValueType::String,
368 annotations: vec![Annotation::Key],
369 is_ordered: false,
370 doc: None,
371 meta: Default::default(),
372 }],
373 plays_cardinalities: BTreeMap::new(),
374 doc: None,
375 meta: Default::default(),
376 },
377 );
378 schema
379 }
380
381 #[test]
382 fn define_schema_operation_round_trips_json() {
383 let operation = OperationSpec::DefineSchema { schema: schema() };
384
385 let json = serde_json::to_string(&operation).unwrap();
386 assert!(json.contains("\"kind\":\"define_schema\""));
387
388 let parsed: OperationSpec = serde_json::from_str(&json).unwrap();
389 assert_eq!(parsed, operation);
390 }
391
392 #[test]
393 fn schema_bearing_operation_round_trips_json() {
394 let operation = OperationSpec::AddOwnership {
395 owner_type: "person".to_string(),
396 attribute: OwnedAttributeEntry {
397 attr_name: "name".to_string(),
398 value_type: ValueType::String,
399 annotations: vec![Annotation::Key],
400 is_ordered: false,
401 doc: None,
402 meta: Default::default(),
403 },
404 };
405
406 let parsed: OperationSpec =
407 serde_json::from_str(&serde_json::to_string(&operation).unwrap()).unwrap();
408
409 assert_eq!(parsed, operation);
410 }
411
412 #[test]
413 fn run_typeql_operation_round_trips_json() {
414 let operation = OperationSpec::RunTypeql {
415 forward: "define attribute nickname, value string;".to_string(),
416 reverse: Some("undefine attribute nickname;".to_string()),
417 };
418
419 let json = serde_json::to_value(&operation).unwrap();
420 assert_eq!(json["kind"], "run_typeql");
421
422 let parsed: OperationSpec = serde_json::from_value(json).unwrap();
423 assert_eq!(parsed, operation);
424 }
425
426 fn lowered_copy_attribute(forward: &str, reverse: Option<&str>) -> OperationSpec {
427 OperationSpec::CopyAttribute {
428 owner: None,
429 source: None,
430 dest: None,
431 filter: None,
432 forward: Some(forward.to_string()),
433 reverse: reverse.map(str::to_string),
434 }
435 }
436
437 fn structured_copy_attribute(filter: Option<&str>) -> OperationSpec {
438 OperationSpec::CopyAttribute {
439 owner: Some("person".to_string()),
440 source: Some("old-name".to_string()),
441 dest: Some("new-name".to_string()),
442 filter: filter.map(str::to_string),
443 forward: None,
444 reverse: None,
445 }
446 }
447
448 #[test]
449 fn copy_attribute_operation_round_trips_json() {
450 let operation = lowered_copy_attribute(
451 "match\n $x isa person, has old-name $v;\n not { $x has new-name $d; };\ninsert\n $x has new-name == $v;",
452 Some("match $x isa person, has new-name $v;\ndelete $v of $x;"),
453 );
454
455 let json = serde_json::to_value(&operation).unwrap();
456 assert_eq!(json["kind"], "copy_attribute");
457 assert!(
458 json["forward"]
459 .as_str()
460 .unwrap()
461 .contains("has new-name == $v")
462 );
463 assert!(json.get("owner").is_none());
466
467 let parsed: OperationSpec = serde_json::from_value(json).unwrap();
468 assert_eq!(parsed, operation);
469 }
470
471 #[test]
472 fn copy_attribute_without_reverse_round_trips_json() {
473 let operation = lowered_copy_attribute(
474 "match\n $x isa company, has legacy-id $v;\n not { $x has new-id $d; };\ninsert\n $x has new-id == $v;",
475 None,
476 );
477
478 let json = serde_json::to_value(&operation).unwrap();
479 assert_eq!(json["kind"], "copy_attribute");
480 let parsed: OperationSpec = serde_json::from_value(json).unwrap();
483 assert_eq!(parsed, operation);
484 }
485
486 #[test]
487 fn legacy_copy_attribute_sidecar_json_still_parses() {
488 let json = r#"{"kind":"copy_attribute","forward":"match ...;","reverse":null}"#;
490
491 let parsed: OperationSpec = serde_json::from_str(json).unwrap();
492 assert_eq!(parsed, lowered_copy_attribute("match ...;", None));
493 }
494
495 #[test]
496 fn structured_copy_attribute_round_trips_json() {
497 let operation = structured_copy_attribute(Some("$x has age $a;"));
498
499 let json = serde_json::to_value(&operation).unwrap();
500 assert_eq!(json["kind"], "copy_attribute");
501 assert_eq!(json["owner"], "person");
502 assert!(json.get("forward").is_none());
503
504 let parsed: OperationSpec = serde_json::from_value(json).unwrap();
505 assert_eq!(parsed, operation);
506 }
507
508 #[test]
509 fn structured_copy_attribute_synthesizes_python_shaped_typeql() {
510 let (forward, reverse) = copy_attribute_typeql(&structured_copy_attribute(None)).unwrap();
511
512 assert_typeql_eq!(
513 forward,
514 "match\n $x isa person, has old-name $v;\n not { $x has new-name $d; };\ninsert\n $x has new-name == $v;"
515 );
516 assert_typeql_eq!(
517 reverse.as_deref(),
518 Some("match $x isa person, has new-name $v;\ndelete $v of $x;")
519 );
520 }
521
522 #[test]
523 fn structured_copy_attribute_synthesizes_filter_line() {
524 let (forward, _) =
527 copy_attribute_typeql(&structured_copy_attribute(Some("$x has age $a"))).unwrap();
528
529 assert_typeql_eq!(
530 forward,
531 "match\n $x isa person, has old-name $v;\n not { $x has new-name $d; };\n $x has age $a;\ninsert\n $x has new-name == $v;"
532 );
533 }
534
535 #[test]
536 fn carried_typeql_wins_over_structured_fields() {
537 let operation = OperationSpec::CopyAttribute {
538 owner: Some("person".to_string()),
539 source: Some("old-name".to_string()),
540 dest: Some("new-name".to_string()),
541 filter: None,
542 forward: Some("match carried;".to_string()),
543 reverse: Some("match carried-reverse;".to_string()),
544 };
545
546 let (forward, reverse) = copy_attribute_typeql(&operation).unwrap();
547 assert_eq!(forward, "match carried;");
548 assert_eq!(reverse.as_deref(), Some("match carried-reverse;"));
549 }
550
551 #[test]
552 fn copy_attribute_without_typeql_or_fields_is_rejected() {
553 let operation = OperationSpec::CopyAttribute {
554 owner: Some("person".to_string()),
555 source: None,
556 dest: Some("new-name".to_string()),
557 filter: None,
558 forward: None,
559 reverse: None,
560 };
561
562 let error = copy_attribute_typeql(&operation).unwrap_err();
563 assert!(matches!(
564 error,
565 crate::error::MigrationError::AuthoringInput { .. }
566 ));
567 }
568
569 #[test]
570 fn normalized_fills_structured_copy_attribute_and_passes_others_through() {
571 let normalized = structured_copy_attribute(None).normalized().unwrap();
572 let OperationSpec::CopyAttribute {
573 owner,
574 forward,
575 reverse,
576 ..
577 } = &normalized
578 else {
579 panic!("normalized must stay a copy_attribute");
580 };
581 assert_eq!(owner.as_deref(), Some("person"));
582 assert!(forward.as_deref().unwrap().contains("has new-name == $v"));
583 assert!(reverse.as_deref().unwrap().contains("delete has $v of $x"));
584
585 let passthrough = OperationSpec::RunTypeql {
586 forward: "match $x isa person;".to_string(),
587 reverse: None,
588 };
589 assert_eq!(passthrough.clone().normalized().unwrap(), passthrough);
590 }
591
592 #[test]
593 fn graph_preserves_spec_order() {
594 let graph = MigrationGraph {
595 migrations: vec![
596 MigrationSpec {
597 app_label: "app".to_string(),
598 name: "0001_initial".to_string(),
599 dependencies: vec![],
600 operations: vec![OperationSpec::DefineSchema { schema: schema() }],
601 checksum: Some("aaa".to_string()),
602 source_sha256: None,
603 reversible: true,
604 },
605 MigrationSpec {
606 app_label: "app".to_string(),
607 name: "0002_custom".to_string(),
608 dependencies: vec![MigrationDependencySpec {
609 app_label: "app".to_string(),
610 migration_name: "0001_initial".to_string(),
611 }],
612 operations: vec![OperationSpec::RunTypeql {
613 forward: "define attribute nickname, value string;".to_string(),
614 reverse: None,
615 }],
616 checksum: Some("bbb".to_string()),
617 source_sha256: None,
618 reversible: false,
619 },
620 ],
621 };
622
623 let parsed: MigrationGraph =
624 serde_json::from_str(&serde_json::to_string(&graph).unwrap()).unwrap();
625
626 assert_eq!(parsed.migrations[0].name, "0001_initial");
627 assert_eq!(parsed.migrations[1].name, "0002_custom");
628 assert_eq!(parsed, graph);
629 }
630
631 #[test]
632 fn absent_raw_digest_preserves_the_legacy_sidecar_wire_shape() {
633 let spec = MigrationSpec {
634 app_label: "app".to_string(),
635 name: "0001_initial".to_string(),
636 dependencies: vec![],
637 operations: vec![],
638 checksum: Some("aaa".to_string()),
639 source_sha256: None,
640 reversible: true,
641 };
642
643 assert_eq!(
644 serde_json::to_string(&spec).unwrap(),
645 r#"{"app_label":"app","name":"0001_initial","dependencies":[],"operations":[],"checksum":"aaa","reversible":true}"#,
646 );
647 }
648
649 #[test]
650 fn annotation_operations_round_trip_json() {
651 let operations = vec![
652 OperationSpec::ModifyTypeAnnotations {
653 type_name: "person".to_string(),
654 old_doc: None,
655 new_doc: Some("A person.".to_string()),
656 old_meta: BTreeMap::new(),
657 new_meta: BTreeMap::from([("owner".to_string(), "core".to_string())]),
658 },
659 OperationSpec::ModifyRoleAnnotations {
660 relation_type: "employment".to_string(),
661 role_name: "employee".to_string(),
662 old_doc: Some("old".to_string()),
663 new_doc: None,
664 old_meta: BTreeMap::new(),
665 new_meta: BTreeMap::new(),
666 },
667 ];
668 for operation in operations {
669 let json = serde_json::to_string(&operation).expect("serialize");
670 let back: OperationSpec = serde_json::from_str(&json).expect("deserialize");
671 assert_eq!(back, operation);
672 }
673 let json = serde_json::to_string(&OperationSpec::ModifyTypeAnnotations {
675 type_name: "person".to_string(),
676 old_doc: None,
677 new_doc: None,
678 old_meta: BTreeMap::new(),
679 new_meta: BTreeMap::new(),
680 })
681 .expect("serialize");
682 assert!(json.contains("\"kind\":\"modify_type_annotations\""));
683 }
684}