pgevolve-core 0.4.6

Postgres declarative schema management — core library (parser, IR, diff, planner) powering the pgevolve CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! `Table` — a Postgres table.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::identifier::{Identifier, QualifiedName};
use crate::ir::column::Column;
use crate::ir::constraint::Constraint;
use crate::ir::difference::Difference;
use crate::ir::eq::{Equiv, field_difference, prefix_differences};

/// A Postgres table.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Table {
    /// Schema-qualified table name.
    pub qname: QualifiedName,
    /// Columns in their logical order.
    pub columns: Vec<Column>,
    /// Constraints, paired by `qname` for diffing.
    pub constraints: Vec<Constraint>,
    /// `Some` → this table is a partitioned parent (`PARTITION BY …`).
    pub partition_by: Option<crate::ir::partition::PartitionBy>,
    /// `Some` → this table is itself a partition (`PARTITION OF … FOR VALUES …`).
    pub partition_of: Option<crate::ir::partition::PartitionOf>,
    /// Optional comment.
    pub comment: Option<String>,
    /// Object owner. `None` = unmanaged (the differ ignores ownership).
    /// `Some(role)` = managed: diff emits `ALTER TABLE ... OWNER TO role`.
    pub owner: Option<Identifier>,
    /// Grants on this object. Empty = no grants. Canonicalized.
    pub grants: Vec<crate::ir::grant::Grant>,
    /// `ROW LEVEL SECURITY` enabled flag. PG default: false.
    pub rls_enabled: bool,
    /// `FORCE ROW LEVEL SECURITY` flag (applies even to owner). PG default: false.
    pub rls_forced: bool,
    /// Policies attached to this table. Canonicalized in `ir::canon::policies`.
    pub policies: Vec<crate::ir::policy::Policy>,
    /// Storage parameters (`WITH (fillfactor = …, autovacuum_* = …, …)`).
    /// Default is the empty/no-overrides state.
    pub storage: crate::ir::reloptions::TableStorageOptions,
    /// Table access method (`CREATE TABLE … USING <am>`). `None` = inherit the
    /// cluster default (`heap`). Canon normalizes `Some("heap")` → `None`.
    pub access_method: Option<Identifier>,
    /// Tablespace placement (`TABLESPACE <name>`). `None` = the database default (`pg_default`). Applies to regular tables, partitioned parents (default for future partitions), and partition children (overrides parent default).
    pub tablespace: Option<Identifier>,
}

impl Equiv for Table {
    fn differences(&self, other: &Self) -> Vec<Difference> {
        // Field-completeness guard: the compiler errors if a field is added to
        // `Table` without being handled below. Bindings are unused (values are
        // read via `self`/`other`); the destructure exists only for the check.
        let Self {
            qname: _,
            columns: _,
            constraints: _,
            partition_by: _,
            partition_of: _,
            comment: _,
            owner: _,
            grants: _,
            rls_enabled: _,
            rls_forced: _,
            policies: _,
            storage: _,
            access_method: _,
            tablespace: _,
        } = self;
        let mut out = Vec::new();
        out.extend(field_difference("qname", &self.qname, &other.qname));
        out.extend(field_difference(
            "partition_by",
            &format!("{:?}", self.partition_by),
            &format!("{:?}", other.partition_by),
        ));
        out.extend(field_difference(
            "partition_of",
            &format!("{:?}", self.partition_of),
            &format!("{:?}", other.partition_of),
        ));
        out.extend(field_difference(
            "comment",
            &format!("{:?}", self.comment),
            &format!("{:?}", other.comment),
        ));
        out.extend(field_difference(
            "owner",
            &format!("{:?}", self.owner),
            &format!("{:?}", other.owner),
        ));
        out.extend(field_difference(
            "grants",
            &format!("{:?}", self.grants),
            &format!("{:?}", other.grants),
        ));
        out.extend(field_difference(
            "rls_enabled",
            &format!("{:?}", self.rls_enabled),
            &format!("{:?}", other.rls_enabled),
        ));
        out.extend(field_difference(
            "rls_forced",
            &format!("{:?}", self.rls_forced),
            &format!("{:?}", other.rls_forced),
        ));
        out.extend(field_difference(
            "policies",
            &format!("{:?}", self.policies),
            &format!("{:?}", other.policies),
        ));
        out.extend(field_difference(
            "storage",
            &format!("{:?}", self.storage),
            &format!("{:?}", other.storage),
        ));
        out.extend(field_difference(
            "access_method",
            &format!("{:?}", self.access_method),
            &format!("{:?}", other.access_method),
        ));
        out.extend(field_difference(
            "tablespace",
            &format!("{:?}", self.tablespace),
            &format!("{:?}", other.tablespace),
        ));
        out.extend(diff_columns(&self.columns, &other.columns));
        out.extend(diff_constraints(&self.constraints, &other.constraints));
        out
    }
}

