switchy_database 0.3.0

Switchy database package
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
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
//! Database dependency management utilities for CASCADE and RESTRICT operations
//!
//! This module provides shared infrastructure for discovering and managing foreign key
//! dependencies between database tables. It supports both forward and backward dependency
//! tracking with cycle detection for safe CASCADE operations.

use crate::{DatabaseError, DatabaseTransaction};
use std::collections::{BTreeMap, BTreeSet};

/// Represents foreign key dependencies between tables
#[derive(Debug, Clone)]
pub struct DependencyGraph {
    /// Map from table name to set of tables that depend on it
    pub dependents: BTreeMap<String, BTreeSet<String>>,
    /// Map from table name to set of tables it depends on
    pub dependencies: BTreeMap<String, BTreeSet<String>>,
}

impl DependencyGraph {
    /// Creates a new empty dependency graph
    #[must_use]
    pub const fn new() -> Self {
        Self {
            dependents: BTreeMap::new(),
            dependencies: BTreeMap::new(),
        }
    }

    /// Adds a dependency relationship between two tables
    pub fn add_dependency(&mut self, dependent: String, depends_on: String) {
        self.dependents
            .entry(depends_on.clone())
            .or_default()
            .insert(dependent.clone());
        self.dependencies
            .entry(dependent)
            .or_default()
            .insert(depends_on);
    }

    /// Returns the set of tables that depend on the given table
    #[must_use]
    pub fn get_dependents(&self, table: &str) -> Option<&BTreeSet<String>> {
        self.dependents.get(table)
    }

    /// Returns the set of tables that the given table depends on
    #[must_use]
    pub fn get_dependencies(&self, table: &str) -> Option<&BTreeSet<String>> {
        self.dependencies.get(table)
    }

    /// Checks if any tables depend on the given table
    #[must_use]
    pub fn has_dependents(&self, table: &str) -> bool {
        self.dependents
            .get(table)
            .is_some_and(|deps| !deps.is_empty())
    }

    /// Performs topological sort with optional subset filtering
    ///
    /// * If `subset` is `Some`, only sort those tables and their dependencies
    /// * If `subset` is `None`, sort all tables in the graph
    /// * Returns tables in dependency order (roots first, leaves last)
    ///
    /// # Errors
    ///
    /// * Returns `CycleError` if circular dependencies are detected
    pub fn topological_sort(
        &self,
        subset: Option<&BTreeSet<String>>,
    ) -> Result<Vec<String>, CycleError> {
        let working_set = subset.map_or_else(
            || {
                let mut all_tables = BTreeSet::new();
                all_tables.extend(self.dependencies.keys().cloned());
                all_tables.extend(self.dependents.keys().cloned());
                all_tables
            },
            std::clone::Clone::clone,
        );

        let mut visited = BTreeSet::new();
        let mut visiting = BTreeSet::new();
        let mut result = Vec::new();

        for table in &working_set {
            if !visited.contains(table) {
                self.topological_visit(
                    table,
                    &working_set,
                    &mut visited,
                    &mut visiting,
                    &mut result,
                )?;
            }
        }

        Ok(result)
    }

    fn topological_visit(
        &self,
        table: &str,
        working_set: &BTreeSet<String>,
        visited: &mut BTreeSet<String>,
        visiting: &mut BTreeSet<String>,
        result: &mut Vec<String>,
    ) -> Result<(), CycleError> {
        if visiting.contains(table) {
            // Found a cycle
            let cycle_tables: Vec<String> = visiting.iter().cloned().collect();
            return Err(CycleError {
                tables: cycle_tables,
                message: format!("Circular dependency involving table '{table}'"),
            });
        }

        if visited.contains(table) {
            return Ok(());
        }

        visiting.insert(table.to_string());

        // Visit dependencies first (tables this table depends on)
        if let Some(deps) = self.dependencies.get(table) {
            for dep in deps {
                if working_set.contains(dep) {
                    self.topological_visit(dep, working_set, visited, visiting, result)?;
                }
            }
        }

        visiting.remove(table);
        visited.insert(table.to_string());
        result.push(table.to_string());

        Ok(())
    }

    /// Resolves the order for dropping a set of tables, handling cycles
    ///
    /// # Errors
    ///
    /// * Returns `DatabaseError` if internal operations fail (currently never fails)
    pub fn resolve_drop_order(
        &self,
        tables_to_drop: BTreeSet<String>,
    ) -> Result<DropPlan, DatabaseError> {
        match self.topological_sort(Some(&tables_to_drop)) {
            Ok(sorted) => Ok(DropPlan::Simple(sorted)),
            Err(CycleError {
                tables: _cycle_tables,
                message: _,
            }) => {
                // All tables in the set need to be dropped even with cycles
                Ok(DropPlan::WithCycles {
                    tables: tables_to_drop.into_iter().collect(),
                    requires_fk_disable: true,
                })
            }
        }
    }

    /// Collects all tables that depend on the given table (recursively)
    #[must_use]
    pub fn collect_all_dependents(&self, table: &str) -> BTreeSet<String> {
        let mut collected = BTreeSet::new();
        self.collect_dependents_recursive(table, &mut collected);
        collected
    }

    fn collect_dependents_recursive(&self, table: &str, collected: &mut BTreeSet<String>) {
        // Add the table itself
        collected.insert(table.to_string());

        // Recursively add all dependent tables
        if let Some(dependents) = self.get_dependents(table) {
            for dependent in dependents {
                if !collected.contains(dependent) {
                    self.collect_dependents_recursive(dependent, collected);
                }
            }
        }
    }

