djogi 0.1.0-alpha.2

Model-first web framework for Rust — web-framework-agnostic core; Axum integration opt-in via the `axum` feature flag
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
//! Replacement-column expand/contract pattern.
//!
//! Covers the "Change column type — requires rewrite" row of the v3
//! plan §7 classification table. The descriptor change that triggers
//! this pattern is an [`AlterColumn`](SchemaOperation::AlterColumn)
//! carrying [`ColumnChange::ChangeType`] whose `from`/`to` rendered
//! types differ in storage shape (varchar widening within `Pg18`
//! catalog-only paths classifies as `OnlineSafe` and never reaches
//! this pattern).
//!
//! # Step graph
//!
//! 1. [`StepKind::ExpandSchema`] — `ALTER TABLE <t> ADD COLUMN
//!    <c>_new <to> NULL`. Adds the shadow column the rewrite drains
//!    into.
//! 2. [`StepKind::BeginCompatibilityWindow`] — register the dual-
//!    read / dual-write hooks that keep `<c>` and `<c>_new` aligned
//!    across in-flight transactions.
//! 3. [`StepKind::BackfillChunked`] — copy `<c>` into `<c>_new`. The
//!    predicate template emits a complete `UPDATE`-tail fragment of
//!    the shape `SET <c>_new = <c>::<to-type> WHERE id IN (SELECT id
//!    FROM <t> WHERE <c>_new IS NULL LIMIT $1)` — bounded to one
//!    chunk via the `LIMIT $1` placeholder, idempotent because the
//!    inner predicate self-cancels (once a row's shadow column is
//!    populated, the chunk skips it).
//! 4. [`StepKind::ValidateBackfill`] — operator gate; runner pauses
//!    until `SELECT count(*) FROM <t> WHERE <c>_new IS NULL` returns
//!    zero.
//! 5. [`StepKind::CutoverReads`] — visage projection switches reads
//!    to `<c>_new`.
//! 6. [`StepKind::CutoverWrites`] — writes target `<c>_new` only.
//!    Dual-write hook turns off.
//! 7. [`StepKind::FinalizeConstraints`] — apply NOT NULL / CHECK on
//!    `<c>_new` if the original column carried them.
//! 8. [`StepKind::CleanupLegacyState`] — `DROP COLUMN <c>` then
//!    `RENAME COLUMN <c>_new TO <c>`.
//!
//! # Idempotency
//!
//! The chunk's WHERE predicate `<c>_new IS NULL` self-cancels — once
//! a row's shadow column has a non-null value, the row falls out of
//! the predicate forever and re-runs are a no-op. The `SET <c>_new =
//! <c>::<to-type>` cast is a stable-volatility expression so a
//! crashed-and-resumed chunk produces the same shadow value as the
//! prior attempt would have.

use super::{Pattern, PatternContext, PatternError};
use crate::live_migrate::plan::{Step, StepKind, StepParameters};
use crate::migrate::SchemaOperation;
use crate::migrate::diff::ColumnChange;

/// Marker type implementing [`Pattern`] for the shadow-column type
/// change rollout.
pub struct ReplacementColumn;

impl Pattern for ReplacementColumn {
    const ID: &'static str = "replacement_column";
    const IDEMPOTENT_PREDICATE: bool = true;

