Skip to main content

toolu_orm_core/
ordering.rs

1//! Operation ordering for migrations using a 13-tier priority system.
2
3use crate::diff::Operation;
4
5/// Sorts operations into dependency-safe execution order.
6pub fn order_operations(ops: Vec<Operation>) -> Vec<Operation> {
7  let mut indexed: Vec<(usize, Operation)> = ops.into_iter().enumerate().collect();
8  indexed.sort_by_key(|(i, op)| (priority(op), *i));
9  indexed.into_iter().map(|(_, op)| op).collect()
10}
11
12fn priority(op: &Operation) -> u8 {
13  match op {
14    Operation::CreateEnum { .. } => 1,
15    Operation::AlterEnum { removed, .. } if removed.is_empty() => 2,
16    Operation::CreateTable { .. } => 3,
17    Operation::RenameTable { .. } => 4,
18    Operation::RenameColumn { .. } => 5,
19    Operation::DropForeignKey { .. }
20    | Operation::DropIndex { .. }
21    | Operation::DropCheckConstraint { .. } => 6,
22    Operation::AlterColumn { .. } => 7,
23    Operation::AddColumn { .. } => 8,
24    Operation::AddForeignKey { .. }
25    | Operation::CreateIndex { .. }
26    | Operation::AddCheckConstraint { .. } => 9,
27    Operation::DropColumn { .. } => 10,
28    Operation::DropTable { .. } => 11,
29    Operation::DropEnum { .. } => 12,
30    Operation::AlterEnum { .. } => 13,
31  }
32}