pgevolve-core 0.3.4

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Rewrite pass: turn an [`OrderedChangeSet`] into a flat `Vec<RawStep>`.
//!
//! Each [`Change`] / [`crate::diff::TableOp`] / [`crate::diff::SequenceOp`] is dispatched to an emitter
//! that produces one or more `RawStep`s. Most emitters produce a single step;
//! the four documented online rewrites (spec §6.5) produce multiple. Online
//! rewrites are gated on [`PlannerPolicy`] switches so atomic mode is a single
//! transaction with no rewriting.

// The dispatcher is inherently long (one arm per IR variant) and its work is
// almost entirely SQL string assembly. Pedantic clippy lints that fight
// straight-line emitter code are silenced at module scope rather than peppered
// through individual arms.
#![allow(clippy::too_many_lines)]
#![allow(clippy::option_if_let_else)]
#![allow(clippy::format_push_string)]
#![allow(clippy::needless_pass_by_value)]

pub mod check_not_valid_validate;
pub mod concurrent_index;
pub mod emit;
pub mod extensions;
pub mod fk_not_valid_validate;
pub mod functions;
pub mod grants;
pub mod partitions;
pub mod policies;
pub mod publications;
pub mod refresh_mv_concurrently;
pub mod reloptions;
pub mod set_not_null_check_pattern;
pub mod sql;
pub mod triggers;
pub mod types;
pub mod views;

use crate::diff::change::{Change, ChangeEntry};
use crate::diff::destructiveness::Destructiveness;
use crate::identifier::QualifiedName;
use crate::ir::catalog::Catalog;
use crate::plan::ordered::OrderedChangeSet;
use crate::plan::policy::PlannerPolicy;
use crate::plan::raw_step::RawStep;

/// Context passed to every emitter — read-only.
pub(super) struct Ctx<'a> {
    pub(super) target: &'a Catalog,
    pub(super) source: &'a Catalog,
    pub(super) policy: &'a PlannerPolicy,
}

/// Apply policy-gated rewrites and emit the flat step list.
///
/// Steps are emitted in this order: creates → modifies → drops → deferred FKs.
/// Within each bucket the dependency order from [`OrderedChangeSet`] is preserved.
pub fn rewrite(
    ordered: OrderedChangeSet,
    target: &Catalog,
    policy: &PlannerPolicy,
) -> Vec<RawStep> {
    rewrite_with_source(ordered, target, &Catalog::empty(), policy)
}

/// Like [`rewrite`] but also accepts the source catalog for drift-recovery
/// changes that need to look up source-side IR (e.g., `RecreateIndex`).
///
/// After all changes are emitted, the `refresh_mv_concurrently` rewrite pass
/// upgrades `REFRESH MATERIALIZED VIEW` steps to `CONCURRENTLY` when the target
/// MV has a unique index (spec §6.5). Lint findings from that pass are
/// currently discarded here (T10 wires them into the plan's lint output).
pub fn rewrite_with_source(
    ordered: OrderedChangeSet,
    target: &Catalog,
    source: &Catalog,
    policy: &PlannerPolicy,
) -> Vec<RawStep> {
    let ctx = Ctx {
        target,
        source,
        policy,
    };
    let mut out = Vec::new();
    for entry in ordered.creates_and_adds {
        emit_change(entry, &ctx, &mut out);
    }
    for entry in ordered.modifies {
        emit_change(entry, &ctx, &mut out);
    }
    for entry in ordered.drops {
        emit_change(entry, &ctx, &mut out);
    }
    for fk in ordered.deferred_fks {
        emit::deferred_fk::emit(&fk, &ctx, &mut out);
    }
    // Post-emit: upgrade REFRESH MATERIALIZED VIEW → CONCURRENTLY where eligible.
    // Check indexes from the *source* catalog (desired state), not target, so
    // that newly-created unique indexes on new MVs are included in the check.
    // Lint findings from this pass are discarded here; T10 wires them into the
    // plan's lint output.
    let mut findings_sink: Vec<crate::lint::Finding> = Vec::new();
    refresh_mv_concurrently::rewrite(&mut out, source, policy, &mut findings_sink);
    out
}