    fn emit(op: &SchemaOperation, ctx: &PatternContext) -> Result<Vec<Step>, PatternError> {
        // djogi#220 — belt-and-braces refusal when the adopter supplied
        // a `#[field(type_change_using = "<expr>")]` clause. The
        // classifier
        // ([`crate::live_migrate::classify::classify_column_change`])
        // routes `using.is_some()` to `OfflineOnly` so this pattern
        // should never be dispatched in that case; the explicit refusal
        // below is a defense-in-depth guard. The shadow-column backfill
        // can only emit a plain SQL cast (`SET <shadow> = <col>::<to>`)
        // and cannot replicate the adopter's USING body — emitting the
        // default cast anyway would silently corrupt or fail-per-row on
        // exactly the rows the adopter wrote the expression to handle.
        if let SchemaOperation::AlterColumn {
            change: ColumnChange::ChangeType { using: Some(_), .. },
            ..
        } = op
        {
            return Err(PatternError::CannotEmit {
                pattern: Self::ID,
                reason: "ColumnChange::ChangeType carries adopter-supplied `using` \
                         (#[field(type_change_using = \"...\")]); the shadow-column \
                         backfill cannot replicate a custom USING expression. The \
                         classifier routes this case to OfflineOnly — apply the \
                         migration via the offline path"
                    .to_string(),
            });
        }
        let (table, column, to_type) = match op {
            SchemaOperation::AlterColumn {
                table,
                column,
                change: ColumnChange::ChangeType { to, .. },
            } => (table, column, to),
            _ => {
                return Err(PatternError::WrongOperation {
                    pattern: Self::ID,
                    reason: "expected AlterColumn { change: ChangeType }".to_string(),
                });
            }
        };

        let shadow = format!("{column}_new");
        let expand_sql = format!(
            "ALTER TABLE {tbl} ADD COLUMN {shadow_q} {ty} NULL",
            tbl = quote_ident(table),
            shadow_q = quote_ident(&shadow),
            ty = to_type,
        );
        // Backfill template: a complete UPDATE-tail fragment that the
        // runner concatenates onto `UPDATE <table> `. The shape is
        //
        //     SET <shadow> = <legacy>::<to-type>
        //     WHERE id IN (SELECT id FROM <table>
        //                  WHERE <shadow> IS NULL
        //                  LIMIT $1)
        //
        // - `SET <shadow> = <legacy>::<to-type>` is the conversion. A
        //   plain SQL cast suffices for the shapes the classifier
        //   routes here (binary-coercible widening was filtered out by
        //   the classifier and never reaches this pattern; rewrites
        //   that need a non-cast transform must classify as
        //   OfflineOnly per the §7 amendment).
        // - The WHERE predicate `<shadow> IS NULL` is structurally
        //   idempotent: once a row converges, it falls out forever.
        // - `LIMIT $1` bounds the row count to one chunk; `$1` is the
        //   only placeholder the runner binds.
        let backfill_predicate = format!(
            "SET {shadow_q} = {col_q}::{ty} WHERE id IN (SELECT id FROM {tbl} WHERE {shadow_q} IS NULL LIMIT $1)",
            shadow_q = quote_ident(&shadow),
            col_q = quote_ident(column),
            ty = to_type,
            tbl = quote_ident(table),
        );
        let gate_query = format!(
            "SELECT count(*) FROM {tbl} WHERE {shadow_q} IS NULL",
            tbl = quote_ident(table),
            shadow_q = quote_ident(&shadow),
        );
        let drop_legacy_sql = format!(
            "ALTER TABLE {tbl} DROP COLUMN {col_q}",
            tbl = quote_ident(table),
            col_q = quote_ident(column),
        );
        let rename_sql = format!(
            "ALTER TABLE {tbl} RENAME COLUMN {shadow_q} TO {col_q}",
            tbl = quote_ident(table),
            shadow_q = quote_ident(&shadow),
            col_q = quote_ident(column),
        );

        Ok(vec![
            Step {
                kind: StepKind::ExpandSchema,
                ordinal: 0,
                parameters: StepParameters::ExpandSchema {
                    sql_segments: vec![expand_sql],
                },
            },
            Step {
                kind: StepKind::BeginCompatibilityWindow,
                ordinal: 1,
                parameters: StepParameters::BeginCompatibilityWindow {
                    hooks: vec![
                        format!("dual_read::{table}::{column}"),
                        format!("dual_write::{table}::{column}"),
                    ],
                },
            },
            Step {
                kind: StepKind::BackfillChunked,
                ordinal: 2,
                parameters: StepParameters::BackfillChunked {
                    table: table.clone(),
                    predicate_template: backfill_predicate,
                    chunk_size: ctx.backfill_chunk_size,
                },
            },
            Step {
                kind: StepKind::ValidateBackfill,
                ordinal: 3,
                parameters: StepParameters::ValidateBackfill { gate_query },
            },
            Step {
                kind: StepKind::CutoverReads,
                ordinal: 4,
                parameters: StepParameters::CutoverReads {
                    description: format!("flip read path for {table}.{column} onto {shadow}"),
                },
            },
            Step {
                kind: StepKind::CutoverWrites,
                ordinal: 5,
                parameters: StepParameters::CutoverWrites {
                    description: format!(
                        "flip write path for {table}.{column} onto {shadow}; drop dual-write",
                    ),
                },
            },
            Step {
                kind: StepKind::FinalizeConstraints,
                ordinal: 6,
                parameters: StepParameters::FinalizeConstraints {
                    sql_segments: Vec::new(),
                },
            },
            Step {
                kind: StepKind::CleanupLegacyState,
                ordinal: 7,
                parameters: StepParameters::CleanupLegacyState {
                    sql_segments: vec![drop_legacy_sql, rename_sql],
                },
            },
        ])
    }
}