/// Diff two column slices: add/remove/change by name, then order drift.
fn diff_columns(
    lhs_cols: &[crate::ir::column::Column],
    rhs_cols: &[crate::ir::column::Column],
) -> Vec<Difference> {
    let mut out = Vec::new();
    let lhs: BTreeMap<_, _> = lhs_cols.iter().map(|c| (c.name.as_str(), c)).collect();
    let rhs: BTreeMap<_, _> = rhs_cols.iter().map(|c| (c.name.as_str(), c)).collect();
    for (name, l) in &lhs {
        match rhs.get(name) {
            None => out.push(Difference::new(
                format!("columns.{name}"),
                "present",
                "removed",
            )),
            Some(r) => {
                out.extend(prefix_differences(
                    &format!("columns.{name}"),
                    l.differences(r),
                ));
            }
        }
    }
    for name in rhs.keys() {
        if !lhs.contains_key(name) {
            out.push(Difference::new(
                format!("columns.{name}"),
                "missing",
                "added",
            ));
        }
    }
    let lhs_order: Vec<&str> = lhs_cols.iter().map(|c| c.name.as_str()).collect();
    let rhs_order: Vec<&str> = rhs_cols.iter().map(|c| c.name.as_str()).collect();
    if lhs_order != rhs_order {
        out.push(Difference::new(
            "columns.<order>",
            lhs_order.join(","),
            rhs_order.join(","),
        ));
    }
    out
}

