Skip to main content

safe_migrate/analysis/
transaction.rs

1// FILE: src/analysis/transaction.rs
2
3use crate::analysis::graph::DependencyEdge;
4use crate::ast::identifiers::ObjectId;
5use crate::model::relation::RelationOverlay;
6use crate::model::sequence::SequenceOverlay;
7use crate::model::types::TypeOverlay;
8use std::collections::HashSet;
9
10#[derive(Debug, Clone)]
11pub enum StateChange {
12    RelationSnapshot {
13        id: ObjectId,
14        previous: Box<Option<RelationOverlay>>,
15    },
16    TypeSnapshot {
17        id: ObjectId,
18        previous: Option<TypeOverlay>,
19    },
20    SequenceSnapshot {
21        id: ObjectId,
22        previous: Option<SequenceOverlay>,
23    },
24    SearchPathSnapshot {
25        previous: Vec<String>,
26    },
27    GenerationCounterSnapshot {
28        previous: u64,
29    },
30    PendingValidationSnapshot {
31        previous: HashSet<(ObjectId, String)>,
32    },
33    GraphLengthMarker {
34        len: usize,
35    },
36    GraphSnapshot {
37        previous: Vec<DependencyEdge>,
38    },
39    FunctionSnapshot {
40        id: ObjectId,
41        previous: Option<crate::model::function::FunctionOverlay>,
42    },
43    PublicationSnapshot {
44        id: ObjectId,
45        previous: Option<crate::model::replication::PublicationOverlay>,
46    },
47    SubscriptionSnapshot {
48        id: ObjectId,
49        previous: Option<crate::model::replication::SubscriptionOverlay>,
50    },
51    RoleSnapshot {
52        id: ObjectId,
53        previous: Option<crate::model::role::RoleOverlay>,
54    },
55    TriggerSnapshot {
56        id: ObjectId,
57        previous: Option<crate::model::trigger::TriggerOverlay>,
58    },
59    ConstraintSnapshot {
60        table_id: ObjectId,
61        name: String,
62        previous: Option<crate::model::constraint::ConstraintState>,
63    },
64    CurrentRoleSnapshot {
65        previous: String,
66    },
67    ConfidenceSnapshot {
68        previous: crate::analysis::state::Confidence,
69    },
70}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
73pub enum TransactionFrameKind {
74    Root,
75    Savepoint(String),
76}
77
78#[derive(Debug, Clone)]
79pub struct TransactionFrame {
80    pub kind: TransactionFrameKind,
81    pub undo_log: Vec<StateChange>,
82}
83
84impl TransactionFrame {
85    pub fn root() -> Self {
86        Self {
87            kind: TransactionFrameKind::Root,
88            undo_log: Vec::new(),
89        }
90    }
91
92    pub fn savepoint(name: impl Into<String>) -> Self {
93        Self {
94            kind: TransactionFrameKind::Savepoint(name.into()),
95            undo_log: Vec::new(),
96        }
97    }
98
99    pub fn is_named_savepoint(&self, name: &str) -> bool {
100        matches!(&self.kind, TransactionFrameKind::Savepoint(candidate) if candidate == name)
101    }
102}