fn quote_ident(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 2);
    out.push('"');
    out.push_str(name);
    out.push('"');
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ctx() -> PatternContext {
        PatternContext::with_defaults()
    }

    fn op() -> SchemaOperation {
        SchemaOperation::AlterColumn {
            table: "ledger_entry".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "INTEGER".to_string(),
                to: "BIGINT".to_string(),
                using: None,
            },
        }
    }

    #[test]
    fn emits_eight_step_expand_contract_sequence() {
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        assert_eq!(steps.len(), 8);
        let kinds: Vec<_> = steps.iter().map(|s| s.kind).collect();
        assert_eq!(
            kinds,
            vec![
                StepKind::ExpandSchema,
                StepKind::BeginCompatibilityWindow,
                StepKind::BackfillChunked,
                StepKind::ValidateBackfill,
                StepKind::CutoverReads,
                StepKind::CutoverWrites,
                StepKind::FinalizeConstraints,
                StepKind::CleanupLegacyState,
            ],
        );
    }

    #[test]
    fn emitted_ordinals_are_sequential_and_kinds_are_consistent() {
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        for (idx, step) in steps.iter().enumerate() {
            assert_eq!(step.ordinal as usize, idx);
            assert_eq!(step.kind, step.parameters.kind());
        }
    }

    #[test]
    fn expand_step_adds_shadow_column_with_target_type() {
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        let StepParameters::ExpandSchema { sql_segments } = &steps[0].parameters else {
            panic!("expected ExpandSchema");
        };
        assert_eq!(sql_segments.len(), 1);
        assert!(sql_segments[0].contains("ADD COLUMN"));
        assert!(sql_segments[0].contains("\"amount_new\""));
        assert!(sql_segments[0].contains("BIGINT"));
        assert!(sql_segments[0].contains("NULL"));
    }

    #[test]
    fn backfill_template_emits_complete_update_tail_with_set_and_limit() {
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        let StepParameters::BackfillChunked {
            predicate_template, ..
        } = &steps[2].parameters
        else {
            panic!("expected BackfillChunked");
        };
        // SET clause names the shadow column and the conversion.
        assert!(
            predicate_template.contains("SET"),
            "template must include SET clause: {predicate_template}",
        );
        assert!(
            predicate_template.contains("\"amount_new\""),
            "template must reference shadow column: {predicate_template}",
        );
        // Idempotent WHERE inside a subquery, bounded by LIMIT $1.
        assert!(
            predicate_template.contains("IS NULL"),
            "WHERE predicate must self-cancel via IS NULL: {predicate_template}",
        );
        assert!(
            predicate_template.contains("LIMIT $1"),
            "template must bound row count via LIMIT $1: {predicate_template}",
        );
        // Subquery that bounds the rewrite to one chunk.
        assert!(
            predicate_template.contains("WHERE id IN (SELECT id FROM"),
            "template must use canonical id-IN-subquery shape: {predicate_template}",
        );
        // No stray placeholder beyond `$1`.
        assert!(
            !predicate_template.contains("$2"),
            "template must not bind any placeholder beyond $1: {predicate_template}",
        );
    }

    #[test]
    fn backfill_template_concatenates_into_valid_update_statement() {
        // End-to-end check: the runner builds the chunk SQL by
        // concatenating "UPDATE <table> " with the predicate template.
        // The result must be a valid Postgres UPDATE statement —
        // specifically, it must contain `UPDATE`, `SET`, `WHERE`, and
        // a `LIMIT $1` somewhere after the SET.
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        let StepParameters::BackfillChunked {
            table,
            predicate_template,
            ..
        } = &steps[2].parameters
        else {
            panic!("expected BackfillChunked");
        };
        let stmt = format!("UPDATE {table} {predicate_template}");
        assert!(stmt.contains("UPDATE"));
        assert!(stmt.contains("SET"));
        assert!(stmt.contains("WHERE"));
        assert!(stmt.contains("LIMIT $1"));
        // SET must come before WHERE — otherwise the statement is
        // syntactically broken.
        let set_pos = stmt.find("SET").unwrap();
        let where_pos = stmt.find("WHERE").unwrap();
        assert!(
            set_pos < where_pos,
            "SET must precede WHERE in the synthesised UPDATE: {stmt}",
        );
    }

    #[test]
    fn cleanup_drops_legacy_then_renames_shadow() {
        let steps = ReplacementColumn::emit(&op(), &ctx()).unwrap();
        let StepParameters::CleanupLegacyState { sql_segments } = &steps[7].parameters else {
            panic!("expected CleanupLegacyState");
        };
        assert_eq!(sql_segments.len(), 2);
        assert!(sql_segments[0].contains("DROP COLUMN \"amount\""));
        assert!(sql_segments[1].contains("RENAME COLUMN \"amount_new\" TO \"amount\""));
    }

    #[test]
    fn rejects_non_change_type_alter_column() {
        let op = SchemaOperation::AlterColumn {
            table: "ledger_entry".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::SetNullable(false),
        };
        let err = ReplacementColumn::emit(&op, &ctx()).unwrap_err();
        assert!(matches!(err, PatternError::WrongOperation { .. }));
    }

    #[test]
    fn rejects_drop_column() {
        let op = SchemaOperation::DropColumn {
            table: "ledger_entry".to_string(),
            column: "amount".to_string(),
        };
        let err = ReplacementColumn::emit(&op, &ctx()).unwrap_err();
        assert!(matches!(err, PatternError::WrongOperation { .. }));
    }

    #[test]
    fn rejects_change_type_with_adopter_using() {
        // djogi#220 — adopter-supplied `using` forces the offline path.
        // The classifier
        // ([`crate::live_migrate::classify::classify_column_change`])
        // routes this case to `OfflineOnly` so the dispatcher never
        // reaches the pattern. The explicit refusal here is the
        // defense-in-depth guard against future composers that bypass
        // the classifier — emitting the default cast in the backfill
        // would silently corrupt or fail-per-row on exactly the rows
        // the adopter wrote the expression to handle.
        let op = SchemaOperation::AlterColumn {
            table: "items".to_string(),
            column: "kind".to_string(),
            change: ColumnChange::ChangeType {
                from: "TEXT".to_string(),
                to: "UUID".to_string(),
                using: Some("(\"kind\"::text)::uuid".to_string()),
            },
        };
        let err = ReplacementColumn::emit(&op, &ctx()).unwrap_err();
        match err {
            PatternError::CannotEmit { reason, .. } => {
                assert!(
                    reason.contains("type_change_using") || reason.contains("USING"),
                    "refusal reason must name the corrective attribute: {reason}",
                );
                assert!(
                    reason.contains("offline") || reason.contains("OfflineOnly"),
                    "refusal reason must point at the offline path: {reason}",
                );
            }
            other => panic!("expected CannotEmit, got: {other:?}"),
        }
    }
}