    /// Checks if a table exists in the dependency graph
    #[must_use]
    pub fn table_exists(&self, table: &str) -> bool {
        self.dependencies.contains_key(table) || self.dependents.contains_key(table)
    }
}

impl Default for DependencyGraph {
    fn default() -> Self {
        Self::new()
    }
}

/// Error when circular dependencies are detected
#[derive(Debug)]
pub struct CycleError {
    /// Tables involved in the circular dependency
    pub tables: Vec<String>,
    /// Human-readable description of the cycle
    pub message: String,
}

impl std::fmt::Display for CycleError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Circular dependency detected: {} (tables: {:?})",
            self.message, self.tables
        )
    }
}

impl std::error::Error for CycleError {}

/// Represents a plan for dropping tables with dependency handling
#[derive(Debug, Clone)]
pub enum DropPlan {
    /// Simple drop order with no cycles
    Simple(Vec<String>),
    /// Tables with circular dependencies requiring FK constraint disable
    WithCycles {
        tables: Vec<String>,
        requires_fk_disable: bool,
    },
}

/// Discover all foreign key dependencies for `SQLite`
///
/// Uses the generic introspection API to discover foreign key relationships between tables.
/// This implementation leverages the `list_tables()` method and `get_table_info()` to build
/// a complete dependency graph without requiring backend-specific PRAGMA commands.
///
/// # Errors
///
/// * Returns `DatabaseError` if database queries fail
pub async fn discover_dependencies_sqlite(
    tx: &dyn DatabaseTransaction,
) -> Result<DependencyGraph, DatabaseError> {
    let mut graph = DependencyGraph::new();

    // Use new list_tables() method
    let tables = tx.list_tables().await?;

    // Use existing get_table_info() for foreign keys
    for table_name in tables {
        if let Some(table_info) = tx.get_table_info(&table_name).await? {
            // Iterate over foreign_keys BTreeMap, ignoring the constraint name
            for (_fk_name, fk) in table_info.foreign_keys {
                graph.add_dependency(table_name.clone(), fk.referenced_table);
            }
        }
    }

    Ok(graph)
}

/// Get tables that depend on the given table
///
/// # Errors
///
/// * Returns `DatabaseError` if dependency discovery fails
pub async fn get_table_dependencies_sqlite(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
) -> Result<BTreeSet<String>, DatabaseError> {
    let graph = discover_dependencies_sqlite(tx).await?;
    Ok(graph
        .get_dependents(table_name)
        .cloned()
        .unwrap_or_default())
}

// Helper function for recursive cascade traversal with cycle detection
fn visit_cascade_recursive(
    current: &str,
    all_deps: &BTreeMap<String, BTreeSet<String>>,
    visited: &mut BTreeSet<String>,
    visiting: &mut BTreeSet<String>,
    to_drop: &mut Vec<String>,
    has_cycle: &mut bool,
) {
    if visiting.contains(current) {
        *has_cycle = true;
        return; // Cycle detected
    }
    if !visited.insert(current.to_string()) {
        return; // Already processed
    }

    visiting.insert(current.to_string());

    // Visit all dependents first
    if let Some(dependents) = all_deps.get(current) {
        for dependent in dependents {
            visit_cascade_recursive(dependent, all_deps, visited, visiting, to_drop, has_cycle);
        }
    }

    visiting.remove(current);
    to_drop.push(current.to_string());
}

/// Find all tables that would be affected by CASCADE deletion of the specified table
///
/// Returns a `DropPlan` which handles both simple and circular dependencies.
/// For simple cases: `DropPlan::Simple(Vec<String>)` with dependents first.
/// For cycles: `DropPlan::WithCycles` indicating FK constraints must be disabled.
///
/// # Performance
///
/// Time: O(d * f) where d = dependent tables, f = foreign keys per table
/// Space: O(d) for visited set and results
/// Note: Optimized for targeted discovery instead of analyzing all tables
///
/// # Errors
///
/// * Returns `DatabaseError` if dependency discovery fails
pub async fn find_cascade_targets(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
) -> Result<DropPlan, DatabaseError> {
    let mut to_drop = Vec::new();
    let mut visited = BTreeSet::new();
    let mut has_cycle = false;

    // First, build a map of all dependencies we need to consider
    let mut all_deps = BTreeMap::new();
    let mut to_check = vec![table_name.to_string()];
    let mut discovered = BTreeSet::new();

    // Discover all tables involved in the cascade
    while let Some(current) = to_check.pop() {
        if !discovered.insert(current.clone()) {
            continue;
        }

        let dependents = get_direct_dependents(tx, &current).await?;
        all_deps.insert(current.clone(), dependents.clone());

        for dep in dependents {
            to_check.push(dep);
        }
    }

    // Now traverse using proper cycle detection
    let mut visiting = BTreeSet::new();
    visit_cascade_recursive(
        table_name,
        &all_deps,
        &mut visited,
        &mut visiting,
        &mut to_drop,
        &mut has_cycle,
    );

    // The recursive function already puts them in the right order:
    // - Visits dependents first, then the current table
    // - So dependents are added to to_drop before their dependencies
    // No need to reverse!

    if has_cycle {
        Ok(DropPlan::WithCycles {
            tables: to_drop,
            requires_fk_disable: true,
        })
    } else {
        Ok(DropPlan::Simple(to_drop))
    }
}

