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
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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
mod common;

mod phase10_bug_fixes_and_sorting_tests {
    use crate::common::*;
    use safe_migrate::_internal::analysis::state::{AnalysisState, Confidence};
    use safe_migrate::_internal::ast::identifiers::ObjectId;
    use safe_migrate::_internal::db::cache::DbCache;
    use safe_migrate::_internal::model::relation::{Persistence, RelationKind};
    use safe_migrate::_internal::report::violations::{
        ObjectKind, OperationKind, Violation, ViolationTier,
    };

    fn baseline_index(
        index_id: ObjectId,
        table_id: ObjectId,
    ) -> safe_migrate::_internal::db::cache::IndexCache {
        safe_migrate::_internal::db::cache::IndexCache {
            index_id,
            table_id,
            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,
        }
    }

    #[test]
    fn comment_on_is_schema_neutral_and_preserves_confidence() {
        let engine = setup_engine();
        let mut state = setup_state();

        let violations = engine
            .analyze(
                "COMMENT ON TABLE future_table IS 'created elsewhere';",
                &mut state,
            )
            .expect("Squawk should parse COMMENT ON statements");

        assert!(violations.is_empty());
        assert_eq!(state.local.confidence, Confidence::Exact);
    }

    #[test]
    fn duplicate_column_assignments_remain_opaque() {
        let engine = setup_engine();
        for sql in [
            "UPDATE users SET display_name = 'first', display_name = 'second';",
            "INSERT INTO users (id) VALUES (1) ON CONFLICT (id) DO UPDATE
             SET display_name = 'first', display_name = 'second';",
        ] {
            let mut state = setup_state();
            let violations = engine
                .analyze(sql, &mut state)
                .expect("Squawk should parse duplicate assignments");

            assert!(
                violations
                    .iter()
                    .any(|violation| violation.rule_id == "opaque-dynamic-sql"),
                "duplicate DML assignment must remain opaque: {sql}"
            );
            assert_eq!(state.local.confidence, Confidence::Tainted);
        }
    }

