use std::path::PathBuf;
use tempfile::TempDir;
use tracing::info;
#[test_log::test(tokio::test)]
#[cfg_attr(not(feature = "redb"), ignore)]
async fn test_automatic_migration_from_v2_to_v3() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "redb")]
use freenet::storages::redb::ReDb;
let v2_db_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("data")
.join("redb_v2_test.db");
assert!(
v2_db_path.exists(),
"v2 test database not found at {:?}. Generate it with: cargo run --manifest-path crates/core/tests/redb_migration_generator/Cargo.toml",
v2_db_path
);
let temp_dir = TempDir::new()?;
let db_path = temp_dir.path().join("db");
std::fs::copy(&v2_db_path, &db_path)?;
info!("Copied v2 database to test directory: {:?}", db_path);
info!("This should trigger automatic migration...");
let result = ReDb::new(temp_dir.path()).await;
assert!(
result.is_ok(),
"Migration should succeed, but got error: {:?}",
result.err()
);
let backups: Vec<_> = std::fs::read_dir(temp_dir.path())?
.filter_map(|e| e.ok())
.filter(|e| {
let name = e.file_name().to_string_lossy().to_string();
name.contains("backup")
})
.collect();
assert!(
!backups.is_empty(),
"Should have created a backup of the v2 database. Found files: {:?}",
std::fs::read_dir(temp_dir.path())?
.filter_map(|e| e.ok())
.map(|e| e.file_name())
.collect::<Vec<_>>()
);
let _db = result.unwrap();
info!("✓ Migration completed successfully");
info!("✓ Backup created: {:?}", backups[0].file_name());
Ok(())
}
#[test_log::test(tokio::test)]
#[cfg_attr(not(feature = "redb"), ignore)]
async fn test_normal_operation_without_migration() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "redb")]
use freenet::storages::redb::ReDb;
let temp_dir = TempDir::new()?;
let result = ReDb::new(temp_dir.path()).await;
assert!(
result.is_ok(),
"Should create fresh database without issues"
);
let backups: Vec<_> = std::fs::read_dir(temp_dir.path())?
.filter_map(|e| e.ok())
.filter(|e| {
let name = e.file_name().to_string_lossy().to_string();
name.contains("backup")
})
.collect();
assert!(
backups.is_empty(),
"Should not create backup when no migration is needed"
);
Ok(())
}