toolu_orm_core/
ordering.rs1use crate::diff::Operation;
4
5pub 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}