safe-migrate 0.8.1

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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
mod common;

mod architectural_gap_tests {
    use crate::common::*;
    use safe_migrate::_internal::analysis::state::Confidence;
    use safe_migrate::_internal::ast::identifiers::ObjectId;
    use safe_migrate::_internal::model::relation::{Persistence, RelationKind, RelationOverlay};
    use safe_migrate::_internal::model::types::TypeOverlay;
    use safe_migrate::_internal::report::violations::ViolationTier;

    // 1. Foreign-key parent-table escalation
    #[test]
    fn test_fk_parent_table_lock_escalation() {
        let engine = setup_engine();
        let mut state = setup_state();
        // Use valid PostgreSQL topology so the FK mutation is applied and the
        // lock rule can classify the larger parent table.
        engine
            .analyze(
                "CREATE TABLE parent_tbl(id int PRIMARY KEY); CREATE TABLE child_tbl(p_id int);",
                &mut state,
            )
            .unwrap();
        if let Some(RelationOverlay::Present(parent)) = state
            .local
            .relations
            .get_mut(&object_id("public", "parent_tbl"))
        {
            parent.estimated_rows = Some(500_000);
        }
        if let Some(RelationOverlay::Present(child)) = state
            .local
            .relations
            .get_mut(&object_id("public", "child_tbl"))
        {
            child.estimated_rows = Some(10);
        }
        let violations = engine.analyze("ALTER TABLE child_tbl ADD CONSTRAINT fk FOREIGN KEY (p_id) REFERENCES parent_tbl(id);", &mut state).unwrap();

        let is_tier_1 = violations
            .iter()
            .any(|v| v.tier == ViolationTier::Tier1 && v.rule_id.contains("blocking-constraint"));
        assert!(
            is_tier_1,
            "Failed to escalate lock severity based on parent table size"
        );
    }

