sql-splitter 1.21.0

High-performance CLI tool for splitting large SQL dump files into individual table files
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
//! Cache manager for persistent DuckDB databases.
//!
//! Caches imported SQL dumps as DuckDB database files for fast repeated queries.
//! Cache identity is (canonical path, size, mtime, --tables set, dialect), so
//! different table selections of the same dump get separate cache slots. Writes
//! are atomic: data is staged in a `.partial` file and renamed into place, and
//! the index is only updated after the data file is committed.

use crate::parser::SqlDialect;
use anyhow::{Context, Result};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// Cache entry metadata
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheEntry {
    /// Canonical path of the original dump file
    pub dump_path: String,
    /// SHA256 hash of (path + size + mtime + tables + dialect)
    pub cache_key: String,
    /// Size of original dump file
    pub dump_size: u64,
    /// Modification time of dump file (as Unix timestamp)
    pub dump_mtime: u64,
    /// Size of cached DuckDB file
    pub cache_size: u64,
    /// When this cache entry was created
    pub created_at: u64,
    /// Number of tables in the cache
    pub table_count: usize,
    /// Total rows in the cache
    pub row_count: u64,
    /// Tables contained in the cache (None = entry from an older version)
    #[serde(default)]
    pub tables: Option<Vec<String>>,
}

/// Cache index containing all cache entries
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct CacheIndex {
    pub entries: Vec<CacheEntry>,
}

/// Manager for cached DuckDB databases
pub struct CacheManager {
    cache_dir: PathBuf,
}

/// DuckDB names its write-ahead log by appending `.wal` to the database path.
fn wal_path(db_path: &Path) -> PathBuf {
    let mut os = db_path.as_os_str().to_os_string();
    os.push(".wal");
    PathBuf::from(os)
}

