safe-migrate 0.8.0

Check PostgreSQL migrations against a synchronized database baseline
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
mod common;

use common::{object_id, setup_engine};
use safe_migrate::_internal::analysis::facts::{PublicationObjectFact, PublicationScope};
use safe_migrate::_internal::analysis::graph::DependencyKind;
use safe_migrate::_internal::db::cache::{DbCache, IndexCache};
use safe_migrate::_internal::model::relation::{Persistence, RelationKind, RelationState};
use safe_migrate::_internal::model::role::RoleState;
use safe_migrate::_internal::model::schema::{SchemaOverlay, SchemaState};
use safe_migrate::_internal::model::sequence::{SequenceKind, SequenceOverlay, SequenceState};
use safe_migrate::_internal::model::types::{TypeKind, TypeState};
use safe_migrate::api::AnalysisState;

fn cache_with_public_schema() -> DbCache {
    let mut cache = DbCache::new();
    cache.metadata.source_role = Some("owner".into());
    cache.metadata.source_session_role = Some("owner".into());
    let owner = object_id("", "owner");
    cache.roles.insert(
        owner.clone(),
        RoleState {
            id: owner.clone(),
            can_login: true,
            is_superuser: true,
            inherits: true,
            member_of: Vec::new(),
            can_administer_membership: Vec::new(),
            can_inherit_from: Vec::new(),
            can_set_role_to: Vec::new(),
        },
    );
    cache.schemas.insert(
        "public".into(),
        SchemaState {
            name: "public".into(),
            owner,
            generation: 0,
        },
    );
    cache
}

#[test]
fn cache_v5_hydrates_schema_sequence_and_ownership_edge() {
    let mut cache = cache_with_public_schema();
    let sequence_id = object_id("public", "t_id_seq");
    let table_id = object_id("public", "t");
    cache.insert_baseline(
        table_id.clone(),
        RelationState::new(
            table_id.clone(),
            object_id("", "owner"),
            0,
            Some(0),
            RelationKind::Table,
            Persistence::Permanent,
            0,
        ),
    );
    cache.sequences.insert(
        sequence_id.clone(),
        SequenceState {
            id: sequence_id.clone(),
            owner: object_id("", "owner"),
            owned_by: Some((table_id.clone(), "id".into())),
            kind: SequenceKind::SerialLike,
            generation: 0,
        },
    );

    let state = AnalysisState::new(cache);
    assert!(matches!(
        state.local.schemas.get("public"),
        Some(SchemaOverlay::Present(_))
    ));
    assert!(matches!(
        state.local.sequences.get(&sequence_id),
        Some(SequenceOverlay::Present(sequence)) if sequence.kind == SequenceKind::SerialLike
    ));
    assert!(state.local.graph.edges().iter().any(|edge| {
        edge.dependent == sequence_id
            && edge.referenced == table_id
            && matches!(
                &edge.kind,
                DependencyKind::SequenceOwnedBy { column } if column == "id"
            )
    }));
}

#[test]
fn schema_authorization_checks_authoritative_role_catalog() {
    let engine = setup_engine();
    let mut state = AnalysisState::new(cache_with_public_schema());
    let findings = engine
        .analyze("CREATE SCHEMA app AUTHORIZATION missing_role;", &mut state)
        .unwrap();

    assert!(findings.iter().any(|finding| {
        finding.rule_id == "chain-conflict" && finding.reason.contains("does not exist")
    }));
    assert!(!state.schema_is_present_for_test("app"));
}