    #[test]
    fn non_ascii_sql_before_execute_prefix_does_not_panic_normalization() {
        let engine = setup_engine();
        let mut state = setup_state();

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            engine.analyze("SELECT 'é';", &mut state)
        }));

        assert!(
            result.is_ok(),
            "normalizing valid non-ASCII SQL must not panic"
        );
    }

    #[test]
    fn test_bug008_index_not_in_baseline_relations() {
        let mut cache = DbCache::new();
        cache.indexes.push(baseline_index(
            object_id("public", "idx_accounts_username"),
            object_id("public", "accounts"),
        ));
        let state = AnalysisState::new(cache);
        assert!(
            !state
                .baseline_relations
                .contains(&object_id("public", "idx_accounts_username"))
        );
    }

    #[test]
    fn test_bug008_index_in_baseline_indexes() {
        let mut cache = DbCache::new();
        cache.indexes.push(baseline_index(
            object_id("public", "idx_accounts_username"),
            object_id("public", "accounts"),
        ));
        let state = AnalysisState::new(cache);
        assert!(
            state
                .baseline_indexes
                .contains(&object_id("public", "idx_accounts_username"))
        );
    }

    #[test]
    fn test_bug008_stale_stats_does_not_fire_on_index() {
        let engine = setup_engine();
        let mut cache = DbCache::new();
        cache.insert_baseline(
            object_id("public", "accounts"),
            safe_migrate::_internal::model::relation::RelationState::new(
                object_id("public", "accounts"),
                ObjectId::new("public", "postgres"),
                0,
                Some(500_000),
                RelationKind::Table,
                Persistence::Permanent,
                0,
            ),
        );
        cache.indexes.push(baseline_index(
            object_id("public", "idx_accounts_username"),
            object_id("public", "accounts"),
        ));

        let mut state = AnalysisState::new(cache);

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

        for v in &violations {
            if v.reason.contains("statistics are stale") {
                assert!(
                    !v.reason.contains("idx_accounts_username"),
                    "Should not fire stale stats warning on the index itself"
                );
            }
        }
    }

    #[test]
    fn stale_constraint_stats_do_not_flag_unrelated_alter_table_actions() {
        let engine = setup_engine();
        let mut cache = DbCache::new();
        cache.insert_baseline(
            object_id("public", "accounts"),
            safe_migrate::_internal::model::relation::RelationState::new(
                object_id("public", "accounts"),
                ObjectId::new("public", "postgres"),
                0,
                Some(500_000),
                RelationKind::Table,
                Persistence::Permanent,
                0,
            ),
        );
        let mut state = AnalysisState::new(cache);

        let violations = engine
            .analyze(
                "ALTER TABLE accounts ADD COLUMN display_name text;",
                &mut state,
            )
            .unwrap();

        assert!(
            !violations
                .iter()
                .any(|violation| violation.rule_id == "blocking-constraint")
        );
    }

    #[test]
    fn test_add_column_now_default_not_flagged() {
        let engine = setup_engine();
        let mut cache = DbCache::new();
        cache.pg_version_num = Some(110000);
        cache.insert_baseline(
            object_id("public", "t"),
            safe_migrate::_internal::model::relation::RelationState::new(
                object_id("public", "t"),
                ObjectId::new("public", "postgres"),
                0,
                Some(500_000),
                RelationKind::Table,
                Persistence::Permanent,
                0,
            ),
        );

        let mut state = AnalysisState::new(cache);

        let violations = engine
            .analyze(
                "ALTER TABLE t ADD COLUMN created_at TIMESTAMP DEFAULT NOW();",
                &mut state,
            )
            .unwrap();

        assert!(
            !violations
                .iter()
                .any(|v| v.rule_id == "size-aware-add-column"),
            "Expected no size-aware-add-column violation for stable DEFAULT NOW() on PG11+, got: {:?}",
            violations
        );
    }

    #[test]
    fn test_drop_index_no_stale_stats_warning() {
        let engine = setup_engine();
        let mut cache = DbCache::new();
        cache.insert_baseline(
            object_id("public", "t"),
            safe_migrate::_internal::model::relation::RelationState::new(
                object_id("public", "t"),
                ObjectId::new("public", "postgres"),
                0,
                Some(500_000),
                RelationKind::Table,
                Persistence::Permanent,
                0,
            ),
        );
        cache.indexes.push(baseline_index(
            object_id("public", "idx_t_id"),
            object_id("public", "t"),
        ));

        let mut state = AnalysisState::new(cache);

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

        assert!(
            violations
                .iter()
                .any(|v| v.rule_id == "require-concurrent-drop-index"),
            "Expected require-concurrent-drop-index violation"
        );

        assert!(
            !violations
                .iter()
                .any(|v| v.reason.contains("statistics are stale") || v.reason.contains("stale")),
            "Should not emit stale statistics warning on DROP INDEX, got: {:?}",
            violations
        );
    }

    #[test]
    fn test_deterministic_violation_sorting() {
        let engine = setup_engine();

        let mut cache = DbCache::new();
        cache.insert_baseline(
            object_id("public", "t"),
            safe_migrate::_internal::model::relation::RelationState::new(
                object_id("public", "t"),
                ObjectId::new("public", "postgres"),
                0,
                Some(500_000),
                RelationKind::Table,
                Persistence::Permanent,
                0,
            ),
        );
        let mut state = AnalysisState::new(cache);

        let sql = "CREATE INDEX idx2 ON t(a); CREATE INDEX idx1 ON t(b);";
        let violations = engine.analyze(sql, &mut state).unwrap();

        let idx_violations: Vec<&Violation> = violations
            .iter()
            .filter(|v| {
                v.rule_id == "require-concurrent-index"
                    && v.reason.contains("Synchronous index creation")
            })
            .collect();

        assert_eq!(idx_violations.len(), 2);
        assert_eq!(idx_violations[0].object_name, "public.idx2");
        assert_eq!(idx_violations[1].object_name, "public.idx1");
    }

    #[test]
    #[allow(clippy::useless_vec)]
    fn test_manual_violation_sorting() {
        let v_tier3 = Violation {
            source_range: Some(rowan::TextRange::new(0.into(), 10.into())),
            rule_id: "rule_a",
            operation_kind: OperationKind::DropTable,
            object_kind: ObjectKind::Table,
            object_name: "a".to_string(),
            tier: ViolationTier::Tier3,
            reason: "tier 3".to_string(),
            recipe: "recipe",
            dedup_key: None,
            sql: None,
            fk_dependency_related: false,
        };
        let v_tier1 = Violation {
            source_range: Some(rowan::TextRange::new(0.into(), 10.into())),
            rule_id: "rule_b",
            operation_kind: OperationKind::DropTable,
            object_kind: ObjectKind::Table,
            object_name: "b".to_string(),
            tier: ViolationTier::Tier1,
            reason: "tier 1".to_string(),
            recipe: "recipe",
            dedup_key: None,
            sql: None,
            fk_dependency_related: false,
        };
        let v_range_later = Violation {
            source_range: Some(rowan::TextRange::new(20.into(), 30.into())),
            rule_id: "rule_c",
            operation_kind: OperationKind::DropTable,
            object_kind: ObjectKind::Table,
            object_name: "c".to_string(),
            tier: ViolationTier::Tier1,
            reason: "tier 1 later range".to_string(),
            recipe: "recipe",
            dedup_key: None,
            sql: None,
            fk_dependency_related: false,
        };
        let v_name_later = Violation {
            source_range: Some(rowan::TextRange::new(0.into(), 10.into())),
            rule_id: "rule_b",
            operation_kind: OperationKind::DropTable,
            object_kind: ObjectKind::Table,
            object_name: "z".to_string(),
            tier: ViolationTier::Tier1,
            reason: "tier 1 name later".to_string(),
            recipe: "recipe",
            dedup_key: None,
            sql: None,
            fk_dependency_related: false,
        };

        let mut violations = vec![
            v_tier3.clone(),
            v_tier1.clone(),
            v_range_later.clone(),
            v_name_later.clone(),
        ];

        violations.sort_by(|a, b| {
            a.tier
                .cmp(&b.tier)
                .then_with(|| match (&a.source_range, &b.source_range) {
                    (Some(ar), Some(br)) => ar
                        .start()
                        .cmp(&br.start())
                        .then_with(|| ar.end().cmp(&br.end())),
                    (Some(_), None) => std::cmp::Ordering::Less,
                    (None, Some(_)) => std::cmp::Ordering::Greater,
                    (None, None) => std::cmp::Ordering::Equal,
                })
                .then_with(|| a.object_name.cmp(&b.object_name))
                .then_with(|| a.rule_id.cmp(b.rule_id))
        });

        assert_eq!(violations[0].reason, "tier 1");
        assert_eq!(violations[1].reason, "tier 1 name later");
        assert_eq!(violations[2].reason, "tier 1 later range");
        assert_eq!(violations[3].reason, "tier 3");
    }

    #[test]
    fn test_source_range_excludes_preceding_comments() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "-- This is a preceding comment\n   /* block comment */\n   DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            !violations.is_empty(),
            "expected a violation for DROP DATABASE"
        );
        let v = &violations[0];
        let range = v.source_range.expect("expected source_range to be set");

        let expected_start = sql.find("DROP DATABASE").unwrap();
        assert_eq!(usize::from(range.start()), expected_start);
        assert_eq!(usize::from(range.end()), sql.len());
    }

    #[test]
    fn test_bug003_broken_compute_fires_on_drop_function() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "CREATE FUNCTION notify_func() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN NEW; END';",
                &mut state,
            )
            .unwrap();

        engine
            .analyze(
                "CREATE TABLE events(id int);
                 CREATE TRIGGER trg_events AFTER INSERT ON events FOR EACH ROW EXECUTE FUNCTION notify_func();",
                &mut state,
            )
            .unwrap();

        let v = engine
            .analyze("DROP FUNCTION notify_func();", &mut state)
            .unwrap();

        assert!(
            v.iter()
                .any(|violation| violation.rule_id == "broken-compute"),
            "expected the trigger dependency to explain the rejected drop"
        );
    }

    #[test]
    fn concurrent_index_if_not_exists_still_fails_inside_a_transaction() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "CREATE TABLE tbl(id int);
                 CREATE INDEX idx_t ON tbl(id);",
                &mut state,
            )
            .unwrap();

        let v = engine
            .analyze(
                "BEGIN;
                 CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_t ON tbl(id);
                 COMMIT;",
                &mut state,
            )
            .unwrap();

        assert!(
            v.iter()
                .any(|violation| violation.rule_id == "concurrent-in-transaction"),
            "PostgreSQL checks the transaction restriction before IF NOT EXISTS can skip the index"
        );
    }

    #[test]
    fn test_bug005_create_table_as_select_skips_on_skipped_mutation() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE tbl(id int);", &mut state)
            .unwrap();

        let v = engine
            .analyze("CREATE TABLE IF NOT EXISTS tbl AS SELECT 1;", &mut state)
            .unwrap();

        assert!(
            !v.iter()
                .any(|violation| violation.rule_id == "create-table-as-select"),
            "Expected no create-table-as-select violation because the table already exists and statement was skipped"
        );
    }

    #[test]
    fn test_bug007_broken_compute_no_fire_on_skipped_drop() {
        let engine = setup_engine();
        let mut state = setup_state();

        let v = engine
            .analyze("DROP FUNCTION IF EXISTS nonexistent_func();", &mut state)
            .unwrap();

        assert!(
            !v.iter()
                .any(|violation| violation.rule_id == "broken-compute"),
            "Expected no broken-compute violation for nonexistent function"
        );
    }

    #[test]
    fn test_bug013_confidence_restored_on_rollback() {
        let engine = setup_engine();
        let mut cache = safe_migrate::_internal::db::cache::DbCache::new();
        let tid = object_id("public", "t");
        cache.insert_baseline(
            tid.clone(),
            safe_migrate::_internal::model::relation::RelationState::new(
                tid.clone(),
                object_id("public", "postgres"),
                0,
                Some(10),
                safe_migrate::_internal::model::relation::RelationKind::Table,
                safe_migrate::_internal::model::relation::Persistence::Permanent,
                0,
            ),
        );
        let mut state = AnalysisState::new(cache);

        // Opaque statement (like DO block) inside transaction block, then ROLLBACK
        engine
            .analyze(
                "BEGIN;
                 DO $$ BEGIN END $$;
                 ROLLBACK;",
                &mut state,
            )
            .unwrap();

        // Dropping table cascade which normally is Tier 1, but would be downgraded to Tier 2 if confidence is tainted.
        let v = engine.analyze("DROP TABLE t CASCADE;", &mut state).unwrap();

        assert!(
            v.iter().any(|violation| violation.tier
                == safe_migrate::_internal::report::violations::ViolationTier::Tier1),
            "Expected Tier1 because confidence should be restored to Exact after rollback"
        );
    }

    #[test]
    fn test_bug014_drop_schema_cascade_cleans_trigger_edges() {
        let engine = setup_engine();
        let mut state = setup_state();

        // 1. Create schema, table, trigger function, trigger, and publication
        engine
            .analyze(
                "CREATE SCHEMA s;
                 CREATE FUNCTION s.notify_func() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN NEW; END';
                 CREATE TABLE s.t1(id int);
                 CREATE TRIGGER trg AFTER INSERT ON s.t1 FOR EACH ROW EXECUTE FUNCTION s.notify_func();
                 CREATE PUBLICATION pub FOR TABLE s.t1;",
                &mut state,
            )
            .unwrap();

        // Verify trigger and publication edges are present
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::TriggerOnTable { .. }
                ))
                .count()
                != 0
        );
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::PublicationIncludes { .. }
                ))
                .count()
                != 0
        );

        // 2. Drop schema cascade
        engine
            .analyze("DROP SCHEMA s CASCADE;", &mut state)
            .unwrap();

        // Verify trigger and publication edges are cleaned up
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::TriggerOnTable { .. }
                ))
                .count()
                == 0
        );
        assert!(
            state
                .local
                .graph
                .edges()
                .iter()
                .filter(|e| matches!(
                    e.kind,
                    safe_migrate::_internal::analysis::graph::DependencyKind::PublicationIncludes { .. }
                ))
                .count()
                == 0
        );

        // 3. Drop a completely unrelated function (say public.unrelated())
        engine
            .analyze(
                "CREATE FUNCTION unrelated() RETURNS void LANGUAGE plpgsql AS 'BEGIN END';",
                &mut state,
            )
            .unwrap();

        let v = engine
            .analyze("DROP FUNCTION unrelated();", &mut state)
            .unwrap();

        assert!(
            !v.iter()
                .any(|violation| violation.rule_id == "broken-compute"),
            "Expected no broken-compute violation for unrelated function after schema drop cascade"
        );
    }

    #[test]
    fn test_bug001_vacuum_full_all_tables() {
        let engine = setup_engine();
        let mut state = setup_state();

        let v = engine.analyze("VACUUM FULL;", &mut state).unwrap();

        let violation = v
            .iter()
            .find(|v| v.rule_id == "vacuum-full")
            .expect("Expected vacuum-full violation");

        assert_eq!(violation.object_name, "<all tables>");
    }

    // ─────────────────────────────────────────────
    // Partition strategy matching.
    // ─────────────────────────────────────────────
    #[test]
    fn test_finding2_partition_strategy_mismatch_silent_on_regular_child() {
        let engine = setup_engine();
        let mut cache = safe_migrate::_internal::db::cache::DbCache::new();
        let parent_id = object_id("public", "parent");
        let mut parent = safe_migrate::_internal::model::relation::RelationState::new(
            parent_id.clone(),
            object_id("public", "postgres"),
            0,
            Some(0),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        parent.partition_type = Some("RANGE".to_string());
        cache.insert_baseline(parent_id, parent);

        let child_id = object_id("public", "child");
        let child = safe_migrate::_internal::model::relation::RelationState::new(
            child_id,
            object_id("public", "postgres"),
            0,
            Some(0),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        cache.insert_baseline(object_id("public", "child"), child);

        let mut state = AnalysisState::new(cache);

        let v = engine
            .analyze(
                "ALTER TABLE parent ATTACH PARTITION child FOR VALUES FROM (1) TO (10);",
                &mut state,
            )
            .unwrap();

        // A regular (non-partitioned) table attached to a RANGE parent is valid SQL.
        // The rule should only fire when the child HAS a partition strategy that mismatches.
        assert!(
            !v.iter().any(|v| v.rule_id == "partition-strategy-mismatch"),
            "partition-strategy-mismatch should NOT fire for attaching a regular table"
        );
    }

    #[test]
    fn test_finding2_partition_strategy_mismatch_fires_on_mismatch() {
        let engine = setup_engine();
        let mut cache = safe_migrate::_internal::db::cache::DbCache::new();
        let parent_id = object_id("public", "parent");
        let mut parent = safe_migrate::_internal::model::relation::RelationState::new(
            parent_id.clone(),
            object_id("public", "postgres"),
            0,
            Some(10),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        parent.partition_type = Some("RANGE".to_string());
        cache.insert_baseline(parent_id, parent);

        let child_id = object_id("public", "child");
        let mut child = safe_migrate::_internal::model::relation::RelationState::new(
            child_id,
            object_id("public", "postgres"),
            0,
            Some(0),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        child.partition_type = Some("LIST".to_string());
        cache.insert_baseline(object_id("public", "child"), child);

        let mut state = AnalysisState::new(cache);

        let v = engine
            .analyze(
                "ALTER TABLE parent ATTACH PARTITION child FOR VALUES FROM (1) TO (10);",
                &mut state,
            )
            .unwrap();

        let rule_violations: Vec<&str> = v.iter().map(|v| v.rule_id).collect();
        assert!(
            v.iter().any(|v| v.rule_id == "partition-strategy-mismatch"),
            "Expected partition-strategy-mismatch violation when strategies differ. Got: {:?}",
            rule_violations
        );
    }

    #[test]
    fn test_finding2_partition_strategy_mismatch_silent_on_match() {
        let engine = setup_engine();
        let mut state = setup_state();

        let v = engine
            .analyze(
                "
            CREATE TABLE parent(id int) PARTITION BY RANGE(id);
            CREATE TABLE child(id int) PARTITION BY RANGE(id);
            ALTER TABLE parent ATTACH PARTITION child FOR VALUES FROM (1) TO (10);
        ",
                &mut state,
            )
            .unwrap();

        assert!(
            !v.iter().any(|v| v.rule_id == "partition-strategy-mismatch"),
            "Expected no partition-strategy-mismatch violation when strategies match"
        );
    }

    // ─────────────────────────────────────────────
    // Enum additions inside transactions.
    // ─────────────────────────────────────────────
    #[test]
    fn test_finding2_alter_type_add_value_fires_inside_txn() {
        let engine = setup_engine();
        let mut state = setup_state();

        let v = engine
            .analyze(
                "BEGIN; ALTER TYPE public.mood ADD VALUE 'ok'; COMMIT;",
                &mut state,
            )
            .unwrap();

        let finding = v
            .iter()
            .find(|v| v.rule_id == "alter-type-add-value-txn")
            .expect("Expected alter-type-add-value-txn violation inside transaction");
        assert_eq!(finding.tier, ViolationTier::Tier2);
        assert!(
            finding
                .reason
                .contains("does not allow the new value to be used until commit")
        );
    }

    #[test]
    fn test_finding2_alter_type_add_value_silent_outside_txn() {
        let engine = setup_engine();
        let mut state = setup_state();

        let v = engine
            .analyze("ALTER TYPE public.mood ADD VALUE 'ok';", &mut state)
            .unwrap();

        assert!(
            !v.iter().any(|v| v.rule_id == "alter-type-add-value-txn"),
            "Expected no alter-type-add-value-txn violation outside transaction"
        );
    }

    #[test]
    fn alter_type_rename_value_is_not_reported_as_add_value() {
        let engine = setup_engine();
        let mut state = setup_state();

        let violations = engine
            .analyze(
                "BEGIN; ALTER TYPE public.mood RENAME VALUE 'sad' TO 'blue'; COMMIT;",
                &mut state,
            )
            .unwrap();

        assert!(
            !violations
                .iter()
                .any(|violation| violation.rule_id == "alter-type-add-value-txn")
        );
    }

    // ─────────────────────────────────────────────
    // Guarded column drops.
    // ─────────────────────────────────────────────
    #[test]
    fn test_finding8_drop_column_if_exists_noop() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();

        let v = engine
            .analyze(
                "ALTER TABLE t DROP COLUMN IF EXISTS nonexistent_col;",
                &mut state,
            )
            .unwrap();

        // Should not trigger irreversible-migration for a column that never existed
        assert!(
            !v.iter().any(|v| v.rule_id == "irreversible-migration"),
            "ReversibilityRule should not fire on DROP COLUMN IF EXISTS of nonexistent column"
        );
    }

    #[test]
    fn test_finding8_drop_column_if_exists_with_existing_column() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int, name text);", &mut state)
            .unwrap();

        let v = engine
            .analyze("ALTER TABLE t DROP COLUMN IF EXISTS name;", &mut state)
            .unwrap();

        // Should trigger irreversible-migration because 'name' existed and was dropped
        assert!(
            v.iter().any(|v| v.rule_id == "irreversible-migration"),
            "ReversibilityRule should fire on DROP COLUMN IF EXISTS of an existing column"
        );
    }

    #[test]
    fn drop_nonexistent_column_without_if_exists_reports_exact_conflict() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();

        let violations = engine
            .analyze("ALTER TABLE t DROP COLUMN nonexistent_col;", &mut state)
            .unwrap();

        assert!(violations.iter().any(|violation| {
            violation.rule_id == "chain-conflict"
                && violation.tier == ViolationTier::Tier1
                && violation
                    .reason
                    .contains("column 'nonexistent_col' does not exist")
        }));
        assert!(
            !violations
                .iter()
                .any(|violation| violation.rule_id == "irreversible-migration"),
            "A failed DROP COLUMN did not perform an irreversible operation"
        );
        assert_eq!(
            state.local.confidence,
            safe_migrate::_internal::analysis::state::Confidence::Exact,
            "A known PostgreSQL column conflict does not make simulation uncertain"
        );
    }

    // ─────────────────────────────────────────────
    // Bug 16 — Duplicate column name on Rename
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug016_rename_prevents_duplicate_column_name() {
        use safe_migrate::_internal::model::column::Column;
        use safe_migrate::_internal::model::relation::ColumnAction;
        use safe_migrate::_internal::model::relation::RelationState;

        let mut rel = RelationState::new(
            ObjectId::new("public", "t"),
            ObjectId::new("public", "postgres"),
            0,
            Some(10),
            RelationKind::Table,
            Persistence::Permanent,
            0,
        );
        rel.columns.push(Column {
            name: "a".into(),
            data_type: Some("int".into()),
            type_id: None,
            is_nullable: true,
            default: None,
            avg_width: None,
            default_expr_text: None,
            type_modifier: None,
        });
        rel.columns.push(Column {
            name: "b".into(),
            data_type: Some("int".into()),
            type_id: None,
            is_nullable: true,
            default: None,
            avg_width: None,
            default_expr_text: None,
            type_modifier: None,
        });

        // Rename "a" to "b" — "b" already exists, so rename should be a no-op
        rel.apply_column_action(&ColumnAction::Rename {
            from: "a".into(),
            to: "b".into(),
        });

        assert_eq!(rel.columns.len(), 2, "no new column should be created");
        assert!(
            rel.has_column("a"),
            "column 'a' should still exist (rename skipped)"
        );
        assert!(rel.has_column("b"), "column 'b' should still exist");
    }

    // ─────────────────────────────────────────────
    // Bug 17 — SetType/SetDefault on nonexistent column
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug017_set_type_on_nonexistent_column_is_a_conflict() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();

        let violations = engine
            .analyze(
                "ALTER TABLE t ALTER COLUMN nonexistent_col SET DATA TYPE text;",
                &mut state,
            )
            .unwrap();

        assert!(violations.iter().any(|violation| {
            violation.rule_id == "chain-conflict" && violation.reason.contains("nonexistent_col")
        }));
        assert_eq!(state.local.confidence, Confidence::Exact);
    }

    #[test]
    fn test_bug017_set_default_on_nonexistent_column_is_a_conflict() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();

        let violations = engine
            .analyze(
                "ALTER TABLE t ALTER COLUMN nonexistent_col SET DEFAULT 42;",
                &mut state,
            )
            .unwrap();

        assert!(violations.iter().any(|violation| {
            violation.rule_id == "chain-conflict" && violation.reason.contains("nonexistent_col")
        }));
        assert_eq!(state.local.confidence, Confidence::Exact);
    }

    #[test]
    fn test_bug017_set_type_on_existing_column_succeeds() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE TABLE t(id int);", &mut state)
            .unwrap();

        engine
            .analyze(
                "ALTER TABLE t ALTER COLUMN id SET DATA TYPE bigint;",
                &mut state,
            )
            .unwrap();

        // Confidence should remain Exact since the column exists
        assert_eq!(
            state.local.confidence,
            safe_migrate::_internal::analysis::state::Confidence::Exact,
            "Confidence should remain Exact when SET TYPE on existing column"
        );
    }

    #[test]
    fn alter_quoted_column_resolves_the_created_column() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                r#"CREATE TABLE entries ("Camel" int);
                   ALTER TABLE entries ALTER COLUMN "Camel" TYPE bigint;"#,
                &mut state,
            )
            .unwrap();

        assert_eq!(state.local.confidence, Confidence::Exact);
    }
    // ─────────────────────────────────────────────
    // Bug 9 — Privilege enum consistency: All variant
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug009_privilege_all_variant_consistency() {
        use safe_migrate::_internal::model::relation::Privilege;
        use safe_migrate::_internal::model::relation::PrivilegeMatrix;
        use std::collections::HashSet;

        // 1. Both enums now have All (compile-time check — relation::Privilege::All exists)
        let _all = Privilege::All;

        let mut matrix = PrivilegeMatrix::default();
        let role = object_id("public", "test_role");
        let mut all_privileges = HashSet::new();
        all_privileges.insert(Privilege::All);

        // 2. Grant All, then check individual privileges
        matrix.grant(role.clone(), all_privileges);
        assert!(matrix.has_privilege(&role, Privilege::Select));
        assert!(matrix.has_privilege(&role, Privilege::Insert));
        assert!(matrix.has_privilege(&role, Privilege::Update));
        assert!(matrix.has_privilege(&role, Privilege::Delete));
        assert!(matrix.has_privilege(&role, Privilege::Truncate));
        assert!(matrix.has_privilege(&role, Privilege::References));
        assert!(matrix.has_privilege(&role, Privilege::Trigger));
        assert!(matrix.has_privilege(&role, Privilege::Maintain));

        // 3. has_privilege for All itself should also work
        assert!(matrix.has_privilege(&role, Privilege::All));

        // 4. Revoke All clears everything
        let mut revoke_all = HashSet::new();
        revoke_all.insert(Privilege::All);
        matrix.revoke(&role, &revoke_all);
        assert!(!matrix.has_privilege(&role, Privilege::Select));
        assert!(!matrix.has_privilege(&role, Privilege::All));
    }

    #[test]
    fn postgres17_all_grant_tracks_maintain_without_leaking_to_older_versions() {
        use safe_migrate::_internal::model::relation::{Privilege, RelationOverlay};

        let engine = setup_engine();
        for (version, expected) in [(170_000, true), (160_000, false)] {
            let mut cache = cache_with_table("public", "t_large", None);
            cache.pg_version_num = Some(version);
            let mut state = AnalysisState::new(cache);
            engine
                .analyze("GRANT ALL ON TABLE t_large TO app_user;", &mut state)
                .expect("GRANT ALL should analyze");

            let Some(RelationOverlay::Present(relation)) =
                state.local.relations.get(&object_id("public", "t_large"))
            else {
                panic!("baseline relation should remain present");
            };
            let grantee = object_id("", "app_user");
            assert_eq!(
                relation
                    .privileges
                    .grants
                    .get(&grantee)
                    .is_some_and(|privileges| privileges.contains(&Privilege::Maintain)),
                expected,
                "PG {version} GRANT ALL MAINTAIN expansion mismatch"
            );
        }
    }
    // ─────────────────────────────────────────────
    // Bug 14 — Directive parsing tolerates whitespace
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug014_directive_ignore_with_spaces() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "/* safe-migrate: ignore ( drop-database ) */ DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            !violations.iter().any(|v| v.rule_id == "drop-database"),
            "ignore directive with spaces before parens should suppress the rule"
        );
    }

    #[test]
    fn test_bug014_directive_ignore_no_spaces() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "/* safe-migrate: ignore(drop-database) */ DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            !violations.iter().any(|v| v.rule_id == "drop-database"),
            "ignore directive without spaces should still work"
        );
    }

    #[test]
    fn test_bug014_directive_ignore_file_with_spaces() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "/* safe-migrate: ignore-file ( drop-database ) */ DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            !violations.iter().any(|v| v.rule_id == "drop-database"),
            "ignore-file directive with spaces before parens should suppress the rule"
        );
    }

    #[test]
    fn test_bug014_directive_comment_variations() {
        let engine = setup_engine();
        let mut state = setup_state();

        // Line comment variant
        let sql = "-- safe-migrate: ignore(drop-database)\nDROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            !violations.iter().any(|v| v.rule_id == "drop-database"),
            "ignore directive in line comment should suppress the rule"
        );
    }

    #[test]
    fn directives_in_sql_literals_do_not_suppress_rules() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "SELECT 'safe-migrate: ignore-file(drop-database)'; DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        assert!(
            violations.iter().any(|v| v.rule_id == "drop-database"),
            "directive-like text in a SQL literal must not suppress a rule"
        );
    }

    // ─────────────────────────────────────────────
    // Bug 15 — stmt_text strips leading comments
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug015_violation_sql_strips_leading_line_comments() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "-- preceding line comment\nDROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        let v = violations
            .iter()
            .find(|v| v.rule_id == "drop-database")
            .expect("Expected drop-database violation");

        let sql_text = v.sql.as_ref().expect("Expected sql field to be set");
        assert!(
            !sql_text.starts_with("--"),
            "Violation sql should not include leading comments. Got: {:?}",
            sql_text
        );
    }

    #[test]
    fn test_bug015_violation_sql_strips_leading_block_comments() {
        let engine = setup_engine();
        let mut state = setup_state();

        let sql = "/* preceding block comment */ DROP DATABASE my_db;";
        let violations = engine.analyze(sql, &mut state).unwrap();

        let v = violations
            .iter()
            .find(|v| v.rule_id == "drop-database")
            .expect("Expected drop-database violation");

        let sql_text = v.sql.as_ref().expect("Expected sql field to be set");
        assert!(
            !sql_text.starts_with("/*"),
            "Violation sql should not include leading block comments. Got: {:?}",
            sql_text
        );
    }

    // ─────────────────────────────────────────────
    // Bug 12 — Partition threshold integer division floor at 1
    // ─────────────────────────────────────────────
    #[test]
    fn test_bug012_partition_threshold_floor_at_one() {
        let config = safe_migrate::_internal::engine::config::Config {
            tier1_threshold_rows: 1,
            tier2_threshold_rows: 1,
            ..Default::default()
        };
        let engine = safe_migrate::_internal::engine::engine::SafeMigrateEngine::new(config);
        let mut cache = safe_migrate::_internal::db::cache::DbCache::new();

        let parent_id = object_id("public", "parent");
        let child_id = object_id("public", "child");
        let mut parent = safe_migrate::_internal::model::relation::RelationState::new(
            parent_id.clone(),
            object_id("public", "postgres"),
            0,
            Some(0),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        parent.partition_type = Some("RANGE".to_string());
        cache.insert_baseline(parent_id, parent);

        let mut child = safe_migrate::_internal::model::relation::RelationState::new(
            child_id.clone(),
            object_id("public", "postgres"),
            0,
            Some(0),
            safe_migrate::_internal::model::relation::RelationKind::Table,
            safe_migrate::_internal::model::relation::Persistence::Permanent,
            0,
        );
        child.partition_type = Some("RANGE".to_string());
        cache.insert_baseline(child_id, child);

        let mut state = safe_migrate::api::AnalysisState::new(cache);

        let violations = engine
            .analyze(
                "ALTER TABLE child ADD FOREIGN KEY (parent_id) REFERENCES parent(id);",
                &mut state,
            )
            .unwrap();

        let tier1_violations: Vec<&safe_migrate::_internal::report::violations::Violation> =
            violations
                .iter()
                .filter(|v| {
                    v.tier == safe_migrate::_internal::report::violations::ViolationTier::Tier1
                        && v.rule_id == "blocking-constraint"
                })
                .collect();

        assert!(
            tier1_violations.is_empty(),
            "Partitioned tables with 0 rows should not trigger Tier1 with threshold=1 (adjusted threshold should floor at 1, not 0). Found: {:?}",
            tier1_violations
        );
    }

    #[test]
    fn test_bug018_drop_schema_no_cascade_conflicts_when_table_exists() {
        // 1a: DROP SCHEMA without CASCADE must produce a chain-conflict violation
        // when the schema still contains tables.
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "CREATE SCHEMA myschema; CREATE TABLE myschema.t1(id int);",
                &mut state,
            )
            .unwrap();

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

        let conflict = violations.iter().find(|v| v.rule_id == "chain-conflict");
        assert!(
            conflict.is_some(),
            "Expected chain-conflict violation for DROP SCHEMA without CASCADE on non-empty schema, got: {:?}",
            violations
        );
        assert!(
            conflict.unwrap().reason.contains("CASCADE"),
            "Conflict reason should mention CASCADE, got: {}",
            conflict.unwrap().reason
        );
    }

    #[test]
    fn drop_schema_without_cascade_taints_when_emptiness_is_not_complete_evidence() {
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze("CREATE SCHEMA myschema;", &mut state)
            .unwrap();

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

        assert!(
            !violations.iter().any(|v| v.rule_id == "chain-conflict"),
            "an apparently empty schema is uncertain rather than a conflict: {violations:?}"
        );
        assert!(state.local.schemas.contains_key("myschema"));
        assert_eq!(state.local.confidence, Confidence::Tainted);
    }

    #[test]
    fn test_bug018_drop_schema_cascade_still_applied_with_table() {
        // 1a: DROP SCHEMA CASCADE must still succeed (no conflict) even with tables.
        let engine = setup_engine();
        let mut state = setup_state();

        engine
            .analyze(
                "CREATE SCHEMA myschema; CREATE TABLE myschema.t1(id int);",
                &mut state,
            )
            .unwrap();

        let violations = engine
            .analyze("DROP SCHEMA myschema CASCADE;", &mut state)
            .unwrap();

        let conflict = violations.iter().find(|v| v.rule_id == "chain-conflict");
        assert!(
            conflict.is_none(),
            "Expected no chain-conflict for DROP SCHEMA CASCADE, got: {:?}",
            violations
        );
    }
}
// ─────────────────────────────────────────────