Skip to main content

uqa_sql/schema/table_alteration/
ordering.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! `PostgreSQL` ALTER execution categories; written order is retained within each category.
8
9use crate::ast::AlterTableAction;
10
11#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub enum AlterOrder {
13    Drop,
14    Type,
15    AddColumn,
16    SetExpression,
17    AddConstraint,
18    ColumnAttributes,
19    AddKey,
20    AddDefault,
21    Other,
22}
23
24pub fn execution_order(action: &AlterTableAction) -> AlterOrder {
25    match action {
26        AlterTableAction::DropColumn { .. }
27        | AlterTableAction::DropConstraint { .. }
28        | AlterTableAction::DropDefault { .. }
29        | AlterTableAction::DropNotNull { .. }
30        | AlterTableAction::DropIdentity { .. }
31        | AlterTableAction::DropExpression { .. } => AlterOrder::Drop,
32        AlterTableAction::AlterColumnType { .. } => AlterOrder::Type,
33        AlterTableAction::AddColumn { .. } => AlterOrder::AddColumn,
34        AlterTableAction::SetExpression { .. } => AlterOrder::SetExpression,
35        AlterTableAction::AddCheckConstraint { .. }
36        | AlterTableAction::AddNotNullConstraint { .. }
37        | AlterTableAction::AddForeignKeyConstraint { .. } => AlterOrder::AddConstraint,
38        AlterTableAction::SetNotNull { .. } => AlterOrder::ColumnAttributes,
39        AlterTableAction::AddKeyConstraint { .. } => AlterOrder::AddKey,
40        AlterTableAction::SetDefault { .. } | AlterTableAction::AddIdentity { .. } => {
41            AlterOrder::AddDefault
42        }
43        _ => AlterOrder::Other,
44    }
45}