magellan 3.3.1

Deterministic codebase mapping tool for local development
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
//! Read-only sqlitegraph database compatibility preflight.
//!
//! Phase 1 goal: refuse incompatible existing DBs *before any writes occur*.

use rusqlite::{params, OpenFlags, OptionalExtension};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

pub fn expected_sqlitegraph_schema_version() -> i64 {
    sqlitegraph::schema::SCHEMA_VERSION
}

pub use crate::migrate_cmd::MAGELLAN_SCHEMA_VERSION;

/// Check if the database schema needs to be created or upgraded.
///
/// Returns `true` when:
/// - `magellan_meta` table does not exist (new database)
/// - `magellan_meta.magellan_schema_version` != `MAGELLAN_SCHEMA_VERSION`
pub fn needs_schema_upgrade(conn: &rusqlite::Connection) -> Result<bool, DbCompatError> {
    let has_meta: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='magellan_meta' LIMIT 1",
            [],
            |_| Ok(true),
        )
        .optional()
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?
        .unwrap_or(false);

    if !has_meta {
        return Ok(true);
    }

    let version: i64 = conn
        .query_row(
            "SELECT magellan_schema_version FROM magellan_meta WHERE id=1",
            [],
            |row| row.get(0),
        )
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;

    Ok(version != MAGELLAN_SCHEMA_VERSION)
}