/// Check if a table has any dependents (for RESTRICT validation)
/// Returns immediately upon finding first dependent for efficiency
///
/// # Performance
///
/// Best case: O(1) - stops at first dependent found
/// Worst case: O(n) - only when table has no dependents
///
/// # Errors
///
/// * Returns `DatabaseError` if introspection fails
pub async fn has_any_dependents(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
) -> Result<bool, DatabaseError> {
    let all_tables = tx.list_tables().await?;

    for table in all_tables {
        if table == table_name {
            continue;
        }

        if let Some(info) = tx.get_table_info(&table).await? {
            for fk in info.foreign_keys.values() {
                if fk.referenced_table == table_name {
                    return Ok(true); // EARLY TERMINATION - key optimization
                }
            }
        }
    }

    Ok(false)
}

/// Get direct dependents of a table (one level only, no recursion)
///
/// # Errors
///
/// * Returns `DatabaseError` if table introspection fails
pub async fn get_direct_dependents(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
) -> Result<BTreeSet<String>, DatabaseError> {
    let mut dependents = BTreeSet::new();

    // We must use list_tables() as it's the only way to discover tables
    // with existing Database methods, but we optimize by only calling
    // get_table_info() on each table once and only as needed
    let all_tables = tx.list_tables().await?;

    for table in all_tables {
        if table == table_name {
            continue; // Skip self-references
        }

        // Get info for this specific table (not all upfront)
        if let Some(info) = tx.get_table_info(&table).await? {
            // Check if this table references our target
            for fk in info.foreign_keys.values() {
                if fk.referenced_table == table_name {
                    dependents.insert(table.clone());
                    break; // Found dependency, move to next table
                }
            }
        }
    }

    Ok(dependents)
}

/// Recursively find all tables that depend on the specified table
/// More efficient than building full graph when only one table's dependents are needed
///
/// # Errors
///
/// * Returns `DatabaseError` if dependency discovery fails
pub async fn get_all_dependents_recursive(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
) -> Result<BTreeSet<String>, DatabaseError> {
    let mut all_dependents = BTreeSet::new();
    let mut to_check = vec![table_name.to_string()];
    let mut visited = BTreeSet::new();

    while let Some(current_table) = to_check.pop() {
        if !visited.insert(current_table.clone()) {
            continue; // Already processed
        }

        // Reuse get_direct_dependents for consistency
        let direct_deps = get_direct_dependents(tx, &current_table).await?;

        for dep in direct_deps {
            if all_dependents.insert(dep.clone()) {
                to_check.push(dep); // Queue for recursive checking
            }
        }
    }

    Ok(all_dependents)
}