/// Diff two constraint slices: add/remove/change by qname.
fn diff_constraints(
    lhs_cs_slice: &[crate::ir::constraint::Constraint],
    rhs_cs_slice: &[crate::ir::constraint::Constraint],
) -> Vec<Difference> {
    let mut out = Vec::new();
    let lhs_cs: BTreeMap<_, _> = lhs_cs_slice.iter().map(|c| (&c.qname, c)).collect();
    let rhs_cs: BTreeMap<_, _> = rhs_cs_slice.iter().map(|c| (&c.qname, c)).collect();
    for (qn, l) in &lhs_cs {
        match rhs_cs.get(qn) {
            None => out.push(Difference::new(
                format!("constraints.{qn}"),
                "present",
                "removed",
            )),
            Some(r) => {
                out.extend(prefix_differences(
                    &format!("constraints.{qn}"),
                    l.differences(r),
                ));
            }
        }
    }
    for qn in rhs_cs.keys() {
        if !lhs_cs.contains_key(qn) {
            out.push(Difference::new(
                format!("constraints.{qn}"),
                "missing",
                "added",
            ));
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identifier::Identifier;
    use crate::ir::column_type::ColumnType;
    use crate::ir::constraint::{ConstraintKind, Deferrable};

    fn id(s: &str) -> Identifier {
        Identifier::from_unquoted(s).unwrap()
    }

    fn qn(name: &str) -> QualifiedName {
        QualifiedName::new(id("app"), id(name))
    }

    fn col(name: &str, ty: ColumnType, nullable: bool) -> Column {
        Column {
            name: id(name),
            ty,
            nullable,
            default: None,
            identity: None,
            generated: None,
            collation: None,
            storage: None,
            compression: None,
            comment: None,
        }
    }

    fn pk(name: &str, cols: &[&str]) -> Constraint {
        Constraint {
            qname: qn(name),
            kind: ConstraintKind::PrimaryKey {
                columns: cols.iter().map(|c| id(c)).collect(),
                include: vec![],
            },
            deferrable: Deferrable::NotDeferrable,
            comment: None,
        }
    }

    fn base() -> Table {
        Table {
            qname: qn("users"),
            columns: vec![
                col("id", ColumnType::BigInt, false),
                col("email", ColumnType::Text, false),
            ],
            constraints: vec![pk("users_pkey", &["id"])],
            partition_by: None,
            partition_of: None,
            comment: None,
            owner: None,
            grants: vec![],
            rls_enabled: false,
            rls_forced: false,
            policies: vec![],
            storage: crate::ir::reloptions::TableStorageOptions::default(),
            access_method: None,
            tablespace: None,
        }
    }

    #[test]
    fn equal_tables_have_no_diff() {
        assert!(base().canonical_eq(&base()));
    }

    #[test]
    fn add_column_diffs() {
        let mut b = base();
        b.columns.push(col("name", ColumnType::Text, true));
        let d = base().differences(&b);
        assert!(d.iter().any(|x| x.path == "columns.name"));
    }

    #[test]
    fn remove_column_diffs() {
        let mut b = base();
        b.columns.pop();
        let d = base().differences(&b);
        assert!(d.iter().any(|x| x.path == "columns.email"));
    }

    #[test]
    fn reorder_columns_diffs_as_order() {
        let mut b = base();
        b.columns.reverse();
        let d = base().differences(&b);
        assert!(d.iter().any(|x| x.path == "columns.<order>"));
    }

    #[test]
    fn add_constraint_diffs() {
        let mut b = base();
        b.constraints.push(pk("users_alt_pkey", &["email"]));
        let d = base().differences(&b);
        assert!(d.iter().any(|x| x.path == "constraints.app.users_alt_pkey"));
    }

    #[test]
    fn changed_column_definition_diffs_under_path() {
        let mut b = base();
        b.columns[1].nullable = true;
        let d = base().differences(&b);
        assert!(d.iter().any(|x| x.path == "columns.email.nullable"));
    }

    #[test]
    fn owner_change_diffs() {
        let mut b = base();
        b.owner = Some(id("new_owner"));
        assert!(base().differences(&b).iter().any(|x| x.path == "owner"));
    }

    #[test]
    fn grants_change_diffs() {
        let mut b = base();
        b.grants.push(crate::ir::grant::Grant {
            grantee: crate::ir::grant::GrantTarget::Public,
            privilege: crate::ir::grant::Privilege::Select,
            with_grant_option: false,
            columns: None,
        });
        assert!(base().differences(&b).iter().any(|x| x.path == "grants"));
    }

    #[test]
    fn rls_enabled_change_diffs() {
        let mut b = base();
        b.rls_enabled = true;
        assert!(
            base()
                .differences(&b)
                .iter()
                .any(|x| x.path == "rls_enabled")
        );
    }

    #[test]
    fn rls_forced_change_diffs() {
        let mut b = base();
        b.rls_forced = true;
        assert!(
            base()
                .differences(&b)
                .iter()
                .any(|x| x.path == "rls_forced")
        );
    }

    #[test]
    fn policies_change_diffs() {
        use crate::ir::grant::GrantTarget;
        use crate::ir::policy::{Policy, PolicyCommand};
        let mut b = base();
        b.policies.push(Policy {
            name: id("p1"),
            permissive: true,
            command: PolicyCommand::All,
            roles: vec![GrantTarget::Public],
            using: None,
            with_check: None,
        });
        assert!(base().differences(&b).iter().any(|x| x.path == "policies"));
    }

    #[test]
    fn storage_change_diffs() {
        let mut b = base();
        b.storage = crate::ir::reloptions::TableStorageOptions {
            fillfactor: Some(80),
            ..Default::default()
        };
        assert!(base().differences(&b).iter().any(|x| x.path == "storage"));
    }

    #[test]
    fn access_method_field_roundtrips() {
        let mut t = base();
        assert!(
            t.access_method.is_none(),
            "default access_method must be None"
        );
        t.access_method = Some(Identifier::from_unquoted("columnar").unwrap());
        assert_eq!(
            t.access_method.as_ref().map(Identifier::as_str),
            Some("columnar"),
        );
        // JSON round-trip preserves the field.
        let json = serde_json::to_string(&t).unwrap();
        let restored: Table = serde_json::from_str(&json).unwrap();
        assert_eq!(
            restored.access_method.as_ref().map(Identifier::as_str),
            Some("columnar"),
        );
    }

    #[test]
    fn tablespace_field_roundtrips() {
        let mut t = base();
        assert!(t.tablespace.is_none(), "default tablespace must be None");
        t.tablespace = Some(Identifier::from_unquoted("fast").unwrap());
        assert_eq!(t.tablespace.as_ref().map(Identifier::as_str), Some("fast"),);
        // JSON round-trip preserves the field.
        let json = serde_json::to_string(&t).unwrap();
        let restored: Table = serde_json::from_str(&json).unwrap();
        assert_eq!(
            restored.tablespace.as_ref().map(Identifier::as_str),
            Some("fast"),
        );
    }
}