pub fn ensure_magellan_meta(
    conn: &rusqlite::Connection,
    db_path: &Path,
) -> Result<(), DbCompatError> {
    if is_in_memory_path(db_path) {
        return Ok(());
    }
    conn.execute(
        "CREATE TABLE IF NOT EXISTS magellan_meta (
            id INTEGER PRIMARY KEY CHECK (id = 1),
            magellan_schema_version INTEGER NOT NULL,
            sqlitegraph_schema_version INTEGER NOT NULL,
            created_at INTEGER NOT NULL
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    let existing: Option<(i64, i64)> = conn
        .query_row(
            "SELECT magellan_schema_version, sqlitegraph_schema_version FROM magellan_meta WHERE id=1",
            [],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .optional()
        .map_err(|e| map_sqlite_query_err(db_path, e))?;

    let expected_sqlitegraph = expected_sqlitegraph_schema_version();
    match existing {
        None => {
            let created_at = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs() as i64;
            conn.execute(
                "INSERT INTO magellan_meta (id, magellan_schema_version, sqlitegraph_schema_version, created_at)
                 VALUES (1, ?1, ?2, ?3)",
                params![MAGELLAN_SCHEMA_VERSION, expected_sqlitegraph, created_at],
            ).map_err(|e| map_sqlite_query_err(db_path, e))?;
            ensure_geo_index_meta_schema(conn)?;
            ensure_symbol_fts_schema(conn)?;
            Ok(())
        }
        Some((found_magellan, found_sqlitegraph)) => {
            if found_magellan != MAGELLAN_SCHEMA_VERSION {
                let mut current_version = found_magellan;
                while current_version < MAGELLAN_SCHEMA_VERSION {
                    match current_version {
                        4 => {
                            ensure_ast_schema(conn)?;
                            current_version = 5;
                        }
                        5 => {
                            ensure_ast_schema(conn)?;
                            current_version = 6;
                        }
                        6 => {
                            ensure_cfg_schema(conn)?;
                            current_version = 7;
                        }
                        7 => {
                            ensure_cfg_hash_column(conn)?;
                            current_version = 8;
                        }
                        8 => {
                            ensure_statements_column(conn)?;
                            current_version = 9;
                        }
                        9 => {
                            ensure_4d_coordinates_columns(conn)?;
                            current_version = 10;
                        }
                        10 => {
                            ensure_geo_index_meta_schema(conn)?;
                            current_version = 11;
                        }
                        11 => {
                            ensure_symbol_fts_schema(&conn)?;
                            current_version = 12;
                        }
                        _ => {
                            return Err(DbCompatError::MagellanSchemaMismatch {
                                path: db_path.to_path_buf(),
                                found: found_magellan,
                                expected: MAGELLAN_SCHEMA_VERSION,
                            });
                        }
                    }
                    conn.execute(
                        "UPDATE magellan_meta SET magellan_schema_version = ?1 WHERE id = 1",
                        params![current_version],
                    )
                    .map_err(|e| map_sqlite_query_err(db_path, e))?;
                }
            }
            if found_sqlitegraph != expected_sqlitegraph {
                return Err(DbCompatError::SqliteGraphSchemaMismatch {
                    path: db_path.to_path_buf(),
                    found: found_sqlitegraph,
                    expected: expected_sqlitegraph,
                });
            }
            Ok(())
        }
    }
}

pub fn ensure_ast_schema(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    conn.execute("CREATE TABLE IF NOT EXISTS ast_nodes (id INTEGER PRIMARY KEY AUTOINCREMENT, parent_id INTEGER, kind TEXT NOT NULL, byte_start INTEGER NOT NULL, byte_end INTEGER NOT NULL, file_id INTEGER)", []).map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_ast_nodes_parent ON ast_nodes(parent_id)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_ast_nodes_span ON ast_nodes(byte_start, byte_end)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_ast_nodes_file_id ON ast_nodes(file_id)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    Ok(())
}

pub fn ensure_metrics_schema(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    conn.execute("CREATE TABLE IF NOT EXISTS file_metrics (file_path TEXT PRIMARY KEY, symbol_count INTEGER NOT NULL, loc INTEGER NOT NULL, estimated_loc REAL NOT NULL, fan_in INTEGER NOT NULL DEFAULT 0, fan_out INTEGER NOT NULL DEFAULT 0, complexity_score REAL NOT NULL DEFAULT 0.0, last_updated INTEGER NOT NULL)", []).map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute("CREATE TABLE IF NOT EXISTS symbol_metrics (symbol_id INTEGER PRIMARY KEY, symbol_name TEXT NOT NULL, kind TEXT NOT NULL, file_path TEXT NOT NULL, loc INTEGER NOT NULL, estimated_loc REAL NOT NULL, fan_in INTEGER NOT NULL DEFAULT 0, fan_out INTEGER NOT NULL DEFAULT 0, cyclomatic_complexity INTEGER NOT NULL DEFAULT 1, last_updated INTEGER NOT NULL, FOREIGN KEY (symbol_id) REFERENCES graph_entities(id) ON DELETE CASCADE)", []).map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    Ok(())
}

pub const CFG_EDGE: &str = "CFG_BLOCK";

pub fn ensure_cfg_schema(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    conn.execute(
        "CREATE TABLE IF NOT EXISTS cfg_blocks (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        function_id INTEGER NOT NULL,
        kind TEXT NOT NULL,
        terminator TEXT NOT NULL,
        byte_start INTEGER NOT NULL,
        byte_end INTEGER NOT NULL,
        start_line INTEGER NOT NULL,
        start_col INTEGER NOT NULL,
        end_line INTEGER NOT NULL,
        end_col INTEGER NOT NULL,
        cfg_hash TEXT,
        statements TEXT,
        coord_x INTEGER DEFAULT 0,
        coord_y INTEGER DEFAULT 0,
        coord_z INTEGER DEFAULT 0,
        coord_t TEXT DEFAULT NULL,
        FOREIGN KEY (function_id) REFERENCES graph_entities(id) ON DELETE CASCADE
    )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_cfg_blocks_function ON cfg_blocks(function_id)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_cfg_blocks_hash ON cfg_blocks(cfg_hash)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;

    // Check if cfg_edges table already exists (with any schema - legacy or new)
    // If it exists, skip creating tables/indexes to avoid schema conflicts
    let table_exists: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='cfg_edges' LIMIT 1",
            [],
            |_row| Ok(true),
        )
        .unwrap_or(false);

    if table_exists {
        // cfg_edges already exists - don't try to create with potentially incompatible schema
        return Ok(());
    }

    conn.execute(
        "CREATE TABLE IF NOT EXISTS cfg_edges (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        function_id INTEGER NOT NULL,
        source_idx INTEGER NOT NULL,
        target_idx INTEGER NOT NULL,
        edge_type TEXT NOT NULL,
        FOREIGN KEY (function_id) REFERENCES graph_entities(id) ON DELETE CASCADE
    )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_cfg_edges_function ON cfg_edges(function_id)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    Ok(())
}

pub fn ensure_statements_column(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    let has_col: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='statements'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_col {
        conn.execute("ALTER TABLE cfg_blocks ADD COLUMN statements TEXT", [])
            .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }
    Ok(())
}

/// Add 4D spatial-temporal coordinate columns to cfg_blocks table
///
/// This enables the 4D SSoT (Single Source of Truth) feature where:
/// - X (coord_x): Dominator depth (structural hierarchy)
/// - Y (coord_y): Loop nesting level (iterative complexity)
/// - Z (coord_z): Branch count (decision density)
/// - T (coord_t): Git commit hash or trace timestamp
pub fn ensure_4d_coordinates_columns(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    // Check and add coord_x column
    let has_x: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='coord_x'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_x {
        conn.execute(
            "ALTER TABLE cfg_blocks ADD COLUMN coord_x INTEGER DEFAULT 0",
            [],
        )
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }

    // Check and add coord_y column
    let has_y: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='coord_y'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_y {
        conn.execute(
            "ALTER TABLE cfg_blocks ADD COLUMN coord_y INTEGER DEFAULT 0",
            [],
        )
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }

    // Check and add coord_z column
    let has_z: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='coord_z'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_z {
        conn.execute(
            "ALTER TABLE cfg_blocks ADD COLUMN coord_z INTEGER DEFAULT 0",
            [],
        )
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }

    // Check and add coord_t column
    let has_t: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='coord_t'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);
    if !has_t {
        conn.execute(
            "ALTER TABLE cfg_blocks ADD COLUMN coord_t TEXT DEFAULT NULL",
            [],
        )
        .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }

    Ok(())
}