fn emit_change(entry: ChangeEntry, ctx: &Ctx<'_>, out: &mut Vec<RawStep>) {
    let destructive_reason = destructive_reason(&entry.destructiveness);
    let destructive = entry.destructiveness.requires_approval();

    match entry.change {
        Change::CreateSchema(s) => emit::schema::create(s, destructive, destructive_reason, out),
        Change::DropSchema(name) => emit::schema::drop_(name, destructive, destructive_reason, out),
        Change::AlterSchema { name, comment } => emit::schema::alter(name, comment, out),

        Change::CreateTable(t) => emit::table::create(t, destructive, destructive_reason, out),
        Change::DropTable { qname, .. } => {
            emit::table::drop_(qname, destructive, destructive_reason, out);
        }
        Change::AlterTable { qname, ops } => emit::table::alter(qname, ops, ctx, out),

        Change::CreateIndex(idx) => {
            emit::index::create(idx, ctx, destructive, destructive_reason, out);
        }
        Change::DropIndex(qname) => {
            emit::index::drop_(qname, ctx, destructive, destructive_reason, out);
        }
        Change::ReplaceIndex { from, to } => {
            emit::index::replace(from, to, ctx, destructive, destructive_reason, out);
        }

        Change::CreateSequence(s) => {
            emit::sequence::create(s, destructive, destructive_reason, out);
        }
        Change::DropSequence(qname) => {
            emit::sequence::drop_(qname, destructive, destructive_reason, out);
        }
        Change::AlterSequence { qname, ops } => emit::sequence::alter(qname, ops, out),

        // Drift-recovery changes emitted from the DriftReport.
        Change::ValidateConstraint { table, constraint } => {
            emit::constraint::validate(table, constraint, destructive, destructive_reason, out);
        }
        Change::RecreateIndex { qname } => {
            emit::index::recreate(qname, ctx, destructive, destructive_reason, out);
        }

        Change::View(vc) => emit::view::emit(vc, destructive, destructive_reason, out),
        Change::Mv(mc) => emit::mv::emit(mc, destructive, destructive_reason, out),
        Change::UserType(utc) => {
            emit::user_type::emit(utc, destructive, destructive_reason, ctx, out);
        }
        Change::Function(fc) => emit::function::emit(fc, destructive, destructive_reason, out),
        Change::Procedure(pc) => emit::procedure::emit(pc, destructive, destructive_reason, out),
        Change::Extension(ec) => emit::extension::emit(ec, destructive, destructive_reason, out),
        Change::Trigger(tc) => emit::trigger::emit(tc, destructive, destructive_reason, out),
        Change::Table(tc) => {
            use crate::diff::change::TableChange;
            match tc {
                TableChange::AttachPartition {
                    parent,
                    child,
                    bounds,
                } => {
                    out.push(RawStep {
                        step_no: 0,
                        kind: crate::plan::raw_step::StepKind::AttachPartition,
                        destructive: false,
                        destructive_reason: None,
                        intent_id: None,
                        targets: vec![child.clone()],
                        sql: crate::plan::rewrite::partitions::attach_partition(
                            &parent, &child, &bounds,
                        ),
                        transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
                    });
                }
                TableChange::DetachPartition { parent, child } => {
                    out.push(RawStep {
                        step_no: 0,
                        kind: crate::plan::raw_step::StepKind::DetachPartition,
                        destructive: false,
                        destructive_reason: None,
                        intent_id: None,
                        targets: vec![child.clone()],
                        sql: crate::plan::rewrite::partitions::detach_partition(&parent, &child),
                        transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
                    });
                }
            }
        }
        Change::AlterObjectOwner(op) => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterObjectOwner,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![op.qname.clone()],
                sql: grants::alter_object_owner(op.kind, &op.qname, &op.signature, &op.to),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::GrantObjectPrivilege {
            kind,
            qname,
            signature,
            grant,
        } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::GrantObjectPrivilege,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: grants::grant_object_privilege(kind, &qname, &signature, &grant),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::RevokeObjectPrivilege {
            kind,
            qname,
            signature,
            grant,
        } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::RevokeObjectPrivilege,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: grants::revoke_object_privilege(kind, &qname, &signature, &grant),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::GrantColumnPrivilege { qname, grant } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::GrantColumnPrivilege,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: grants::grant_column_privilege(&qname, &grant),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::RevokeColumnPrivilege { qname, grant } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::RevokeColumnPrivilege,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: grants::revoke_column_privilege(&qname, &grant),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterDefaultPrivileges {
            target_role,
            schema,
            object_type,
            is_grant,
            grant,
        } => {
            // Default-priv rules are not scoped to a per-object QualifiedName;
            // targets is left empty (same convention as cluster ops in
            // plan/cluster_rewrite/emit.rs).
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterDefaultPrivileges,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: grants::alter_default_privileges(
                    &target_role,
                    schema.as_ref(),
                    object_type,
                    is_grant,
                    &grant,
                ),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::CreatePolicy { table, policy } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::CreatePolicy,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![table.clone()],
                sql: policies::create_policy(&table, &policy),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::DropPolicy { table, name } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::DropPolicy,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![table.clone()],
                sql: policies::drop_policy(&table, &name),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPolicy { table, policy } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPolicy,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![table.clone()],
                sql: policies::alter_policy(&table, &policy),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::SetTableRowSecurity { qname, enable } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::SetTableRowSecurity,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: policies::set_table_row_security(&qname, enable),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::SetTableForceRowSecurity { qname, force } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::SetTableForceRowSecurity,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: policies::set_table_force_row_security(&qname, force),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }

        Change::SetTableStorage { qname, options } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::SetTableStorage,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: reloptions::alter_table_set_storage(&qname, &options),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::SetIndexStorage { qname, options } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::SetIndexStorage,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: reloptions::alter_index_set_storage(&qname, &options),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::SetMaterializedViewStorage { qname, options } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::SetMaterializedViewStorage,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: reloptions::alter_mv_set_storage(&qname, &options),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }

        Change::CreatePublication(p) => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::CreatePublication,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::create_publication(&p),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
            // Follow-up COMMENT step if a comment is present.
            if let Some(c) = &p.comment {
                out.push(RawStep {
                    step_no: 0,
                    kind: crate::plan::raw_step::StepKind::CommentOnPublication,
                    destructive: false,
                    destructive_reason: None,
                    intent_id: None,
                    targets: vec![],
                    sql: publications::comment_on_publication(&p.name, Some(c)),
                    transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
                });
            }
        }
        Change::DropPublication { name } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::DropPublication,
                destructive,
                destructive_reason,
                intent_id: None,
                targets: vec![],
                sql: publications::drop_publication(&name),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::ReplacePublication { from, to } => {
            let [drop_sql, create_sql] = publications::replace_publication(&from, &to);
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::ReplacePublication,
                destructive,
                destructive_reason,
                intent_id: None,
                targets: vec![],
                sql: format!("{drop_sql}\n{create_sql}"),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationAddTable { publication, table } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationAddTable,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![table.qname.clone()],
                sql: publications::alter_publication_add_table(&publication, &table),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationDropTable { publication, qname } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationDropTable,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![qname.clone()],
                sql: publications::alter_publication_drop_table(&publication, &qname),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationSetTable { publication, table } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationSetTable,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![table.qname.clone()],
                sql: publications::alter_publication_set_table(&publication, &table),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationAddSchema {
            publication,
            schema,
        } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationAddSchema,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::alter_publication_add_schema(&publication, &schema),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationDropSchema {
            publication,
            schema,
        } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationDropSchema,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::alter_publication_drop_schema(&publication, &schema),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationSetPublish { publication, kinds } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationSetPublish,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::alter_publication_set_publish(&publication, kinds),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::AlterPublicationSetViaRoot { publication, value } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::AlterPublicationSetViaRoot,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::alter_publication_set_via_root(&publication, value),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }
        Change::CommentOnPublication { name, comment } => {
            out.push(RawStep {
                step_no: 0,
                kind: crate::plan::raw_step::StepKind::CommentOnPublication,
                destructive: false,
                destructive_reason: None,
                intent_id: None,
                targets: vec![],
                sql: publications::comment_on_publication(&name, comment.as_deref()),
                transactional: crate::plan::raw_step::TransactionConstraint::InTransaction,
            });
        }

        // UnsupportedDiff is intercepted by the ordering phase and never reaches here.
        Change::UnsupportedDiff { .. } => {
            unreachable!("UnsupportedDiff must never reach the rewrite phase")
        }
    }
}

// `Schema` is identified by an `Identifier`, but `RawStep::targets` carries
// `QualifiedName`s. Promote the schema name to a `QualifiedName` whose schema
// half equals its name — same convention used for ordering in the planner's
// Phase 5 helpers.
pub(super) fn schema_target(name: &crate::identifier::Identifier) -> QualifiedName {
    QualifiedName::new(name.clone(), name.clone())
}

pub(super) fn destructive_reason(d: &Destructiveness) -> Option<String> {
    match d {
        Destructiveness::Safe => None,
        Destructiveness::RequiresApproval { reason }
        | Destructiveness::RequiresApprovalAndDataLossWarning { reason } => Some(reason.clone()),
    }
}

#[cfg(test)]
mod tests;