    // 2. Nested RELEASE SAVEPOINT rollback chain
    #[test]
    fn test_nested_release_savepoint_chain() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "
            BEGIN;
            CREATE TABLE t1(id int);
            SAVEPOINT s1;
            CREATE TABLE t2(id int);
            SAVEPOINT s2;
            CREATE TABLE t3(id int);
            RELEASE SAVEPOINT s2;
            ROLLBACK TO s1;
            COMMIT;
        ",
                &mut state,
            )
            .unwrap();

        assert!(state.relation_is_present(&object_id("public", "t1")));
        assert!(!state.relation_is_present(&object_id("public", "t2")));
        assert!(!state.relation_is_present(&object_id("public", "t3")));
    }

    // 3. ROLLBACK TO SAVEPOINT partial preservation
    #[test]
    fn test_rollback_to_savepoint_partial() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "
            BEGIN;
            CREATE TABLE a(id int PRIMARY KEY);
            SAVEPOINT s;
            CREATE TABLE b(id int);
            ROLLBACK TO s;
            CREATE TABLE c(id int);
            COMMIT;
        ",
                &mut state,
            )
            .unwrap();

        assert!(state.relation_is_present(&object_id("public", "a")));
        assert!(state.relation_is_present(&object_id("public", "c")));
        assert!(!state.relation_is_present(&object_id("public", "b")));
    }

    // 4. DROP SCHEMA CASCADE rename-edge cleanup
    #[test]
    fn test_drop_schema_cascade_cleans_renames() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE SCHEMA s; CREATE TABLE s.t(id int); ALTER TABLE s.t RENAME TO t2;",
                &mut state,
            )
            .unwrap();
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::RenameTo
                ))
                .count()
                != 0
        );

        engine
            .analyze("DROP SCHEMA s CASCADE;", &mut state)
            .unwrap();
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::RenameTo
                ))
                .count()
                == 0,
            "Rename edges leaked after schema cascade"
        );
    }

    // 5. Multi-schema search_path resolution
    #[test]
    fn test_multi_schema_search_path_resolution() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE SCHEMA s1; CREATE SCHEMA s2; SET search_path TO s1, s2;",
                &mut state,
            )
            .unwrap();
        engine
            .analyze("CREATE TABLE t1(id int);", &mut state)
            .unwrap();
        assert!(state.relation_is_present(&object_id("s1", "t1")));
    }

    // 6. Tombstone shadowing / recreate semantics
    #[test]
    fn test_tombstone_shadowing_recreate() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let gen1 = if let RelationOverlay::Present(r) =
            state.get_relation(&object_id("public", "t")).unwrap()
        {
            r.generation
        } else {
            0
        };

        engine.analyze("DROP TABLE t;", &mut state).unwrap();
        engine
            .analyze("CREATE TABLE t(new_id text);", &mut state)
            .unwrap();
        if let RelationOverlay::Present(r) = state.get_relation(&object_id("public", "t")).unwrap()
        {
            assert!(
                r.generation > gen1,
                "Recreated table must have higher generation"
            );
            assert!(r.has_column("new_id"));
        } else {
            panic!("Table did not recreate over tombstone");
        }
    }

    #[test]
    fn exact_baseline_treats_missing_unguarded_drop_as_conflict() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE exists_tbl(id int);", &mut state)
            .unwrap();
        let violations = engine
            .analyze("DROP TABLE missing_tbl;", &mut state)
            .unwrap();

        assert!(state.relation_is_present(&object_id("public", "exists_tbl")));
        assert_eq!(state.local.confidence, Confidence::Exact);
        assert!(
            violations
                .iter()
                .any(|violation| violation.rule_id == "chain-conflict")
        );
    }

    // 8. View dependency alias/CTE isolation
    #[test]
    fn test_view_dependency_cte_isolation() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE base_table(id int);", &mut state)
            .unwrap();
        engine
            .analyze(
                "CREATE VIEW v AS WITH my_cte AS (SELECT * FROM base_table) SELECT * FROM my_cte;",
                &mut state,
            )
            .unwrap();

        assert!(state.local.graph.edges().iter().any(|e| matches!(
            e.kind,
            safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
        ) && e.dependent
            == object_id("public", "v")
            && e.referenced == object_id("public", "base_table")));
        assert!(!state.local.graph.edges().iter().any(|e| matches!(
            e.kind,
            safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
        ) && e.dependent
            == object_id("public", "v")
            && e.referenced == object_id("public", "my_cte")));
    }

    #[test]
    fn test_view_dependency_schema_qualified() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE app_sessions(id int);", &mut state)
            .unwrap();
        engine
            .analyze("CREATE VIEW v AS SELECT * FROM app_sessions;", &mut state)
            .unwrap();

        assert!(state.local.graph.edges().iter().any(|e| matches!(
            e.kind,
            safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
        ) && e.dependent
            == object_id("public", "v")
            && e.referenced == object_id("public", "app_sessions")));
    }

    #[test]
    fn test_view_dependency_intermediate_segments_skipped() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE SCHEMA app; CREATE TABLE app.sessions(id int);",
                &mut state,
            )
            .unwrap();
        engine
            .analyze("CREATE VIEW v AS SELECT * FROM app.sessions;", &mut state)
            .unwrap();

        assert!(
            state.local.graph.edges().iter().any(|e| matches!(
                e.kind,
                safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
            ) && e.dependent == object_id("public", "v")
                && e.referenced == object_id("app", "sessions")),
            "Schema-qualified table should produce qualified depends_on entry"
        );

        assert!(
            !state.local.graph.edges().iter().any(|e| matches!(
                e.kind,
                safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
            ) && e.dependent == object_id("public", "v")
                && e.referenced == object_id("public", "app")),
            "Schema segment should not appear as a phantom dependency"
        );
    }

    // 9. Partition graph cleanup after DROP TABLE
    #[test]
    fn test_partition_graph_cleanup_on_drop() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine.analyze("CREATE TABLE p(id int) PARTITION BY RANGE(id); CREATE TABLE c PARTITION OF p FOR VALUES FROM (1) TO (10);", &mut state).unwrap();
        engine.analyze("DROP TABLE c;", &mut state).unwrap();
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::PartitionOf
                ))
                .count()
                == 0,
            "Partition edge leaked after child drop"
        );
    }

    // 10. Concurrent index rollback semantics
    #[test]
    fn test_concurrent_index_rollback() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        engine
            .analyze(
                "BEGIN; CREATE INDEX CONCURRENTLY idx ON t(id); ROLLBACK;",
                &mut state,
            )
            .unwrap();
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::IndexOnRelation { .. }
                ))
                .count()
                == 0
        );
    }

    // 11. Opaque confidence taint persistence
    #[test]
    fn test_opaque_confidence_taint_persistence() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("DO $$ BEGIN EXECUTE 'DROP TABLE x;'; END $$;", &mut state)
            .unwrap();
        assert_eq!(state.local.confidence, Confidence::Tainted);
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        assert_eq!(state.local.confidence, Confidence::Tainted);
    }

    // 12. Quoted identifier + search_path interaction
    #[test]
    fn test_quoted_ident_search_path() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE SCHEMA \"MySchema\"; SET search_path TO \"MySchema\";",
                &mut state,
            )
            .unwrap();
        engine
            .analyze("CREATE TABLE \"MyTable\" (\"MyCol\" int);", &mut state)
            .unwrap();
        assert!(state.relation_is_present(&object_id("MySchema", "MyTable")));
    }

    // 13. CREATE TYPE recreation after DROP
    #[test]
    fn test_create_domain_recreation() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE DOMAIN my_type AS int;", &mut state)
            .unwrap();
        engine.analyze("DROP DOMAIN my_type;", &mut state).unwrap();
        engine
            .analyze("CREATE DOMAIN my_type AS text;", &mut state)
            .unwrap();
        assert!(matches!(
            state.local.types.get(&object_id("public", "my_type")),
            Some(TypeOverlay::Present(_))
        ));
    }

    // 14. Duplicate/stale view-edge cleanup
    #[test]
    fn test_stale_view_edge_cleanup() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE TABLE t(id int); CREATE VIEW v AS SELECT * FROM t;",
                &mut state,
            )
            .unwrap();
        engine
            .analyze("DROP VIEW v; CREATE VIEW v AS SELECT * FROM t;", &mut state)
            .unwrap();
        assert_eq!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::ViewDependency { .. }
                ))
                .count(),
            1,
            "Duplicate view edge created"
        );
    }

    // 15. IF NOT EXISTS metadata preservation
    #[test]
    fn test_if_not_exists_preserves_original_metadata() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id INT);", &mut state)
            .unwrap();
        let gen1 = if let RelationOverlay::Present(r) =
            state.get_relation(&object_id("public", "t")).unwrap()
        {
            r.generation
        } else {
            0
        };

        engine
            .analyze(
                "CREATE TABLE IF NOT EXISTS t(id TEXT, diff_col INT);",
                &mut state,
            )
            .unwrap();

        let rel = state.get_relation(&object_id("public", "t")).unwrap();
        if let RelationOverlay::Present(r) = rel {
            assert_eq!(r.generation, gen1);
            assert_eq!(
                r.get_column("id").unwrap().data_type.as_deref(),
                Some("INT")
            );
            assert!(!r.has_column("diff_col"));
        }
    }

    // Rename traversal across cascade dependencies.
    #[test]
    fn test_deep_rename_traversal_cascade() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "
            CREATE TABLE a(id int PRIMARY KEY);
            CREATE VIEW v AS SELECT * FROM a;
            ALTER TABLE a RENAME TO b;
            DROP TABLE b CASCADE;
        ",
                &mut state,
            )
            .unwrap();

        // The View 'v' relies on 'a'. We renamed 'a' to 'b'.
        // Dropping 'b' should dynamically resolve the rename graph and correctly drop 'v'.
        assert!(
            !state.relation_is_present(&object_id("public", "a")),
            "Original table a should be gone"
        );
        assert!(
            !state.relation_is_present(&object_id("public", "b")),
            "Renamed table b should be gone"
        );
        assert!(
            !state.relation_is_present(&object_id("public", "v")),
            "Dependent view v should have been cascaded"
        );
    }

    // Partition cycle rejection.
    #[test]
    fn test_partition_cycle_rejection() {
        let engine = setup_engine();
        let mut state = setup_state();

        // Attempting to attach 'a' as a partition of 'b', while 'b' is a partition of 'a'
        let findings = engine
            .analyze(
                "
            CREATE TABLE a(id int) PARTITION BY RANGE(id);
            CREATE TABLE b PARTITION OF a FOR VALUES FROM (1) TO (10) PARTITION BY RANGE(id);
            ALTER TABLE b ATTACH PARTITION a FOR VALUES FROM (1) TO (10);
        ",
                &mut state,
            )
            .unwrap();

        // A modeled, impossible topology is a deterministic PostgreSQL-style
        // conflict, not unknown semantics. It must neither apply nor taint the
        // following chain state.
        assert_eq!(
            state.local.confidence,
            Confidence::Exact,
            "partition cycle conflict must not taint the engine"
        );
        assert!(
            findings
                .iter()
                .any(|finding| finding.rule_id == "chain-conflict")
        );
        // The rejected attach must leave the partition graph unchanged: 'a'
        // must not appear as a child of 'b' after the cycle is detected.
        let a_id = safe_migrate::_internal::ast::identifiers::ObjectId::new("public", "a");
        let b_id = safe_migrate::_internal::ast::identifiers::ObjectId::new("public", "b");
        assert!(
            !state.local.graph.partitions_of(&b_id).contains(&&a_id),
            "cycle rejection must not insert 'a' as a partition of 'b'"
        );
    }

    // 18. Tablespace and Access Method Rewrite Rule
    #[test]
    fn test_tablespace_access_method_rewrite() {
        let engine = setup_engine();
        let mut cache = safe_migrate::_internal::db::cache::DbCache::new();
        cache.metadata.schemas = Some(vec!["public".to_string()]);
        cache.search_path = vec!["public".to_string()];
        cache.schemas.insert(
            "public".to_string(),
            safe_migrate::_internal::model::schema::SchemaState {
                name: "public".to_string(),
                owner: ObjectId::new("", "postgres"),
                generation: 0,
            },
        );

        // Force Tier 1 by giving the table 150,000 rows
        let table_id = object_id("public", "massive_table");
        let mut table = safe_migrate::_internal::model::relation::RelationState::new(
            table_id.clone(),
            ObjectId::new("public", "postgres"),
            0,
            Some(150_000),
            RelationKind::Table,
            Persistence::Permanent,
            0,
        );
        table
            .columns
            .push(safe_migrate::_internal::model::column::Column {
                name: "id".to_string(),
                data_type: Some("integer".to_string()),
                type_id: None,
                is_nullable: true,
                default: None,
                avg_width: None,
                default_expr_text: None,
                type_modifier: Some(-1),
            });
        table.last_analyze = Some("2026-09-01 00:00:00+00".to_string());
        cache.insert_baseline(table_id.clone(), table);
        let mut state = safe_migrate::_internal::analysis::state::AnalysisState::with_baseline(
            cache.clone(),
            true,
        );
        let mut storage_state =
            safe_migrate::_internal::analysis::state::AnalysisState::with_baseline(cache, true);

        let v1 = engine
            .analyze(
                "ALTER TABLE public.massive_table SET ACCESS METHOD columnar;",
                &mut state,
            )
            .unwrap();
        assert!(
            v1.iter()
                .any(|v| v.rule_id == "table-rewrite-access-method"
                    && v.tier == ViolationTier::Tier1)
        );
        let v2 = engine
            .analyze(
                "ALTER TABLE public.massive_table ALTER COLUMN id SET STORAGE MAIN;",
                &mut storage_state,
            )
            .unwrap();
        assert!(
            v2.iter()
                .any(|v| v.rule_id == "table-rewrite-storage" && v.tier == ViolationTier::Tier1)
        );
    }
    // Generation counter rollback.
    #[test]
    fn test_generation_counter_rollback() {
        let engine = setup_engine();
        let mut state = setup_state();

        let initial_gen = state.local.generation_counter;

        engine
            .analyze("BEGIN; CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let mid_gen = state.local.generation_counter;
        assert!(
            mid_gen > initial_gen,
            "Generation counter should increment on create"
        );

        engine.analyze("ROLLBACK;", &mut state).unwrap();
        let post_gen = state.local.generation_counter;
        assert_eq!(
            post_gen, initial_gen,
            "Generation counter should restore strictly to pre-txn state on rollback"
        );
    }

    // Partition children in cascade closure.
    #[test]
    fn test_partition_children_cascade_enumeration() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "
            CREATE TABLE parent(id int) PARTITION BY RANGE(id);
            CREATE TABLE child PARTITION OF parent FOR VALUES FROM (1) TO (10);
            DROP TABLE parent CASCADE;
        ",
                &mut state,
            )
            .unwrap();

        assert!(
            !state.relation_is_present(&object_id("public", "parent")),
            "Parent should be dropped"
        );
        assert!(
            !state.relation_is_present(&object_id("public", "child")),
            "Child should be dropped via reverse-graph cascade"
        );
    }

    // Foreign-key graph lookups follow renames.
    #[test]
    fn test_rename_updates_fk_graph_edges() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "
            CREATE TABLE a(id int PRIMARY KEY);
            CREATE TABLE b(a_id int);
            ALTER TABLE b ADD CONSTRAINT fk FOREIGN KEY (a_id) REFERENCES a(id);
            ALTER TABLE a RENAME TO a2;
        ",
                &mut state,
            )
            .unwrap();

        let refs = state
            .local
            .graph
            .is_referenced_by_fk(&object_id("public", "a2"));
        assert!(
            !refs.is_empty(),
            "a2 should be recognized as referenced by b's FK dynamically"
        );
        assert_eq!(refs[0].0, &object_id("public", "b"));
    }

    // Search-path existence checks.
    #[test]
    fn test_search_path_existence_check() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "
            CREATE SCHEMA actual_schema;
            CREATE TABLE actual_schema.my_table(id int);
            SET search_path = nonexistent_schema, actual_schema;
            ALTER TABLE my_table ADD COLUMN new_col int;
        ",
                &mut state,
            )
            .unwrap();

        let rel = state
            .get_relation(&object_id("actual_schema", "my_table"))
            .unwrap();
        if let safe_migrate::_internal::model::relation::RelationOverlay::Present(r) = rel {
            assert!(
                r.has_column("new_col"),
                "Should resolve to actual_schema bypassing nonexistent_schema"
            );
        } else {
            panic!("Table not found; resolver hallucinated the schema");
        }
    }

    // Non-cascading drops validate dependents.
    #[test]
    fn drop_without_cascade_reports_conflict_and_preserves_dependents() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "
            CREATE TABLE a(id int PRIMARY KEY);
            CREATE TABLE b(a_id int);
            ALTER TABLE b ADD CONSTRAINT fk FOREIGN KEY (a_id) REFERENCES a(id);
        ",
                &mut state,
            )
            .unwrap();

        let violations = engine.analyze("DROP TABLE a;", &mut state).unwrap();

        assert!(violations.iter().any(|violation| {
            violation.rule_id == "chain-conflict"
                && violation.tier == ViolationTier::Tier1
                && violation.reason.contains("still has dependent objects")
        }));
        assert_eq!(
            state.local.confidence,
            Confidence::Exact,
            "A known PostgreSQL dependency conflict does not make simulation uncertain"
        );
        assert!(
            state.relation_is_present(&object_id("public", "a")),
            "Table a should not be dropped if dependents exist without CASCADE"
        );
    }

    // 24. ALTER TABLE typed actions produce opaque without crashing
    #[test]
    fn test_alter_table_set_access_method_typed() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let result = engine.analyze("ALTER TABLE t SET ACCESS METHOD heap;", &mut state);
        assert!(result.is_ok(), "SetAccessMethod should not crash");
        assert!(state.evidence().iter().any(|record| {
            record.code == safe_migrate::_internal::analysis::evidence::EvidenceCode::UnsupportedSemantics
        }));

        let mut state2 = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state2)
            .unwrap();
        let result2 = engine.analyze(
            "ALTER TABLE t ALTER COLUMN id SET STORAGE PLAIN;",
            &mut state2,
        );
        assert!(result2.is_ok(), "SetStorage should not crash");
        assert!(
            state2.evidence().iter().any(|record| {
                record.code == safe_migrate::_internal::analysis::evidence::EvidenceCode::UnsupportedSemantics
            }),
            "SetStorage must produce UnsupportedSemantics evidence"
        );
    }

    #[test]
    fn test_alter_table_disable_enable_trigger_typed() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let r1 = engine.analyze("ALTER TABLE t DISABLE TRIGGER ALL;", &mut state);
        assert!(r1.is_ok(), "DisableTrigger should not crash");
        let r2 = engine.analyze("ALTER TABLE t ENABLE TRIGGER ALL;", &mut state);
        assert!(r2.is_ok(), "EnableTrigger should not crash");
        let r3 = engine.analyze("ALTER TABLE t ENABLE TRIGGER my_trig;", &mut state);
        assert!(r3.is_ok(), "EnableTrigger named should not crash");
    }

    #[test]
    fn test_alter_table_set_schema_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let result = engine.analyze("ALTER TABLE t SET SCHEMA target;", &mut state);
        assert!(result.is_ok(), "SetSchema should not crash");
    }

    #[test]
    fn test_alter_table_set_tablespace_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let result = engine.analyze("ALTER TABLE t SET TABLESPACE fastspace;", &mut state);
        assert!(result.is_ok(), "SetTablespace should not crash");
    }

    #[test]
    fn test_alter_table_owner_to_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let result = engine.analyze("ALTER TABLE t OWNER TO app_owner;", &mut state);
        assert!(result.is_ok(), "OwnerTo should not crash");
    }

    #[test]
    fn test_alter_table_logged_unlogged_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let r1 = engine.analyze("ALTER TABLE t SET LOGGED;", &mut state);
        assert!(r1.is_ok(), "SetLogged should not crash");
        let r2 = engine.analyze("ALTER TABLE t SET UNLOGGED;", &mut state);
        assert!(r2.is_ok(), "SetUnlogged should not crash");
    }

    #[test]
    fn test_alter_table_replica_identity_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let r1 = engine.analyze("ALTER TABLE t REPLICA IDENTITY FULL;", &mut state);
        assert!(r1.is_ok(), "ReplicaIdentity FULL should not crash");
        let r2 = engine.analyze("ALTER TABLE t REPLICA IDENTITY DEFAULT;", &mut state);
        assert!(r2.is_ok(), "ReplicaIdentity DEFAULT should not crash");
        let r3 = engine.analyze("ALTER TABLE t REPLICA IDENTITY NOTHING;", &mut state);
        assert!(r3.is_ok(), "ReplicaIdentity NOTHING should not crash");
        let r4 = engine.analyze(
            "ALTER TABLE t REPLICA IDENTITY USING INDEX my_idx;",
            &mut state,
        );
        assert!(r4.is_ok(), "ReplicaIdentity USING INDEX should not crash");
    }

    #[test]
    fn test_alter_table_cluster_on_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let result = engine.analyze("ALTER TABLE t CLUSTER ON t_pkey;", &mut state);
        assert!(result.is_ok(), "ClusterOn should not crash");
    }

    #[test]
    fn test_alter_table_inherit_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE TABLE parent(id int); CREATE TABLE child(id int);",
                &mut state,
            )
            .unwrap();
        let result = engine.analyze("ALTER TABLE child INHERIT parent;", &mut state);
        assert!(result.is_ok(), "InheritTable should not crash");
        let r2 = engine.analyze("ALTER TABLE child NO INHERIT parent;", &mut state);
        assert!(r2.is_ok(), "NoInheritTable should not crash");
    }

    #[test]
    fn test_alter_table_rls_force_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let r1 = engine.analyze("ALTER TABLE t ENABLE ROW LEVEL SECURITY;", &mut state);
        assert!(r1.is_ok(), "EnableRls should not crash");
        let r2 = engine.analyze("ALTER TABLE t FORCE ROW LEVEL SECURITY;", &mut state);
        assert!(r2.is_ok(), "ForceRls should not crash");
        let r3 = engine.analyze("ALTER TABLE t DISABLE ROW LEVEL SECURITY;", &mut state);
        assert!(r3.is_ok(), "DisableRls should not crash");
    }

    #[test]
    fn test_alter_table_merge_split_partition_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze(
                "CREATE TABLE p(id int) PARTITION BY RANGE (id);
             CREATE TABLE p1 PARTITION OF p FOR VALUES FROM (1) TO (10);
             CREATE TABLE p2 PARTITION OF p FOR VALUES FROM (10) TO (20);",
                &mut state,
            )
            .unwrap();
        let sql = "ALTER TABLE p MERGE PARTITIONS p1, p2 INTO p_merged;";
        match engine.analyze(sql, &mut state) {
            Ok(_) => {} // passes
            Err(e) => {
                // Parser may not support MERGE PARTITIONS syntax — skip
                eprintln!("MergePartitions parse errors: {e:?}");
            }
        }
    }

    #[test]
    fn test_alter_table_enable_always_replica_trigger_does_not_crash() {
        let engine = setup_engine();
        let mut state = setup_state();
        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();
        let r1 = engine.analyze("ALTER TABLE t ENABLE ALWAYS TRIGGER my_tg;", &mut state);
        assert!(r1.is_ok(), "EnableAlwaysTrigger should not crash");
        let r2 = engine.analyze("ALTER TABLE t ENABLE REPLICA TRIGGER my_tg;", &mut state);
        assert!(r2.is_ok(), "EnableReplicaTrigger should not crash");
    }
}

// ─────────────────────────────────────────────
// 10. Multi-File Execution (analyze_chain)
// ─────────────────────────────────────────────