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        previous_template: Vec<String>,
27    },
28    GenerationCounterSnapshot {
29        previous: u64,
30    },
31    PendingValidationSnapshot {
32        previous: HashSet<(ObjectId, String)>,
33    },
34    GraphLengthMarker {
35        len: usize,
36    },
37    GraphSnapshot {
38        previous: Vec<DependencyEdge>,
39    },
40    FunctionSnapshot {
41        id: ObjectId,
42        previous: Option<crate::model::function::FunctionOverlay>,
43    },
44    PublicationSnapshot {
45        id: ObjectId,
46        previous: Option<crate::model::replication::PublicationOverlay>,
47    },
48    SubscriptionSnapshot {
49        id: ObjectId,
50        previous: Option<crate::model::replication::SubscriptionOverlay>,
51    },
52    RoleSnapshot {
53        id: ObjectId,
54        previous: Option<crate::model::role::RoleOverlay>,
55    },
56    TriggerSnapshot {
57        id: ObjectId,
58        previous: Option<crate::model::trigger::TriggerOverlay>,
59    },
60    ConstraintSnapshot {
61        table_id: ObjectId,
62        name: String,
63        previous: Option<crate::model::constraint::ConstraintState>,
64    },
65    RoleContextSnapshot {
66        current_role: String,
67        current_role_known: bool,
68        persistent_current_role: String,
69        persistent_current_role_known: bool,
70        session_role: String,
71        session_role_known: bool,
72        persistent_session_role: String,
73        persistent_session_role_known: bool,
74    },
75    ConfidenceSnapshot {
76        previous: crate::analysis::state::Confidence,
77    },
78}
79
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum TransactionFrameKind {
82    Root,
83    Savepoint(String),
84}
85
86#[derive(Debug, Clone)]
87pub struct TransactionFrame {
88    pub kind: TransactionFrameKind,
89    pub undo_log: Vec<StateChange>,
90}
91
92impl TransactionFrame {
93    pub fn root() -> Self {
94        Self {
95            kind: TransactionFrameKind::Root,
96            undo_log: Vec::new(),
97        }
98    }
99
100    pub fn savepoint(name: impl Into<String>) -> Self {
101        Self {
102            kind: TransactionFrameKind::Savepoint(name.into()),
103            undo_log: Vec::new(),
104        }
105    }
106
107    pub fn is_named_savepoint(&self, name: &str) -> bool {
108        matches!(&self.kind, TransactionFrameKind::Savepoint(candidate) if candidate == name)
109    }
110}