/// For `SQLite` `PRAGMA`: Table names cannot be parameterized
/// Basic validation for PRAGMA syntax - NOT comprehensive security
///
/// # Errors
///
/// * Returns `DatabaseError::InvalidQuery` if table name contains unsafe characters
pub fn validate_table_name_for_pragma(name: &str) -> Result<(), DatabaseError> {
    // Only allow safe characters for PRAGMA usage
    if name.chars().all(|c| c.is_alphanumeric() || c == '_') && !name.is_empty() {
        Ok(())
    } else {
        Err(DatabaseError::InvalidQuery(format!(
            "Table name contains unsafe characters for PRAGMA: {name}"
        )))
    }
}

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

    // Mock Transaction for testing the new async functions
    #[cfg(feature = "simulator")]
    mod async_tests {
        use super::*;
        use crate::Database;
        use crate::simulator::SimulationDatabase;

        async fn create_test_database_with_dependencies() -> SimulationDatabase {
            let db = SimulationDatabase::new().unwrap();

            // Create tables with foreign key dependencies
            // users (root)
            db.exec_raw(
                "CREATE TABLE users (
                    id INTEGER PRIMARY KEY,
                    name TEXT NOT NULL
                )",
            )
            .await
            .unwrap();

            // posts (depends on users)
            db.exec_raw(
                "CREATE TABLE posts (
                    id INTEGER PRIMARY KEY,
                    title TEXT NOT NULL,
                    user_id INTEGER,
                    FOREIGN KEY (user_id) REFERENCES users(id)
                )",
            )
            .await
            .unwrap();

            // comments (depends on posts)
            db.exec_raw(
                "CREATE TABLE comments (
                    id INTEGER PRIMARY KEY,
                    content TEXT NOT NULL,
                    post_id INTEGER,
                    FOREIGN KEY (post_id) REFERENCES posts(id)
                )",
            )
            .await
            .unwrap();

            // tags (independent table)
            db.exec_raw(
                "CREATE TABLE tags (
                    id INTEGER PRIMARY KEY,
                    name TEXT NOT NULL
                )",
            )
            .await
            .unwrap();

            // post_tags (depends on both posts and tags)
            db.exec_raw(
                "CREATE TABLE post_tags (
                    post_id INTEGER,
                    tag_id INTEGER,
                    PRIMARY KEY (post_id, tag_id),
                    FOREIGN KEY (post_id) REFERENCES posts(id),
                    FOREIGN KEY (tag_id) REFERENCES tags(id)
                )",
            )
            .await
            .unwrap();

            db
        }

        async fn create_test_database_with_cycles() -> SimulationDatabase {
            let db = SimulationDatabase::new().unwrap();

            // Create a three-table cycle: A -> B -> C -> A
            // This avoids SQLite's limitations with direct circular references

            db.exec_raw(
                "CREATE TABLE cycle_a (
                    id INTEGER PRIMARY KEY,
                    b_ref INTEGER,
                    FOREIGN KEY (b_ref) REFERENCES cycle_b(id)
                )",
            )
            .await
            .unwrap();

            db.exec_raw(
                "CREATE TABLE cycle_b (
                    id INTEGER PRIMARY KEY,
                    c_ref INTEGER,
                    FOREIGN KEY (c_ref) REFERENCES cycle_c(id)
                )",
            )
            .await
            .unwrap();

            db.exec_raw(
                "CREATE TABLE cycle_c (
                    id INTEGER PRIMARY KEY,
                    a_ref INTEGER,
                    FOREIGN KEY (a_ref) REFERENCES cycle_a(id)
                )",
            )
            .await
            .unwrap();

            db
        }

        #[switchy_async::test]
        async fn test_get_direct_dependents_basic() {
            let db = create_test_database_with_dependencies().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test that users has posts as a dependent
            let dependents = get_direct_dependents(&*tx, "users").await.unwrap();
            assert_eq!(dependents.len(), 1);
            assert!(dependents.contains("posts"));

            // Test that posts has comments and post_tags as dependents
            let dependents = get_direct_dependents(&*tx, "posts").await.unwrap();
            assert_eq!(dependents.len(), 2);
            assert!(dependents.contains("comments"));
            assert!(dependents.contains("post_tags"));

            // Test that tags has post_tags as a dependent
            let dependents = get_direct_dependents(&*tx, "tags").await.unwrap();
            assert_eq!(dependents.len(), 1);
            assert!(dependents.contains("post_tags"));

            // Test that comments has no dependents
            let dependents = get_direct_dependents(&*tx, "comments").await.unwrap();
            assert!(dependents.is_empty());

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_get_direct_dependents_non_existent_table() {
            let db = create_test_database_with_dependencies().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test non-existent table
            let dependents = get_direct_dependents(&*tx, "non_existent").await.unwrap();
            assert!(dependents.is_empty());

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_has_any_dependents_early_termination() {
            let db = create_test_database_with_dependencies().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test tables with dependents (should return true)
            assert!(has_any_dependents(&*tx, "users").await.unwrap());
            assert!(has_any_dependents(&*tx, "posts").await.unwrap());
            assert!(has_any_dependents(&*tx, "tags").await.unwrap());

            // Test table without dependents
            assert!(!has_any_dependents(&*tx, "comments").await.unwrap());
            assert!(!has_any_dependents(&*tx, "post_tags").await.unwrap());

            // Test non-existent table
            assert!(!has_any_dependents(&*tx, "non_existent").await.unwrap());

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_get_all_dependents_recursive() {
            let db = create_test_database_with_dependencies().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test users (root) - should find all its recursive dependents
            let all_dependents = get_all_dependents_recursive(&*tx, "users").await.unwrap();
            assert_eq!(all_dependents.len(), 3); // posts, comments, post_tags
            assert!(all_dependents.contains("posts"));
            assert!(all_dependents.contains("comments"));
            assert!(all_dependents.contains("post_tags"));

            // Test posts - should find its dependents
            let all_dependents = get_all_dependents_recursive(&*tx, "posts").await.unwrap();
            assert_eq!(all_dependents.len(), 2); // comments, post_tags
            assert!(all_dependents.contains("comments"));
            assert!(all_dependents.contains("post_tags"));

            // Test tags - should find only post_tags
            let all_dependents = get_all_dependents_recursive(&*tx, "tags").await.unwrap();
            assert_eq!(all_dependents.len(), 1);
            assert!(all_dependents.contains("post_tags"));

            // Test leaf table - should have no dependents
            let all_dependents = get_all_dependents_recursive(&*tx, "comments")
                .await
                .unwrap();
            assert!(all_dependents.is_empty());

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_find_cascade_targets_simple_case() {
            let db = create_test_database_with_dependencies().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test CASCADE targets for users (should include all dependents)
            let drop_plan = find_cascade_targets(&*tx, "users").await.unwrap();
            match drop_plan {
                DropPlan::Simple(tables) => {
                    assert_eq!(tables.len(), 4); // users + 3 dependents
                    // Should include all tables in proper order (dependents first)
                    assert!(tables.contains(&"users".to_string()));
                    assert!(tables.contains(&"posts".to_string()));
                    assert!(tables.contains(&"comments".to_string()));
                    assert!(tables.contains(&"post_tags".to_string()));

                    // Verify order: dependents should come before dependencies
                    let users_pos = tables.iter().position(|t| t == "users").unwrap();
                    let posts_pos = tables.iter().position(|t| t == "posts").unwrap();
                    assert!(posts_pos < users_pos, "posts should come before users");
                }
                DropPlan::WithCycles { .. } => {
                    panic!("Expected Simple drop plan, got WithCycles");
                }
            }

            // Test CASCADE targets for posts
            let drop_plan = find_cascade_targets(&*tx, "posts").await.unwrap();
            match drop_plan {
                DropPlan::Simple(tables) => {
                    assert_eq!(tables.len(), 3); // posts + 2 dependents
                    assert!(tables.contains(&"posts".to_string()));
                    assert!(tables.contains(&"comments".to_string()));
                    assert!(tables.contains(&"post_tags".to_string()));
                }
                DropPlan::WithCycles { .. } => {
                    panic!("Expected Simple drop plan, got WithCycles");
                }
            }

            // Test leaf table (should only include itself)
            let drop_plan = find_cascade_targets(&*tx, "comments").await.unwrap();
            match drop_plan {
                DropPlan::Simple(tables) => {
                    assert_eq!(tables.len(), 1);
                    assert_eq!(tables[0], "comments");
                }
                DropPlan::WithCycles { .. } => {
                    panic!("Expected Simple drop plan, got WithCycles");
                }
            }

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_find_cascade_targets_with_cycles() {
            let db = create_test_database_with_cycles().await;
            let tx = db.begin_transaction().await.unwrap();

            // Test CASCADE targets with circular dependencies (A -> B -> C -> A)
            let drop_plan = find_cascade_targets(&*tx, "cycle_a").await.unwrap();
            match drop_plan {
                DropPlan::WithCycles {
                    tables,
                    requires_fk_disable,
                } => {
                    assert!(requires_fk_disable);
                    assert_eq!(tables.len(), 3);
                    assert!(tables.contains(&"cycle_a".to_string()));
                    assert!(tables.contains(&"cycle_b".to_string()));
                    assert!(tables.contains(&"cycle_c".to_string()));
                }
                DropPlan::Simple(_) => {
                    panic!("Expected WithCycles drop plan, got Simple");
                }
            }

            // Test from cycle_b as well
            let drop_plan = find_cascade_targets(&*tx, "cycle_b").await.unwrap();
            match drop_plan {
                DropPlan::WithCycles {
                    tables,
                    requires_fk_disable,
                } => {
                    assert!(requires_fk_disable);
                    assert_eq!(tables.len(), 3);
                    assert!(tables.contains(&"cycle_a".to_string()));
                    assert!(tables.contains(&"cycle_b".to_string()));
                    assert!(tables.contains(&"cycle_c".to_string()));
                }
                DropPlan::Simple(_) => {
                    panic!("Expected WithCycles drop plan, got Simple");
                }
            }

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_edge_case_self_references() {
            let db = SimulationDatabase::new().unwrap();

            // Create table with self-reference
            db.exec_raw(
                "CREATE TABLE self_ref (
                    id INTEGER PRIMARY KEY,
                    parent_id INTEGER,
                    name TEXT,
                    FOREIGN KEY (parent_id) REFERENCES self_ref(id)
                )",
            )
            .await
            .unwrap();

            let tx = db.begin_transaction().await.unwrap();

            // Test get_direct_dependents with self-reference
            let dependents = get_direct_dependents(&*tx, "self_ref").await.unwrap();

            // SQLite should detect the self-reference if foreign keys are properly introspected
            if dependents.contains("self_ref") {
                assert_eq!(dependents.len(), 1);

                // Test has_any_dependents (should return true due to self-reference)
                assert!(has_any_dependents(&*tx, "self_ref").await.unwrap());

                // Test find_cascade_targets (should detect cycle due to self-reference)
                let drop_plan = find_cascade_targets(&*tx, "self_ref").await.unwrap();
                match drop_plan {
                    DropPlan::WithCycles {
                        tables,
                        requires_fk_disable,
                    } => {
                        assert!(requires_fk_disable);
                        assert_eq!(tables.len(), 1);
                        assert!(tables.contains(&"self_ref".to_string()));
                    }
                    DropPlan::Simple(tables) => {
                        // If cycle detection didn't work, at least verify the table is included
                        assert_eq!(tables.len(), 1);
                        assert!(tables.contains(&"self_ref".to_string()));
                    }
                }
            } else {
                // If SQLite doesn't report self-reference, that's also valid behavior
                // Just test that the table doesn't crash the algorithm
                assert!(dependents.is_empty());
                assert!(!has_any_dependents(&*tx, "self_ref").await.unwrap());

                let drop_plan = find_cascade_targets(&*tx, "self_ref").await.unwrap();
                match drop_plan {
                    DropPlan::Simple(tables) => {
                        assert_eq!(tables.len(), 1);
                        assert!(tables.contains(&"self_ref".to_string()));
                    }
                    DropPlan::WithCycles { .. } => {
                        // This is also acceptable if detected
                    }
                }
            }

            tx.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_transaction_rollback_with_failed_operations() {
            let db = create_test_database_with_dependencies().await;

            // Begin transaction
            let tx = db.begin_transaction().await.unwrap();

            // Perform some dependency discovery operations
            let dependents = get_direct_dependents(&*tx, "users").await.unwrap();
            assert!(!dependents.is_empty());

            // Test that operations work within transaction context
            let has_deps = has_any_dependents(&*tx, "posts").await.unwrap();
            assert!(has_deps);

            // Rollback transaction
            tx.rollback().await.unwrap();

            // Create a new transaction and verify database state is unchanged
            let tx2 = db.begin_transaction().await.unwrap();
            let dependents_after = get_direct_dependents(&*tx2, "users").await.unwrap();
            assert_eq!(dependents, dependents_after); // Should be the same

            tx2.commit().await.unwrap();
        }

        #[switchy_async::test]
        async fn test_complex_dependency_chains() {
            let db = SimulationDatabase::new().unwrap();

            // Create complex dependency chain: A -> B -> C -> D
            db.exec_raw("CREATE TABLE table_d (id INTEGER PRIMARY KEY, name TEXT)")
                .await
                .unwrap();

            db.exec_raw(
                "CREATE TABLE table_c (
                    id INTEGER PRIMARY KEY, 
                    d_id INTEGER,
                    FOREIGN KEY (d_id) REFERENCES table_d(id)
                )",
            )
            .await
            .unwrap();

            db.exec_raw(
                "CREATE TABLE table_b (
                    id INTEGER PRIMARY KEY, 
                    c_id INTEGER,
                    FOREIGN KEY (c_id) REFERENCES table_c(id)
                )",
            )
            .await
            .unwrap();

            db.exec_raw(
                "CREATE TABLE table_a (
                    id INTEGER PRIMARY KEY, 
                    b_id INTEGER,
                    FOREIGN KEY (b_id) REFERENCES table_b(id)
                )",
            )
            .await
            .unwrap();

            let tx = db.begin_transaction().await.unwrap();

            // Test cascade from root (table_d)
            let drop_plan = find_cascade_targets(&*tx, "table_d").await.unwrap();
            match drop_plan {
                DropPlan::Simple(tables) => {
                    assert_eq!(tables.len(), 4);
                    // Verify proper ordering: dependents first
                    let d_pos = tables.iter().position(|t| t == "table_d").unwrap();
                    let c_pos = tables.iter().position(|t| t == "table_c").unwrap();
                    let b_pos = tables.iter().position(|t| t == "table_b").unwrap();
                    let a_pos = tables.iter().position(|t| t == "table_a").unwrap();

                    assert!(a_pos < b_pos);
                    assert!(b_pos < c_pos);
                    assert!(c_pos < d_pos);
                }
                DropPlan::WithCycles { .. } => {
                    panic!("Expected Simple drop plan, got WithCycles");
                }
            }

            // Test recursive dependents
            let all_deps = get_all_dependents_recursive(&*tx, "table_d").await.unwrap();
            assert_eq!(all_deps.len(), 3); // c, b, a
            assert!(all_deps.contains("table_c"));
            assert!(all_deps.contains("table_b"));
            assert!(all_deps.contains("table_a"));

            tx.commit().await.unwrap();
        }
    }

    #[test]
    fn test_new_graph_is_empty() {
        let graph = DependencyGraph::new();
        assert!(graph.dependencies.is_empty());
    }

    #[test]
    fn test_add_single_dependency() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());

        // Posts depends on users
        assert!(graph.dependencies.contains_key("posts"));
        assert!(graph.dependencies["posts"].contains("users"));

        // Users has dependents (posts depends on it)
        assert!(graph.dependents.contains_key("users"));
        assert!(graph.dependents["users"].contains("posts"));

        // Users has no dependencies (no entry created since it doesn't depend on anything)
        assert!(!graph.dependencies.contains_key("users"));
    }

    #[test]
    fn test_add_multiple_dependencies() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());
        graph.add_dependency("comments".to_string(), "users".to_string());

        assert_eq!(graph.dependencies["comments"].len(), 2);
        assert!(graph.dependencies["comments"].contains("posts"));
        assert!(graph.dependencies["comments"].contains("users"));
    }

    #[test]
    fn test_topological_sort_linear_chain() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());

        // Test sorting all tables - returns roots first, leaves last
        let result = graph.topological_sort(None).unwrap();
        assert_eq!(result, vec!["users", "posts", "comments"]);

        // Test sorting with subset that includes only one table
        let subset = BTreeSet::from(["comments".to_string()]);
        let result = graph.topological_sort(Some(&subset)).unwrap();
        assert_eq!(result, vec!["comments"]);
    }

    #[test]
    fn test_topological_sort_diamond_dependency() {
        let mut graph = DependencyGraph::new();
        // Diamond: D depends on B and C, both depend on A
        graph.add_dependency("D".to_string(), "B".to_string());
        graph.add_dependency("D".to_string(), "C".to_string());
        graph.add_dependency("B".to_string(), "A".to_string());
        graph.add_dependency("C".to_string(), "A".to_string());

        // Test sorting all tables - roots first (A), leaves last (D)
        let result = graph.topological_sort(None).unwrap();
        assert_eq!(result.len(), 4);
        assert_eq!(result[0], "A"); // Root comes first
        assert_eq!(result[3], "D"); // Leaf comes last
        // B and C can be in either order at positions 1 and 2
        assert!(result.contains(&"B".to_string()));
        assert!(result.contains(&"C".to_string()));
    }

    #[test]
    fn test_topological_sort_detects_simple_cycle() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("A".to_string(), "B".to_string());
        graph.add_dependency("B".to_string(), "A".to_string());

        let result = graph.topological_sort(None);
        assert!(matches!(result, Err(CycleError { .. })));

        if let Err(CycleError { tables, .. }) = result {
            assert!(tables.contains(&"A".to_string()));
            assert!(tables.contains(&"B".to_string()));
        }
    }

    #[test]
    fn test_topological_sort_detects_complex_cycle() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("A".to_string(), "B".to_string());
        graph.add_dependency("B".to_string(), "C".to_string());
        graph.add_dependency("C".to_string(), "D".to_string());
        graph.add_dependency("D".to_string(), "B".to_string()); // Creates cycle B->C->D->B

        let result = graph.topological_sort(None);
        assert!(matches!(result, Err(CycleError { .. })));
    }

    #[test]
    fn test_topological_sort_independent_table() {
        let mut graph = DependencyGraph::new();

        // Add a table with no dependencies
        graph.add_dependency("independent".to_string(), String::new());
        graph.dependencies.get_mut("independent").unwrap().clear(); // Remove the empty dependency

        let subset = BTreeSet::from(["independent".to_string()]);
        let result = graph.topological_sort(Some(&subset)).unwrap();
        assert_eq!(result, vec!["independent"]);
    }

    #[test]
    fn test_get_dependencies_direct() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());

        let deps = graph.get_dependencies("posts").unwrap();
        assert_eq!(*deps, BTreeSet::from(["users".to_string()]));
    }

    #[test]
    fn test_get_dependencies_transitive() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("comments".to_string(), "posts".to_string());
        graph.add_dependency("posts".to_string(), "users".to_string());

        // Direct dependencies only
        let deps = graph.get_dependencies("comments").unwrap();
        assert_eq!(*deps, BTreeSet::from(["posts".to_string()]));

        // For transitive dependencies, use topological sort on all tables (roots first)
        let sorted = graph.topological_sort(None).unwrap();
        assert_eq!(sorted, vec!["users", "posts", "comments"]);
    }

    #[test]
    fn test_get_dependencies_empty() {
        let graph = DependencyGraph::new();
        assert!(graph.get_dependencies("nonexistent").is_none());

        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());

        // Users has no dependencies
        let deps = graph.get_dependencies("users");
        assert!(deps.is_none() || deps.unwrap().is_empty());
    }

    #[test]
    fn test_get_dependents() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "users".to_string());

        let dependents = graph.get_dependents("users").unwrap();
        assert_eq!(dependents.len(), 2);
        assert!(dependents.contains("posts"));
        assert!(dependents.contains("comments"));

        // Table with no dependents
        assert!(
            graph.get_dependents("posts").is_none()
                || graph.get_dependents("posts").unwrap().is_empty()
        );
    }

    #[test]
    fn test_has_dependents() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());

        assert!(graph.has_dependents("users"));
        assert!(!graph.has_dependents("posts"));
        assert!(!graph.has_dependents("nonexistent"));
    }

    #[test_log::test]
    fn test_collect_all_dependents_simple_chain() {
        let mut graph = DependencyGraph::new();
        // Chain: users <- posts <- comments
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());

        // Starting from users, should collect posts and comments
        let all_deps = graph.collect_all_dependents("users");
        assert_eq!(
            all_deps,
            BTreeSet::from([
                "users".to_string(),
                "posts".to_string(),
                "comments".to_string()
            ])
        );
    }

    #[test_log::test]
    fn test_collect_all_dependents_diamond_pattern() {
        let mut graph = DependencyGraph::new();
        // Diamond: users <- posts <- post_tags, users <- comments <- post_tags
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "users".to_string());
        graph.add_dependency("post_tags".to_string(), "posts".to_string());
        graph.add_dependency("post_tags".to_string(), "comments".to_string());

        // Starting from users, should collect all tables
        let all_deps = graph.collect_all_dependents("users");
        assert_eq!(
            all_deps,
            BTreeSet::from([
                "users".to_string(),
                "posts".to_string(),
                "comments".to_string(),
                "post_tags".to_string()
            ])
        );
    }

    #[test_log::test]
    fn test_collect_all_dependents_leaf_table() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());

        // Starting from leaf table (comments) - should only include itself
        let all_deps = graph.collect_all_dependents("comments");
        assert_eq!(all_deps, BTreeSet::from(["comments".to_string()]));
    }

    #[test_log::test]
    fn test_collect_all_dependents_nonexistent_table() {
        let graph = DependencyGraph::new();

        // Nonexistent table - should still include itself
        let all_deps = graph.collect_all_dependents("nonexistent");
        assert_eq!(all_deps, BTreeSet::from(["nonexistent".to_string()]));
    }

    #[test_log::test]
    fn test_table_exists_in_graph() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());

        // Table with dependents
        assert!(graph.table_exists("users"));

        // Table with both dependencies and dependents
        assert!(graph.table_exists("posts"));

        // Table with only dependencies
        assert!(graph.table_exists("comments"));

        // Nonexistent table
        assert!(!graph.table_exists("nonexistent"));
    }

    #[test_log::test]
    fn test_table_exists_only_in_dependents() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());

        // users only appears in dependents, not in dependencies
        assert!(graph.table_exists("users"));
    }

    #[test_log::test]
    fn test_resolve_drop_order_simple() {
        let mut graph = DependencyGraph::new();
        graph.add_dependency("posts".to_string(), "users".to_string());
        graph.add_dependency("comments".to_string(), "posts".to_string());

        let tables_to_drop = BTreeSet::from([
            "users".to_string(),
            "posts".to_string(),
            "comments".to_string(),
        ]);

        let result = graph.resolve_drop_order(tables_to_drop).unwrap();
        match result {
            DropPlan::Simple(order) => {
                assert_eq!(order.len(), 3);
                // Verify proper ordering: users should come first (root), comments last
                assert_eq!(order[0], "users");
                assert_eq!(order[2], "comments");
            }
            DropPlan::WithCycles { .. } => panic!("Expected Simple plan, got WithCycles"),
        }
    }

    #[test_log::test]
    fn test_resolve_drop_order_with_cycle() {
        let mut graph = DependencyGraph::new();
        // Create a cycle: A -> B -> A
        graph.add_dependency("A".to_string(), "B".to_string());
        graph.add_dependency("B".to_string(), "A".to_string());

        let tables_to_drop = BTreeSet::from(["A".to_string(), "B".to_string()]);

        let result = graph.resolve_drop_order(tables_to_drop).unwrap();
        match result {
            DropPlan::WithCycles {
                tables,
                requires_fk_disable,
            } => {
                assert!(requires_fk_disable);
                assert_eq!(tables.len(), 2);
                assert!(tables.contains(&"A".to_string()));
                assert!(tables.contains(&"B".to_string()));
            }
            DropPlan::Simple(_) => panic!("Expected WithCycles plan, got Simple"),
        }
    }

    #[test_log::test]
    fn test_validate_table_name_for_pragma_valid_names() {
        assert!(validate_table_name_for_pragma("users").is_ok());
        assert!(validate_table_name_for_pragma("my_table").is_ok());
        assert!(validate_table_name_for_pragma("Table123").is_ok());
        assert!(validate_table_name_for_pragma("_private").is_ok());
        assert!(validate_table_name_for_pragma("a").is_ok());
    }

    #[test_log::test]
    fn test_validate_table_name_for_pragma_empty_rejected() {
        let result = validate_table_name_for_pragma("");
        assert!(result.is_err());
        match result {
            Err(crate::DatabaseError::InvalidQuery(msg)) => {
                assert!(msg.contains("unsafe characters"));
            }
            _ => panic!("Expected InvalidQuery error"),
        }
    }

    #[test_log::test]
    fn test_validate_table_name_for_pragma_special_chars_rejected() {
        // SQL injection attempts
        assert!(validate_table_name_for_pragma("users; DROP TABLE users").is_err());
        assert!(validate_table_name_for_pragma("users--").is_err());
        assert!(validate_table_name_for_pragma("users'").is_err());
        assert!(validate_table_name_for_pragma("users\"").is_err());

        // Other invalid characters
        assert!(validate_table_name_for_pragma("table-name").is_err());
        assert!(validate_table_name_for_pragma("table.name").is_err());
        assert!(validate_table_name_for_pragma("table name").is_err());
        assert!(validate_table_name_for_pragma("table*").is_err());
    }

    #[test]
    fn test_column_dependencies_struct() {
        let deps = ColumnDependencies {
            indexes: vec!["idx_email".to_string(), "idx_composite".to_string()],
            foreign_keys: vec!["fk_user_email".to_string()],
        };

        assert_eq!(deps.indexes.len(), 2);
        assert_eq!(deps.foreign_keys.len(), 1);
        assert!(deps.indexes.contains(&"idx_email".to_string()));
        assert!(deps.foreign_keys.contains(&"fk_user_email".to_string()));
    }

    #[cfg(feature = "simulator")]
    #[switchy_async::test]
    async fn test_get_column_dependencies_empty_table() {
        use crate::Database;
        use crate::simulator::SimulationDatabase;

        let db = SimulationDatabase::new().unwrap();

        // Create a simple table without indexes or foreign keys
        db.exec_raw(
            "CREATE TABLE simple_table (
                id INTEGER PRIMARY KEY,
                name TEXT,
                email TEXT
            )",
        )
        .await
        .unwrap();

        let tx = db.begin_transaction().await.unwrap();

        // Test column that exists but has no dependencies
        let deps = super::get_column_dependencies(&*tx, "simple_table", "email")
            .await
            .unwrap();
        assert!(deps.indexes.is_empty());
        assert!(deps.foreign_keys.is_empty());

        // Test nonexistent column
        let deps = super::get_column_dependencies(&*tx, "simple_table", "nonexistent")
            .await
            .unwrap();
        assert!(deps.indexes.is_empty());
        assert!(deps.foreign_keys.is_empty());

        // Test nonexistent table
        let deps = super::get_column_dependencies(&*tx, "nonexistent_table", "email")
            .await
            .unwrap();
        assert!(deps.indexes.is_empty());
        assert!(deps.foreign_keys.is_empty());

        tx.commit().await.unwrap();
    }
}

/// Column-level dependency information
#[derive(Debug, Clone)]
pub struct ColumnDependencies {
    /// Indexes that use this column
    pub indexes: Vec<String>,
    /// Foreign key constraints that reference this column
    pub foreign_keys: Vec<String>,
}

/// Get column dependencies using existing table introspection infrastructure
///
/// # Errors
///
/// * Returns `DatabaseError` if table introspection fails
pub async fn get_column_dependencies(
    tx: &dyn DatabaseTransaction,
    table_name: &str,
    column_name: &str,
) -> Result<ColumnDependencies, DatabaseError> {
    let mut deps = ColumnDependencies {
        indexes: Vec::new(),
        foreign_keys: Vec::new(),
    };

    // Get table info using existing introspection
    if let Some(table_info) = tx.get_table_info(table_name).await? {
        // Find indexes that contain this column
        for (index_name, index_info) in table_info.indexes {
            if index_info.columns.contains(&column_name.to_string()) {
                deps.indexes.push(index_name);
            }
        }

        // Find foreign keys that reference this column
        for (fk_name, fk_info) in table_info.foreign_keys {
            if fk_info.column == column_name {
                deps.foreign_keys.push(fk_name);
            }
        }
    }

    Ok(deps)
}