1use crate::analysis::graph::DependencyEdge;
2use crate::ast::identifiers::ObjectId;
3use crate::model::relation::RelationOverlay;
4use crate::model::schema::SchemaOverlay;
5use crate::model::sequence::SequenceOverlay;
6use crate::model::types::TypeOverlay;
7use std::collections::{HashMap, HashSet};
8
9#[derive(Debug, Clone)]
10pub struct NamespaceSnapshot {
11 pub schemas: HashMap<String, SchemaOverlay>,
12 pub relations: HashMap<ObjectId, RelationOverlay>,
13 pub types: HashMap<ObjectId, TypeOverlay>,
14 pub functions: HashMap<ObjectId, crate::model::function::FunctionOverlay>,
15 pub sequences: HashMap<ObjectId, SequenceOverlay>,
16 pub publications: HashMap<String, crate::model::replication::PublicationOverlay>,
17 pub triggers: HashMap<ObjectId, crate::model::trigger::TriggerOverlay>,
18 pub constraints: HashMap<(ObjectId, String), crate::model::constraint::ConstraintState>,
19 pub graph: Vec<DependencyEdge>,
20 pub pending_validation: HashSet<(ObjectId, String)>,
21 pub baseline_relations: HashSet<ObjectId>,
22 pub baseline_indexes: HashSet<ObjectId>,
23 pub baseline_foreign_keys: HashSet<(ObjectId, String)>,
24 pub baseline_fk_dependencies: HashSet<ObjectId>,
25 pub baseline_sequences: HashSet<ObjectId>,
26}
27
28#[derive(Debug, Clone)]
29pub enum StateChange {
30 SchemaSnapshot {
31 name: String,
32 previous: Option<SchemaOverlay>,
33 },
34 NamespaceSnapshot(Box<NamespaceSnapshot>),
35 RelationSnapshot {
36 id: ObjectId,
37 previous: Box<Option<RelationOverlay>>,
38 },
39 TypeSnapshot {
40 id: ObjectId,
41 previous: Option<TypeOverlay>,
42 },
43 SequenceSnapshot {
44 id: ObjectId,
45 previous: Option<SequenceOverlay>,
46 },
47 SearchPathSnapshot {
48 previous: Vec<String>,
49 previous_template: Vec<String>,
50 previous_session_template: Vec<String>,
51 },
52 TimeoutSettingsSnapshot {
53 lock_timeout: crate::analysis::settings::ScopedSetting<Option<u64>>,
54 statement_timeout: crate::analysis::settings::ScopedSetting<Option<u64>>,
55 },
56 GenerationCounterSnapshot {
57 previous: u64,
58 },
59 PendingValidationSnapshot {
60 previous: HashSet<(ObjectId, String)>,
61 },
62 GraphLengthMarker {
63 len: usize,
64 },
65 GraphSnapshot {
66 previous: Vec<DependencyEdge>,
67 },
68 FunctionSnapshot {
69 id: ObjectId,
70 previous: Option<crate::model::function::FunctionOverlay>,
71 },
72 PublicationSnapshot {
73 id: ObjectId,
74 previous: Option<crate::model::replication::PublicationOverlay>,
75 },
76 SubscriptionSnapshot {
77 id: ObjectId,
78 previous: Option<crate::model::replication::SubscriptionOverlay>,
79 },
80 RoleSnapshot {
81 id: ObjectId,
82 previous: Option<crate::model::role::RoleOverlay>,
83 },
84 TriggerSnapshot {
85 id: ObjectId,
86 previous: Option<crate::model::trigger::TriggerOverlay>,
87 },
88 ConstraintSnapshot {
89 table_id: ObjectId,
90 name: String,
91 previous: Option<crate::model::constraint::ConstraintState>,
92 },
93 BaselineForeignKeysSnapshot {
94 previous: HashSet<(ObjectId, String)>,
95 },
96 RoleContextSnapshot {
97 current_role: String,
98 current_role_known: bool,
99 persistent_current_role: String,
100 persistent_current_role_known: bool,
101 session_role: String,
102 session_role_known: bool,
103 persistent_session_role: String,
104 persistent_session_role_known: bool,
105 },
106 ConfidenceSnapshot {
107 previous: crate::analysis::state::Confidence,
108 },
109}
110
111#[derive(Debug, Clone, PartialEq, Eq)]
112pub enum TransactionFrameKind {
113 Root,
114 Savepoint(String),
115}
116
117#[derive(Debug, Clone)]
118pub struct TransactionFrame {
119 pub kind: TransactionFrameKind,
120 pub undo_log: Vec<StateChange>,
121}
122
123impl TransactionFrame {
124 pub fn root() -> Self {
125 Self {
126 kind: TransactionFrameKind::Root,
127 undo_log: Vec::new(),
128 }
129 }
130
131 pub fn savepoint(name: impl Into<String>) -> Self {
132 Self {
133 kind: TransactionFrameKind::Savepoint(name.into()),
134 undo_log: Vec::new(),
135 }
136 }
137
138 pub fn is_named_savepoint(&self, name: &str) -> bool {
139 matches!(&self.kind, TransactionFrameKind::Savepoint(candidate) if candidate == name)
140 }
141}