Skip to main content

safe_migrate/analysis/
mutations.rs

1// FILE: src/analysis/mutations.rs
2use crate::analysis::expr_ir::ExprIr;
3use crate::analysis::facts::{SearchPathTarget, TableConstraintFact};
4use crate::ast::identifiers::ObjectId;
5use crate::model::types::TypeKind;
6
7#[derive(Clone, Debug, PartialEq)]
8pub enum PersistenceMutation {
9    Permanent,
10    Temporary,
11    Unlogged,
12}
13
14#[derive(Clone, Debug, PartialEq)]
15pub enum Mutation {
16    CreateSchema(CreateSchemaMutation),
17    DropSchema(DropSchemaMutation),
18    CreateTable(CreateTable),
19    CreateView(CreateView),
20    CreateMaterializedView(CreateMaterializedView),
21    RefreshMaterializedView(RefreshMaterializedViewMutation),
22    CreateIndex(CreateIndex),
23    CreatePolicy(CreatePolicyMutation),
24    DropPolicy(DropPolicyMutation),
25    CreateTrigger(CreateTriggerMutation),
26    DropTrigger(DropTriggerMutation),
27    AlterTable(AlterTable),
28    CreateType(CreateTypeMutation),
29    AlterType(AlterTypeMutation),
30    CreateDomain(CreateDomainMutation),
31    AlterDomain(AlterDomainMutation),
32    DropDomain(DropDomainMutation),
33    CreateSequence(CreateSequenceMutation),
34    AlterSequence(AlterSequenceMutation),
35    DropSequence(DropSequenceMutation),
36    Rename(Rename),
37    DropTable(DropTable),
38    DropView(DropViewMutation),
39    DropMaterializedView(DropMaterializedViewMutation),
40    DropIndex(DropIndex),
41    SearchPath(SearchPathChange),
42    BeginTransaction,
43    CommitTransaction,
44    RollbackTransaction,
45    RollbackToSavepoint(RollbackToSavepointMutation),
46    Savepoint(SavepointMutation),
47    ReleaseSavepoint(ReleaseSavepointMutation),
48    Opaque(OpaqueMutation),
49    Vacuum { is_full: bool },
50}
51
52#[derive(Clone, Debug, PartialEq)]
53pub struct CreateSchemaMutation {
54    pub name: String,
55    pub if_not_exists: bool,
56}
57
58#[derive(Clone, Debug, PartialEq)]
59pub struct DropSchemaMutation {
60    pub names: Vec<String>,
61    pub if_exists: bool,
62    pub cascade: bool,
63}
64
65#[derive(Clone, Debug, PartialEq)]
66pub struct CreatePolicyMutation {
67    pub name: String,
68    pub table: ObjectId,
69}
70
71#[derive(Clone, Debug, PartialEq)]
72pub struct DropPolicyMutation {
73    pub name: String,
74    pub table: ObjectId,
75    pub if_exists: bool,
76}
77
78#[derive(Clone, Debug, PartialEq)]
79pub struct CreateTriggerMutation {
80    pub name: String,
81    pub table: ObjectId,
82}
83
84#[derive(Clone, Debug, PartialEq)]
85pub struct DropTriggerMutation {
86    pub name: String,
87    pub table: ObjectId,
88    pub if_exists: bool,
89}
90
91#[derive(Clone, Debug, PartialEq)]
92pub struct DropViewMutation {
93    pub ids: Vec<ObjectId>,
94    pub if_exists: bool,
95}
96
97#[derive(Clone, Debug, PartialEq)]
98pub struct DropMaterializedViewMutation {
99    pub ids: Vec<ObjectId>,
100    pub if_exists: bool,
101}
102
103#[derive(Clone, Debug, PartialEq)]
104pub struct CreateMaterializedView {
105    pub id: ObjectId,
106    pub depends_on: Vec<ObjectId>,
107}
108
109#[derive(Clone, Debug, PartialEq)]
110pub struct RefreshMaterializedViewMutation {
111    pub id: ObjectId,
112    pub concurrently: bool,
113}
114
115#[derive(Clone, Debug, PartialEq)]
116pub struct CreateSequenceMutation {
117    pub id: ObjectId,
118    pub if_not_exists: bool,
119    pub owned_by: Option<(ObjectId, String)>,
120}
121
122#[derive(Clone, Debug, PartialEq)]
123pub struct AlterSequenceMutation {
124    pub id: ObjectId,
125    pub owned_by: Option<(ObjectId, String)>,
126}
127
128#[derive(Clone, Debug, PartialEq)]
129pub struct DropSequenceMutation {
130    pub ids: Vec<ObjectId>,
131    pub if_exists: bool,
132}
133
134#[derive(Clone, Debug, PartialEq)]
135pub struct CreateDomainMutation {
136    pub id: ObjectId,
137    pub base_type: String,
138}
139
140#[derive(Clone, Debug, PartialEq)]
141pub struct AlterDomainMutation {
142    pub id: ObjectId,
143}
144
145#[derive(Clone, Debug, PartialEq)]
146pub struct DropDomainMutation {
147    pub ids: Vec<ObjectId>,
148    pub if_exists: bool,
149}
150
151#[derive(Clone, Debug, PartialEq)]
152pub struct CreateTypeMutation {
153    pub id: ObjectId,
154    pub kind: TypeKind,
155}
156
157#[derive(Clone, Debug, PartialEq)]
158pub struct AlterTypeMutation {
159    pub id: ObjectId,
160    pub action: AlterTypeActionMutation,
161}
162
163#[derive(Clone, Debug, PartialEq)]
164pub enum AlterTypeActionMutation {
165    AddValue { new_value: String },
166}
167
168#[derive(Clone, Debug, PartialEq)]
169pub struct CreateTable {
170    pub id: ObjectId,
171    pub if_not_exists: bool,
172    pub as_select: bool,
173    pub persistence: PersistenceMutation,
174    pub columns: Vec<ColumnMutation>,
175    pub foreign_keys: Vec<FkMutation>,
176    pub table_constraints: Vec<TableConstraintFact>,
177    pub partition_by: Option<String>,
178    pub partition_of: Option<ObjectId>,
179}
180
181#[derive(Clone, Debug, PartialEq)]
182pub struct ColumnMutation {
183    pub name: String,
184    pub ty: Option<String>,
185    pub not_null: bool,
186    pub is_primary_key: bool,
187    pub default: Option<ExprIr>,
188}
189
190#[derive(Clone, Debug, PartialEq)]
191pub struct FkMutation {
192    pub constraint_name: Option<String>,
193    pub to_table: ObjectId,
194    pub from_columns: Vec<String>,
195    pub to_columns: Vec<String>,
196}
197
198#[derive(Clone, Debug, PartialEq)]
199pub struct CreateView {
200    pub id: ObjectId,
201    pub or_replace: bool,
202    pub depends_on: Vec<ObjectId>,
203}
204
205#[derive(Clone, Debug, PartialEq)]
206pub struct CreateIndex {
207    pub id: ObjectId,
208    pub table: ObjectId,
209    pub if_not_exists: bool,
210    pub concurrently: bool,
211    pub using_method: Option<String>,
212    pub has_predicate: bool,
213}
214
215#[derive(Clone, Debug, PartialEq)]
216pub struct AlterTable {
217    pub id: ObjectId,
218    pub action: AlterTableActionMutation,
219}
220
221#[derive(Clone, Debug, PartialEq)]
222pub struct Rename {
223    pub old_id: ObjectId,
224    pub new_id: ObjectId,
225}
226
227#[derive(Clone, Debug, PartialEq)]
228pub struct DropTable {
229    pub id: ObjectId,
230    pub if_exists: bool,
231    pub cascade: bool,
232}
233
234#[derive(Clone, Debug, PartialEq)]
235pub struct DropIndex {
236    pub id: ObjectId,
237    pub if_exists: bool,
238    pub concurrently: bool,
239}
240
241#[derive(Clone, Debug, PartialEq)]
242pub struct SearchPathChange {
243    pub target: SearchPathTarget,
244}
245
246#[derive(Clone, Debug, PartialEq)]
247pub struct SavepointMutation {
248    pub name: String,
249}
250
251#[derive(Clone, Debug, PartialEq)]
252pub struct ReleaseSavepointMutation {
253    pub name: String,
254}
255
256#[derive(Clone, Debug, PartialEq)]
257pub struct RollbackToSavepointMutation {
258    pub name: String,
259}
260
261#[derive(Clone, Debug, PartialEq)]
262pub enum OpaqueMutation {
263    DoBlock,
264    Execute,
265    DynamicSql,
266    PrepareTransaction,
267    SetTransaction,
268    SetConstraints,
269}
270
271#[derive(Clone, Debug, PartialEq)]
272pub enum AlterTableActionMutation {
273    AddColumn {
274        name: String,
275        ty: Option<String>,
276        if_not_exists: bool,
277        not_null: bool,
278        default: Option<ExprIr>,
279    },
280    DropColumn {
281        name: String,
282        if_exists: bool,
283    },
284    RenameColumn {
285        from: String,
286        to: String,
287    },
288    AddForeignKey {
289        constraint_name: Option<String>,
290        to_table: ObjectId,
291        from_columns: Vec<String>,
292        to_columns: Vec<String>,
293        not_valid: bool,
294    },
295    AlterConstraint {
296        name: String,
297        deferrable: bool,
298    },
299    RenameConstraint {
300        old_name: String,
301        new_name: String,
302    },
303    DropConstraint {
304        name: String,
305    },
306    AddCheckConstraint {
307        constraint_name: Option<String>,
308        not_valid: bool,
309    },
310    AddUniqueConstraint,
311    AddPrimaryKeyConstraint,
312    AddExcludeConstraint,
313    SetNotNull {
314        column: String,
315    },
316    DropNotNull {
317        column: String,
318    },
319    SetType {
320        column: String,
321        ty: String,
322        has_using: bool,
323    },
324    SetDefault {
325        column: String,
326        default: Option<ExprIr>,
327    },
328    ValidateConstraint {
329        constraint_name: String,
330    },
331    AttachPartition {
332        child: ObjectId,
333    },
334    DetachPartition {
335        child: ObjectId,
336    },
337    SetStorage {
338        column: String,
339    },
340    SetAccessMethod,
341}