/// Add geo_index_meta table for lazy geometric index tracking
///
/// This table records when a .geo file was built from the SQLite database
/// and is used for staleness detection.
pub fn ensure_geo_index_meta_schema(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    conn.execute(
        "CREATE TABLE IF NOT EXISTS geo_index_meta (
            id INTEGER PRIMARY KEY CHECK (id = 1),
            geo_path TEXT NOT NULL,
            built_at INTEGER NOT NULL,
            schema_version INTEGER NOT NULL,
            symbol_count INTEGER NOT NULL,
            call_count INTEGER NOT NULL,
            cfg_block_count INTEGER NOT NULL,
            checksum TEXT NOT NULL
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    Ok(())
}

/// Add symbol_fts FTS5 virtual table for fast symbol search (v11 -> v12 migration)
///
/// Creates an FTS5 virtual table that indexes symbol names from graph_entities
/// for prefix and full-text search capabilities.
pub fn ensure_symbol_fts_schema(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    conn.execute(
        "CREATE VIRTUAL TABLE IF NOT EXISTS symbol_fts USING fts5(
            name,
            content='graph_entities',
            content_rowid='id'
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    Ok(())
}

/// Ensure cfg_hash column exists in cfg_blocks table (v7 -> v8 migration)
///
/// Adds cfg_hash column for cache invalidation in downstream tools.
pub fn ensure_cfg_hash_column(conn: &rusqlite::Connection) -> Result<(), DbCompatError> {
    // Add cfg_hash column if it doesn't exist
    // SQLite doesn't support IF NOT EXISTS for ADD COLUMN, so we check first
    let has_column: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_blocks') WHERE name='cfg_hash'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);

    if !has_column {
        conn.execute("ALTER TABLE cfg_blocks ADD COLUMN cfg_hash TEXT", [])
            .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;
    }

    // Create index for hash-based cache lookups
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_cfg_blocks_hash
         ON cfg_blocks(cfg_hash)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(Path::new(":memory:"), e))?;

    Ok(())
}

/// Add coverage side tables for weighted CFG analysis.
///
/// Creates cfg_block_coverage, cfg_edge_coverage, and cfg_coverage_meta.
/// Safe to call repeatedly — uses CREATE TABLE IF NOT EXISTS.
pub fn ensure_coverage_schema(
    conn: &rusqlite::Connection,
    db_path: &std::path::Path,
) -> Result<(), DbCompatError> {
    // Check if cfg_edges has the expected 'id' column before creating coverage tables
    // that reference it. If not, skip coverage schema (this is a legacy database).
    let has_cfg_edges_id: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('cfg_edges') WHERE name='id' LIMIT 1",
            [],
            |_row| Ok(true),
        )
        .unwrap_or(false);

    if !has_cfg_edges_id {
        // Legacy database with different cfg_edges schema - skip coverage tables
        return Ok(());
    }

    conn.execute(
        "CREATE TABLE IF NOT EXISTS cfg_block_coverage (
            block_id INTEGER PRIMARY KEY,
            hit_count INTEGER NOT NULL DEFAULT 0,
            source_kind TEXT NOT NULL,
            source_revision TEXT,
            ingested_at INTEGER NOT NULL,
            FOREIGN KEY (block_id) REFERENCES cfg_blocks(id) ON DELETE CASCADE
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS cfg_edge_coverage (
            edge_id INTEGER PRIMARY KEY,
            hit_count INTEGER NOT NULL DEFAULT 0,
            source_kind TEXT NOT NULL,
            source_revision TEXT,
            ingested_at INTEGER NOT NULL,
            FOREIGN KEY (edge_id) REFERENCES cfg_edges(id) ON DELETE CASCADE
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS cfg_coverage_meta (
            source_kind TEXT PRIMARY KEY,
            source_revision TEXT,
            ingested_at INTEGER,
            total_blocks INTEGER,
            total_edges INTEGER
        )",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_block_cov_hit ON cfg_block_coverage(block_id, hit_count)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_edge_cov_hit ON cfg_edge_coverage(edge_id, hit_count)",
        [],
    )
    .map_err(|e| map_sqlite_query_err(db_path, e))?;

    Ok(())
}

/// Result of a successful preflight.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PreflightOk {
    NewDb,
    CompatibleExisting { found_schema_version: i64 },
}