#[test]
fn schema_rename_remaps_namespace_and_rolls_back_atomically() {
    let engine = setup_engine();
    let mut cache = cache_with_public_schema();
    let owner = object_id("", "owner");
    cache.schemas.insert(
        "schema_old".into(),
        SchemaState {
            name: "schema_old".into(),
            owner: owner.clone(),
            generation: 0,
        },
    );
    cache.schemas.insert(
        "schema_new".into(),
        SchemaState {
            name: "schema_new".into(),
            owner: owner.clone(),
            generation: 0,
        },
    );
    cache.metadata.schemas = Some(vec![
        "public".into(),
        "schema_old".into(),
        "schema_new".into(),
    ]);
    let table = object_id("schema_old", "t");
    cache.insert_baseline(
        table.clone(),
        RelationState::new(
            table.clone(),
            owner.clone(),
            0,
            Some(0),
            RelationKind::Table,
            Persistence::Permanent,
            0,
        ),
    );
    let ty = object_id("schema_old", "mood");
    cache.types.insert(
        ty.clone(),
        TypeState {
            id: ty,
            generation: 0,
            kind: TypeKind::Enum {
                variants: vec!["ok".into()],
            },
        },
    );
    let sequence = object_id("schema_old", "t_id_seq");
    cache.sequences.insert(
        sequence.clone(),
        SequenceState {
            id: sequence,
            owner,
            owned_by: Some((table.clone(), "id".into())),
            kind: SequenceKind::Owned,
            generation: 0,
        },
    );
    cache.search_path = vec!["schema_old".into(), "public".into()];
    cache.metadata.source_search_path = Some(vec!["schema_old".into(), "public".into()]);
    let mut state = AnalysisState::new(cache);

    engine
        .analyze(
            "CREATE PUBLICATION app_publication FOR TABLE schema_old.t;",
            &mut state,
        )
        .unwrap();
    // Make the target a locally authoritative tombstone. A scoped baseline
    // cannot prove that an entirely unseen schema name is absent.
    engine
        .analyze("DROP SCHEMA schema_new CASCADE;", &mut state)
        .unwrap();

    let rename_findings = engine
        .analyze(
            "BEGIN; ALTER SCHEMA schema_old RENAME TO schema_new;",
            &mut state,
        )
        .unwrap();
    assert!(
        state.relation_is_present(&object_id("schema_new", "t")),
        "relations after rename: {:?}; findings: {:?}",
        state.local.relations.keys().collect::<Vec<_>>(),
        rename_findings
    );
    assert!(
        state
            .local
            .types
            .contains_key(&object_id("schema_new", "mood"))
    );
    assert!(
        state
            .local
            .sequences
            .contains_key(&object_id("schema_new", "t_id_seq"))
    );
    assert!(
        state
            .baseline_relations
            .contains(&object_id("schema_new", "t"))
    );
    assert_eq!(state.local.search_path_template, ["schema_old", "public"]);
    assert_eq!(state.local.search_path, ["public"]);
    assert!(matches!(
        state.local.publications.get("app_publication"),
        Some(safe_migrate::_internal::model::replication::PublicationOverlay::Present(publication))
            if matches!(
                &publication.scope,
                PublicationScope::Explicit(objects)
                    if matches!(
                        objects.first(),
                        Some(PublicationObjectFact::Table { name, .. })
                            if name.schema.as_ref().is_some_and(|schema| schema.resolve() == "schema_new")
                    )
            )
    ));

    engine.analyze("ROLLBACK;", &mut state).unwrap();
    assert!(state.relation_is_present(&object_id("schema_old", "t")));
    assert!(
        !state
            .local
            .relations
            .contains_key(&object_id("schema_new", "t"))
    );
    assert!(
        state
            .local
            .sequences
            .contains_key(&object_id("schema_old", "t_id_seq"))
    );
    assert_eq!(state.local.search_path, ["schema_old", "public"]);
    assert_eq!(
        state.baseline_schemas.as_ref(),
        Some(&std::collections::HashSet::from([
            "public".to_string(),
            "schema_old".to_string(),
            "schema_new".to_string(),
        ])),
        "rollback must restore authoritative schema scope as well as local objects"
    );
    assert!(matches!(
        state.local.publications.get("app_publication"),
        Some(safe_migrate::_internal::model::replication::PublicationOverlay::Present(publication))
            if matches!(
                &publication.scope,
                PublicationScope::Explicit(objects)
                    if matches!(
                        objects.first(),
                        Some(PublicationObjectFact::Table { name, .. })
                            if name.schema.as_ref().is_some_and(|schema| schema.resolve() == "schema_old")
                    )
            )
    ));

    engine
        .analyze("DROP SCHEMA schema_old CASCADE;", &mut state)
        .unwrap();
    assert!(matches!(
        state.local.publications.get("app_publication"),
        Some(safe_migrate::_internal::model::replication::PublicationOverlay::Present(publication))
            if matches!(
                &publication.scope,
                PublicationScope::Explicit(objects) if objects.is_empty()
            )
    ));
}

