use dbx_core::Database;
use tempfile::TempDir;
fn main() -> dbx_core::DbxResult<()> {
println!("\n═══════════════════════════════════════════");
println!(" Metadata Persistence Integration Test");
println!("═══════════════════════════════════════════\n");
let temp_dir = TempDir::new()?;
let db_path = temp_dir.path().to_path_buf();
println!("Phase 1: Creating database with schemas...");
{
let db = Database::open(&db_path)?;
db.execute_sql("CREATE TABLE users (id INT, name TEXT, age INT)")?;
db.execute_sql("CREATE TABLE products (id INT, name TEXT, price REAL)")?;
println!(" ✓ Created 2 tables");
db.flush()?;
println!(" ✓ Flushed to storage");
}
println!("\nPhase 2: Reopening database and verifying schema persistence...");
{
let db = Database::open(&db_path)?;
println!(" ✓ Database reopened successfully");
match db.execute_sql("CREATE TABLE users (id INT, name TEXT)") {
Err(e) => {
println!(
" ✓ users table schema persisted (got expected error: {})",
e
);
}
Ok(_) => {
panic!("users table should already exist!");
}
}
db.execute_sql("CREATE TABLE IF NOT EXISTS users (id INT, name TEXT)")?;
println!(" ✓ CREATE TABLE IF NOT EXISTS works correctly");
db.execute_sql("CREATE TABLE orders (id INT, total REAL)")?;
println!(" ✓ Can create new tables");
db.flush()?;
}
println!("\nPhase 3: Reopening and testing DROP TABLE persistence...");
{
let db = Database::open(&db_path)?;
match db.execute_sql("CREATE TABLE users (id INT)") {
Err(_) => println!(" ✓ users table still exists"),
Ok(_) => panic!("users should exist"),
}
match db.execute_sql("CREATE TABLE products (id INT)") {
Err(_) => println!(" ✓ products table still exists"),
Ok(_) => panic!("products should exist"),
}
match db.execute_sql("CREATE TABLE orders (id INT)") {
Err(_) => println!(" ✓ orders table still exists"),
Ok(_) => panic!("orders should exist"),
}
db.execute_sql("DROP TABLE products")?;
println!(" ✓ DROP TABLE executed");
db.flush()?;
}
println!("\nPhase 4: Reopening after DROP TABLE...");
{
let db = Database::open(&db_path)?;
db.execute_sql("CREATE TABLE products (id INT, name TEXT)")?;
println!(" ✓ products table was correctly dropped (can recreate it)");
match db.execute_sql("CREATE TABLE users (id INT)") {
Err(_) => println!(" ✓ users table still exists"),
Ok(_) => panic!("users should still exist"),
}
match db.execute_sql("CREATE TABLE orders (id INT)") {
Err(_) => println!(" ✓ orders table still exists"),
Ok(_) => panic!("orders should still exist"),
}
}
println!("\n═══════════════════════════════════════════");
println!(" ✅ All metadata persistence tests passed!");
println!("═══════════════════════════════════════════\n");
Ok(())
}