fn mtime_secs(metadata: &fs::Metadata) -> u64 {
    metadata
        .modified()
        .unwrap_or(SystemTime::UNIX_EPOCH)
        .duration_since(SystemTime::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// Normalize a table selection for cache-key hashing: lowercased (matching the
/// loader's case-insensitive filter), sorted, deduped. `None` means all tables.
fn normalize_tables(tables: Option<&[String]>) -> String {
    match tables {
        None => "all".to_string(),
        Some(tables) => {
            let mut names: Vec<String> = tables.iter().map(|t| t.to_ascii_lowercase()).collect();
            names.sort();
            names.dedup();
            names.join(",")
        }
    }
}

impl CacheManager {
    /// Create a new cache manager with the default cache directory
    pub fn new() -> Result<Self> {
        let cache_dir = Self::default_cache_dir()?;
        fs::create_dir_all(&cache_dir).context("Failed to create cache directory")?;
        Ok(Self { cache_dir })
    }

    /// Create a cache manager with a custom cache directory
    pub fn with_dir(cache_dir: PathBuf) -> Result<Self> {
        fs::create_dir_all(&cache_dir).context("Failed to create cache directory")?;
        Ok(Self { cache_dir })
    }

    /// Get the default cache directory (`SQL_SPLITTER_CACHE_DIR` overrides)
    pub fn default_cache_dir() -> Result<PathBuf> {
        if let Some(dir) = std::env::var_os("SQL_SPLITTER_CACHE_DIR").filter(|d| !d.is_empty()) {
            return Ok(PathBuf::from(dir));
        }

        let cache_base = dirs::cache_dir()
            .or_else(|| dirs::home_dir().map(|h| h.join(".cache")))
            .context("Could not determine cache directory")?;

        Ok(cache_base.join("sql-splitter").join("duckdb"))
    }

    /// Compute the cache key for a dump file and import configuration
    pub fn compute_cache_key(
        dump_path: &Path,
        tables: Option<&[String]>,
        dialect: Option<SqlDialect>,
    ) -> Result<String> {
        let canonical = dump_path
            .canonicalize()
            .with_context(|| format!("Failed to canonicalize path: {}", dump_path.display()))?;

        let metadata = fs::metadata(&canonical)
            .with_context(|| format!("Failed to read metadata: {}", dump_path.display()))?;

        let dialect = dialect.map_or_else(|| "auto".to_string(), |d| d.to_string());
        let key_input = format!(
            "{}:{}:{}:{}:{}",
            canonical.display(),
            metadata.len(),
            mtime_secs(&metadata),
            normalize_tables(tables),
            dialect
        );

        let mut hasher = Sha256::new();
        hasher.update(key_input.as_bytes());
        let hash = hasher.finalize();

        Ok(hex::encode(&hash[..16])) // Use first 16 bytes for shorter filename
    }

    /// Get the path where a cached database would be stored
    pub fn cache_path(&self, cache_key: &str) -> PathBuf {
        self.cache_dir.join(format!("{}.duckdb", cache_key))
    }

    /// Get the staging path where a cache is built before being committed
    pub fn partial_path(&self, cache_key: &str) -> PathBuf {
        self.cache_dir.join(format!("{}.duckdb.partial", cache_key))
    }

    /// Check if a valid cache exists for a dump file and import configuration.
    ///
    /// Existence is sufficient: the key encodes the dump's size and mtime, and
    /// files only appear at the final path via atomic rename of a complete copy.
    pub fn has_valid_cache(
        &self,
        dump_path: &Path,
        tables: Option<&[String]>,
        dialect: Option<SqlDialect>,
    ) -> Result<bool> {
        let cache_key = Self::compute_cache_key(dump_path, tables, dialect)?;
        Ok(self.cache_path(&cache_key).exists())
    }

    /// Get the cache path for a dump file, if a valid cache exists
    pub fn get_cache(
        &self,
        dump_path: &Path,
        tables: Option<&[String]>,
        dialect: Option<SqlDialect>,
    ) -> Result<Option<PathBuf>> {
        if self.has_valid_cache(dump_path, tables, dialect)? {
            let cache_key = Self::compute_cache_key(dump_path, tables, dialect)?;
            Ok(Some(self.cache_path(&cache_key)))
        } else {
            Ok(None)
        }
    }

    /// Remove a staged partial cache file (leftover from an interrupted run)
    pub fn discard_partial(&self, cache_key: &str) -> Result<()> {
        let partial = self.partial_path(cache_key);
        if partial.exists() {
            fs::remove_file(&partial).context("Failed to remove partial cache file")?;
        }
        let wal = wal_path(&partial);
        if wal.exists() {
            fs::remove_file(&wal)?;
        }
        Ok(())
    }

    /// Commit a fully-written partial cache file: atomically move it into place
    /// and update the index. The previous cache and index are untouched if this
    /// (or anything before it) fails.
    pub fn commit_cache(
        &self,
        dump_path: &Path,
        cache_key: &str,
        tables: Vec<String>,
        row_count: u64,
    ) -> Result<PathBuf> {
        let partial = self.partial_path(cache_key);
        let cache_path = self.cache_path(cache_key);

        if !partial.exists() {
            anyhow::bail!("No staged cache file to commit: {}", partial.display());
        }

        // rename() does not overwrite on Windows; clear the destination first
        if cache_path.exists() {
            fs::remove_file(&cache_path).context("Failed to remove old cache file")?;
        }
        let old_wal = wal_path(&cache_path);
        if old_wal.exists() {
            let _ = fs::remove_file(&old_wal);
        }

        fs::rename(&partial, &cache_path).context("Failed to move cache file into place")?;
        let partial_wal = wal_path(&partial);
        if partial_wal.exists() {
            let _ = fs::remove_file(&partial_wal);
        }

        let canonical = dump_path
            .canonicalize()
            .with_context(|| format!("Failed to canonicalize path: {}", dump_path.display()))?;
        let metadata = fs::metadata(&canonical)?;
        let dump_mtime = mtime_secs(&metadata);
        let dump_path_str = canonical.display().to_string();

        let mut index = self.load_index()?;

        // Garbage-collect caches of older versions of this dump file
        let stale_keys: Vec<String> = index
            .entries
            .iter()
            .filter(|e| {
                e.dump_path == dump_path_str
                    && (e.dump_size != metadata.len() || e.dump_mtime != dump_mtime)
            })
            .map(|e| e.cache_key.clone())
            .collect();
        for key in &stale_keys {
            let path = self.cache_path(key);
            let _ = fs::remove_file(&path);
            let _ = fs::remove_file(wal_path(&path));
        }
        index
            .entries
            .retain(|e| e.cache_key != cache_key && !stale_keys.contains(&e.cache_key));

        index.entries.push(CacheEntry {
            dump_path: dump_path_str,
            cache_key: cache_key.to_string(),
            dump_size: metadata.len(),
            dump_mtime,
            cache_size: fs::metadata(&cache_path).map(|m| m.len()).unwrap_or(0),
            created_at: SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0),
            table_count: tables.len(),
            row_count,
            tables: Some(tables),
        });

        self.save_index(&index)?;
        Ok(cache_path)
    }

    /// Load the cache index. A corrupt index is treated as empty (with a
    /// warning) so one bad write can't permanently brick cache operations.
    pub fn load_index(&self) -> Result<CacheIndex> {
        let index_path = self.cache_dir.join("index.json");

        if !index_path.exists() {
            return Ok(CacheIndex::default());
        }

        let content = fs::read_to_string(&index_path).context("Failed to read cache index")?;
        Ok(serde_json::from_str(&content).unwrap_or_else(|e| {
            eprintln!(
                "Warning: cache index {} is corrupt ({}); treating it as empty",
                index_path.display(),
                e
            );
            CacheIndex::default()
        }))
    }

    /// Save the cache index atomically (write to temp file, then rename)
    fn save_index(&self, index: &CacheIndex) -> Result<()> {
        let index_path = self.cache_dir.join("index.json");
        let tmp_path = self.cache_dir.join("index.json.tmp");
        let content =
            serde_json::to_string_pretty(index).context("Failed to serialize cache index")?;
        fs::write(&tmp_path, content).context("Failed to write cache index")?;
        fs::rename(&tmp_path, &index_path).context("Failed to replace cache index")?;
        Ok(())
    }

    /// List all cache entries
    pub fn list_entries(&self) -> Result<Vec<CacheEntry>> {
        let index = self.load_index()?;
        Ok(index.entries)
    }

    /// Remove a specific cache entry
    pub fn remove_cache(&self, cache_key: &str) -> Result<()> {
        let cache_path = self.cache_path(cache_key);

        if cache_path.exists() {
            fs::remove_file(&cache_path).context("Failed to remove cache file")?;
        }

        // Also remove WAL and staged partial files if they exist
        let wal = wal_path(&cache_path);
        if wal.exists() {
            fs::remove_file(&wal)?;
        }
        self.discard_partial(cache_key)?;

        // Update index
        let mut index = self.load_index()?;
        index.entries.retain(|e| e.cache_key != cache_key);
        self.save_index(&index)?;

        Ok(())
    }

    /// Clear all cached databases.
    ///
    /// Sweeps the whole cache directory (data files, WAL files, staged
    /// partials) rather than trusting the index, so it also recovers from a
    /// lost/corrupt index and collects files written by older versions.
    /// Returns the number of cached databases removed.
    pub fn clear_all(&self) -> Result<usize> {
        let mut count = 0;

        for entry in fs::read_dir(&self.cache_dir).context("Failed to read cache directory")? {
            let path = entry?.path();
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if name.ends_with(".duckdb") {
                count += 1;
            }
            if name.ends_with(".duckdb") || name.ends_with(".wal") || name.ends_with(".partial") {
                fs::remove_file(&path)
                    .with_context(|| format!("Failed to remove {}", path.display()))?;
            }
        }

        self.save_index(&CacheIndex::default())?;
        Ok(count)
    }

    /// Get total cache size in bytes
    pub fn total_size(&self) -> Result<u64> {
        let entries = self.list_entries()?;
        Ok(entries.iter().map(|e| e.cache_size).sum())
    }

    /// Get the cache directory path
    pub fn cache_dir(&self) -> &Path {
        &self.cache_dir
    }
}

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

    fn setup_test_cache() -> (CacheManager, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let cache_manager = CacheManager::with_dir(temp_dir.path().to_path_buf()).unwrap();
        (cache_manager, temp_dir)
    }

    fn strings(names: &[&str]) -> Vec<String> {
        names.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn test_cache_key_computation() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let key1 = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        let key2 = CacheManager::compute_cache_key(&test_file, None, None).unwrap();

        assert_eq!(key1, key2);
        assert_eq!(key1.len(), 32); // 16 bytes hex encoded
    }

    #[test]
    fn test_cache_key_changes_with_content() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.sql");

        fs::write(&test_file, "SELECT 1;").unwrap();
        let key1 = CacheManager::compute_cache_key(&test_file, None, None).unwrap();

        // Modify the file with different size (which is always captured, unlike mtime)
        fs::write(&test_file, "SELECT 2; -- with extra content to change size").unwrap();
        let key2 = CacheManager::compute_cache_key(&test_file, None, None).unwrap();

        // Key should be different because size changed
        assert_ne!(key1, key2);
    }

    #[test]
    fn test_cache_key_includes_tables() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let all = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        let users =
            CacheManager::compute_cache_key(&test_file, Some(&strings(&["users"])), None).unwrap();
        let both =
            CacheManager::compute_cache_key(&test_file, Some(&strings(&["users", "orders"])), None)
                .unwrap();

        assert_ne!(all, users);
        assert_ne!(all, both);
        assert_ne!(users, both);
    }

    #[test]
    fn test_cache_key_normalizes_tables() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let messy = CacheManager::compute_cache_key(
            &test_file,
            Some(&strings(&["Orders", "users", "USERS"])),
            None,
        )
        .unwrap();
        let clean =
            CacheManager::compute_cache_key(&test_file, Some(&strings(&["orders", "users"])), None)
                .unwrap();

        assert_eq!(messy, clean);
    }

    #[test]
    fn test_cache_key_includes_dialect() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let auto = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        let mysql =
            CacheManager::compute_cache_key(&test_file, None, Some(SqlDialect::MySql)).unwrap();

        assert_ne!(auto, mysql);
    }

    #[test]
    fn test_cache_path() {
        let (cache_manager, _temp_dir) = setup_test_cache();
        let cache_path = cache_manager.cache_path("abc123");
        assert!(cache_path.to_string_lossy().ends_with("abc123.duckdb"));
    }

    #[test]
    fn test_has_valid_cache_when_missing() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        assert!(!cache_manager
            .has_valid_cache(&test_file, None, None)
            .unwrap());
    }

    #[test]
    fn test_partial_is_not_a_valid_cache() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        fs::write(cache_manager.partial_path(&key), b"garbage").unwrap();

        assert!(!cache_manager
            .has_valid_cache(&test_file, None, None)
            .unwrap());
        assert!(cache_manager
            .get_cache(&test_file, None, None)
            .unwrap()
            .is_none());
    }

    #[test]
    fn test_commit_cache_renames_and_records_size() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        fs::write(cache_manager.partial_path(&key), b"fake database contents").unwrap();

        let cache_path = cache_manager
            .commit_cache(&test_file, &key, strings(&["users"]), 42)
            .unwrap();

        assert!(cache_path.exists());
        assert!(!cache_manager.partial_path(&key).exists());

        let entries = cache_manager.list_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert!(entries[0].cache_size > 0);
        assert_eq!(entries[0].tables, Some(strings(&["users"])));
        assert_eq!(entries[0].table_count, 1);
        assert_eq!(entries[0].row_count, 42);
    }

    #[test]
    fn test_commit_without_partial_leaves_index_intact() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        fs::write(cache_manager.partial_path(&key), b"data").unwrap();
        cache_manager
            .commit_cache(&test_file, &key, strings(&["users"]), 1)
            .unwrap();

        // Committing a key with no staged file must fail without touching the index
        let result = cache_manager.commit_cache(&test_file, "deadbeef", strings(&["x"]), 1);
        assert!(result.is_err());
        assert_eq!(cache_manager.list_entries().unwrap().len(), 1);
    }

    #[test]
    fn test_two_table_sets_coexist() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let key1 =
            CacheManager::compute_cache_key(&test_file, Some(&strings(&["users"])), None).unwrap();
        let key2 = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        assert_ne!(key1, key2);

        fs::write(cache_manager.partial_path(&key1), b"one").unwrap();
        cache_manager
            .commit_cache(&test_file, &key1, strings(&["users"]), 1)
            .unwrap();
        fs::write(cache_manager.partial_path(&key2), b"two").unwrap();
        cache_manager
            .commit_cache(&test_file, &key2, strings(&["users", "orders"]), 2)
            .unwrap();

        assert!(cache_manager.cache_path(&key1).exists());
        assert!(cache_manager.cache_path(&key2).exists());
        assert_eq!(cache_manager.list_entries().unwrap().len(), 2);
    }

    #[test]
    fn test_gc_deletes_caches_of_changed_dump() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();

        let old_key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        fs::write(cache_manager.partial_path(&old_key), b"v1").unwrap();
        cache_manager
            .commit_cache(&test_file, &old_key, strings(&["users"]), 1)
            .unwrap();

        // Change the dump (size change guarantees a new key)
        fs::write(&test_file, "SELECT 2; -- different size now").unwrap();
        let new_key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        assert_ne!(old_key, new_key);
        fs::write(cache_manager.partial_path(&new_key), b"v2").unwrap();
        cache_manager
            .commit_cache(&test_file, &new_key, strings(&["users"]), 2)
            .unwrap();

        assert!(!cache_manager.cache_path(&old_key).exists());
        let entries = cache_manager.list_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].cache_key, new_key);
    }

    #[test]
    fn test_corrupt_index_recovers_as_empty() {
        let (cache_manager, temp_dir) = setup_test_cache();
        fs::write(temp_dir.path().join("index.json"), "{broken json").unwrap();

        let index = cache_manager.load_index().unwrap();
        assert!(index.entries.is_empty());

        // And a subsequent commit works normally
        let test_file = temp_dir.path().join("test.sql");
        fs::write(&test_file, "SELECT 1;").unwrap();
        let key = CacheManager::compute_cache_key(&test_file, None, None).unwrap();
        fs::write(cache_manager.partial_path(&key), b"data").unwrap();
        cache_manager
            .commit_cache(&test_file, &key, strings(&["users"]), 1)
            .unwrap();
        assert_eq!(cache_manager.list_entries().unwrap().len(), 1);
    }

    #[test]
    fn test_index_old_format_parses_with_unknown_tables() {
        let (cache_manager, temp_dir) = setup_test_cache();
        let old_entry = r#"{"entries":[{"dump_path":"/tmp/a.sql","cache_key":"abc","dump_size":1,"dump_mtime":2,"cache_size":3,"created_at":4,"table_count":5,"row_count":6}]}"#;
        fs::write(temp_dir.path().join("index.json"), old_entry).unwrap();

        let entries = cache_manager.list_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].tables, None);
    }

    #[test]
    fn test_clear_all_sweeps_directory() {
        let (cache_manager, temp_dir) = setup_test_cache();
        fs::write(temp_dir.path().join("aa.duckdb"), b"x").unwrap();
        fs::write(temp_dir.path().join("aa.duckdb.wal"), b"x").unwrap();
        fs::write(temp_dir.path().join("bb.duckdb.partial"), b"x").unwrap();
        fs::write(temp_dir.path().join("index.json"), "{broken").unwrap();

        let count = cache_manager.clear_all().unwrap();
        assert_eq!(count, 1);
        assert!(!temp_dir.path().join("aa.duckdb").exists());
        assert!(!temp_dir.path().join("aa.duckdb.wal").exists());
        assert!(!temp_dir.path().join("bb.duckdb.partial").exists());
        assert!(cache_manager.list_entries().unwrap().is_empty());
    }

    #[test]
    fn test_list_entries_empty() {
        let (cache_manager, _temp_dir) = setup_test_cache();
        let entries = cache_manager.list_entries().unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_total_size_empty() {
        let (cache_manager, _temp_dir) = setup_test_cache();
        assert_eq!(cache_manager.total_size().unwrap(), 0);
    }
}