#[test]
fn table_set_schema_moves_the_relation_and_preserves_baseline_origin() {
    let engine = setup_engine();
    let mut cache = cache_with_public_schema();
    let owner = object_id("", "owner");
    cache.schemas.insert(
        "app".into(),
        SchemaState {
            name: "app".into(),
            owner: owner.clone(),
            generation: 0,
        },
    );
    let old_id = object_id("public", "accounts");
    cache.insert_baseline(
        old_id.clone(),
        RelationState::new(
            old_id.clone(),
            owner,
            0,
            Some(0),
            RelationKind::Table,
            Persistence::Permanent,
            0,
        ),
    );
    let sequence_id = object_id("public", "accounts_id_seq");
    cache.sequences.insert(
        sequence_id.clone(),
        SequenceState {
            id: sequence_id.clone(),
            owner: object_id("", "owner"),
            owned_by: Some((old_id.clone(), "id".into())),
            kind: SequenceKind::SerialLike,
            generation: 0,
        },
    );
    let index_id = object_id("public", "accounts_id_idx");
    cache.indexes.push(IndexCache {
        index_id: index_id.clone(),
        table_id: old_id.clone(),
        using_method: "btree".into(),
        key_columns: vec!["id".into()],
        included_columns: Vec::new(),
        dependency_columns: vec!["id".into()],
        dependency_columns_known: true,
        has_expression_keys: false,
        has_predicate: false,
        is_unique: false,
        is_valid: true,
        is_ready: true,
        is_live: true,
        has_default_sort_order: true,
        has_default_opclasses: true,
        has_default_collations: true,
    });
    let mut state = AnalysisState::new(cache);

    engine
        .analyze("ALTER TABLE public.accounts SET SCHEMA app;", &mut state)
        .unwrap();

    let new_id = object_id("app", "accounts");
    assert!(state.relation_is_present(&new_id));
    assert!(!state.relation_is_present(&old_id));
    assert!(state.baseline_relations.contains(&new_id));
    assert!(!state.baseline_relations.contains(&old_id));

    let new_sequence_id = object_id("app", "accounts_id_seq");
    assert!(matches!(
        state.local.sequences.get(&new_sequence_id),
        Some(SequenceOverlay::Present(sequence))
            if sequence.id == new_sequence_id
                && sequence.owned_by == Some((new_id.clone(), "id".into()))
    ));
    assert!(!state.local.sequences.contains_key(&sequence_id));
    assert!(state.baseline_sequences.contains(&new_sequence_id));
    assert!(!state.baseline_sequences.contains(&sequence_id));

    let new_index_id = object_id("app", "accounts_id_idx");
    assert!(!state.local.graph.edges().iter().any(|edge| {
        edge.dependent == index_id && matches!(edge.kind, DependencyKind::IndexOnRelation { .. })
    }));
    assert!(state.local.graph.edges().iter().any(|edge| {
        edge.dependent == new_index_id
            && matches!(edge.kind, DependencyKind::IndexOnRelation { .. })
    }));
    assert!(state.baseline_indexes.contains(&new_index_id));
    assert!(!state.baseline_indexes.contains(&index_id));
    assert!(state.local.graph.edges().iter().any(|edge| {
        edge.dependent == new_index_id
            && edge.referenced == new_id
            && matches!(edge.kind, DependencyKind::IndexOnRelation { .. })
    }));
}

#[test]
fn serial_and_identity_columns_create_distinct_owned_sequences() {
    let engine = setup_engine();
    let mut state = AnalysisState::new(cache_with_public_schema());
    engine
        .analyze(
            "CREATE TABLE accounts (
                id serial,
                external_id bigint GENERATED ALWAYS AS IDENTITY
             );",
            &mut state,
        )
        .unwrap();

    let serial_id = object_id("public", "accounts_id_seq");
    let identity_id = object_id("public", "accounts_external_id_seq");
    assert!(matches!(
        state.local.sequences.get(&serial_id),
        Some(SequenceOverlay::Present(sequence))
            if sequence.kind == SequenceKind::SerialLike
                && sequence.owned_by == Some((object_id("public", "accounts"), "id".into()))
    ));
    assert!(matches!(
        state.local.sequences.get(&identity_id),
        Some(SequenceOverlay::Present(sequence)) if sequence.kind == SequenceKind::Identity
    ));

    engine
        .analyze(
            "ALTER TABLE accounts RENAME COLUMN id TO account_id;",
            &mut state,
        )
        .unwrap();
    assert!(state.local.sequences.contains_key(&serial_id));
    assert!(matches!(
        state.local.sequences.get(&serial_id),
        Some(SequenceOverlay::Present(sequence))
            if sequence.owned_by == Some((object_id("public", "accounts"), "account_id".into()))
    ));

    engine
        .analyze("ALTER TABLE accounts DROP COLUMN external_id;", &mut state)
        .unwrap();
    assert!(matches!(
        state.local.sequences.get(&identity_id),
        Some(SequenceOverlay::Dropped)
    ));
}