#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
pub enum DbCompatError {
    #[error("DB_COMPAT: not a sqlite database: {path}")]
    NotSqlite { path: PathBuf },
    #[error("DB_COMPAT: corrupt sqlite database: {path}")]
    CorruptSqlite { path: PathBuf },
    #[error("DB_COMPAT: sqlitegraph schema mismatch: {path} (found={found}, expected={expected})")]
    SqliteGraphSchemaMismatch {
        path: PathBuf,
        found: i64,
        expected: i64,
    },
    #[error("DB_COMPAT: magellan schema mismatch: {path} (found={found}, expected={expected})")]
    MagellanSchemaMismatch {
        path: PathBuf,
        found: i64,
        expected: i64,
    },
    #[error("DB_COMPAT: expected sqlitegraph database but missing graph_meta table: {path}")]
    MissingGraphMeta { path: PathBuf },
    #[error("DB_COMPAT: graph_meta missing schema_version column: {path}")]
    GraphMetaMissingSchemaVersion { path: PathBuf },
    #[error("DB_COMPAT: graph_meta missing expected row id=1: {path}")]
    MissingGraphMetaRow { path: PathBuf, id: i64 },
    #[error("DB_COMPAT: sqlite preflight failure: {path}")]
    PreflightSqliteFailure { path: PathBuf },
}

pub fn preflight_sqlitegraph_compat(db_path: &Path) -> Result<PreflightOk, DbCompatError> {
    if is_in_memory_path(db_path) || !db_path.exists() {
        return Ok(PreflightOk::NewDb);
    }

    let header = std::fs::read(db_path).map_err(|_| DbCompatError::PreflightSqliteFailure {
        path: db_path.to_path_buf(),
    })?;
    if header.len() < 16 || &header[..16] != b"SQLite format 3\0" {
        return Err(DbCompatError::NotSqlite {
            path: db_path.to_path_buf(),
        });
    }

    let conn = rusqlite::Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_ONLY)
        .map_err(|e| map_sqlite_open_err(db_path, e))?;

    let has_graph_meta: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='graph_meta' LIMIT 1",
            [],
            |_row| Ok(true),
        )
        .optional()
        .map_err(|e| map_sqlite_query_err(db_path, e))?
        .unwrap_or(false);
    if !has_graph_meta {
        return Err(DbCompatError::MissingGraphMeta {
            path: db_path.to_path_buf(),
        });
    }

    let has_schema_version: bool = conn
        .query_row(
            "SELECT 1 FROM pragma_table_info('graph_meta') WHERE name='schema_version' LIMIT 1",
            [],
            |_row| Ok(true),
        )
        .optional()
        .map_err(|e| map_sqlite_query_err(db_path, e))?
        .unwrap_or(false);
    if !has_schema_version {
        return Err(DbCompatError::GraphMetaMissingSchemaVersion {
            path: db_path.to_path_buf(),
        });
    }

    let found: Option<i64> = conn
        .query_row(
            "SELECT schema_version FROM graph_meta WHERE id=1",
            [],
            |row| row.get(0),
        )
        .optional()
        .map_err(|e| map_sqlite_query_err(db_path, e))?;
    let Some(found) = found else {
        return Err(DbCompatError::MissingGraphMetaRow {
            path: db_path.to_path_buf(),
            id: 1,
        });
    };

    let expected = expected_sqlitegraph_schema_version();
    if found != expected {
        return Err(DbCompatError::SqliteGraphSchemaMismatch {
            path: db_path.to_path_buf(),
            found,
            expected,
        });
    }
    Ok(PreflightOk::CompatibleExisting {
        found_schema_version: found,
    })
}

fn is_in_memory_path(db_path: &Path) -> bool {
    db_path == Path::new(":memory:")
}
fn map_sqlite_open_err(path: &Path, _e: rusqlite::Error) -> DbCompatError {
    DbCompatError::PreflightSqliteFailure {
        path: path.to_path_buf(),
    }
}
fn map_sqlite_query_err(path: &Path, _e: rusqlite::Error) -> DbCompatError {
    DbCompatError::PreflightSqliteFailure {
        path: path.to_path_buf(),
    }
}

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

    #[test]
    fn test_cfg_edges_schema_created() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        ensure_cfg_schema(&conn).unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='cfg_edges'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 1);

        // Verify index was also created
        let idx_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name='idx_cfg_edges_function'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(idx_count, 1);
    }

    #[test]
    fn test_coverage_schema_created() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        ensure_cfg_schema(&conn).unwrap();
        ensure_coverage_schema(&conn, std::path::Path::new(":memory:")).unwrap();

        let block_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='cfg_block_coverage'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(block_count, 1);

        let edge_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='cfg_edge_coverage'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(edge_count, 1);
    }
}