#[test]
fn implicit_sequence_collision_uses_postgres_suffix() {
    let engine = setup_engine();
    let mut state = AnalysisState::new(cache_with_public_schema());
    engine
        .analyze(
            "CREATE SEQUENCE accounts_id_seq;
             CREATE TABLE accounts (id serial);",
            &mut state,
        )
        .unwrap();
    assert!(
        state
            .local
            .sequences
            .contains_key(&object_id("public", "accounts_id_seq1"))
    );
}

#[test]
fn sequence_ownership_move_drop_and_rollback_follow_dependency_rules() {
    let engine = setup_engine();
    let mut cache = cache_with_public_schema();
    cache.schemas.insert(
        "archive".into(),
        SchemaState {
            name: "archive".into(),
            owner: object_id("", "owner"),
            generation: 0,
        },
    );
    let mut state = AnalysisState::new(cache);
    engine
        .analyze(
            "CREATE TABLE events (id integer, serial_id serial,
                identity_id bigint GENERATED ALWAYS AS IDENTITY);
             CREATE SEQUENCE event_cursor;
             ALTER SEQUENCE event_cursor OWNED BY events.id;",
            &mut state,
        )
        .unwrap();

    let cursor = object_id("public", "event_cursor");
    assert!(matches!(
        state.local.sequences.get(&cursor),
        Some(SequenceOverlay::Present(sequence))
            if sequence.kind == SequenceKind::Owned
                && sequence.owned_by == Some((object_id("public", "events"), "id".into()))
    ));

    engine
        .analyze(
            "BEGIN; ALTER SEQUENCE event_cursor RENAME TO renamed_cursor; ROLLBACK;",
            &mut state,
        )
        .unwrap();
    assert!(matches!(
        state.local.sequences.get(&cursor),
        Some(SequenceOverlay::Present(_))
    ));
    assert!(
        !state
            .local
            .sequences
            .contains_key(&object_id("public", "renamed_cursor"))
    );

    let move_findings = engine
        .analyze(
            "ALTER SEQUENCE event_cursor SET SCHEMA archive;",
            &mut state,
        )
        .unwrap();
    assert!(move_findings.iter().any(|finding| {
        finding.rule_id == "chain-conflict" && finding.reason.contains("same schema")
    }));
    engine
        .analyze(
            "ALTER SEQUENCE event_cursor OWNED BY NONE;
             ALTER SEQUENCE event_cursor SET SCHEMA archive;",
            &mut state,
        )
        .unwrap();
    assert!(matches!(
        state
            .local
            .sequences
            .get(&object_id("archive", "event_cursor")),
        Some(SequenceOverlay::Present(sequence)) if sequence.kind == SequenceKind::Standalone
    ));

    let serial_id = object_id("public", "events_serial_id_seq");
    let restrict_findings = engine
        .analyze("DROP SEQUENCE events_serial_id_seq;", &mut state)
        .unwrap();
    assert!(restrict_findings.iter().any(|finding| {
        finding.rule_id == "chain-conflict" && finding.reason.contains("dependent defaults")
    }));
    assert!(matches!(
        state.local.sequences.get(&serial_id),
        Some(SequenceOverlay::Present(_))
    ));
    engine
        .analyze("DROP SEQUENCE events_serial_id_seq CASCADE;", &mut state)
        .unwrap();
    assert!(matches!(
        state.local.sequences.get(&serial_id),
        Some(SequenceOverlay::Dropped)
    ));

    let identity_id = object_id("public", "events_identity_id_seq");
    let identity_findings = engine
        .analyze("DROP SEQUENCE events_identity_id_seq CASCADE;", &mut state)
        .unwrap();
    assert!(identity_findings.iter().any(|finding| {
        finding.rule_id == "chain-conflict" && finding.reason.contains("identity sequence")
    }));
    assert!(matches!(
        state.local.sequences.get(&identity_id),
        Some(SequenceOverlay::Present(_))
    ));
}

// Keep test-only schema inspection out of the public production API surface.
trait SchemaTestExt {
    fn schema_is_present_for_test(&self, name: &str) -> bool;
}

impl SchemaTestExt for AnalysisState {
    fn schema_is_present_for_test(&self, name: &str) -> bool {
        matches!(
            self.local.schemas.get(name),
            Some(SchemaOverlay::Present(_))
        )
    }
}