use toolu_orm_core::journal::{compute_hash, Journal};
pub const INIT_SQL: &str = "CREATE TABLE users (id TEXT PRIMARY KEY);\n\
--> statement-breakpoint\nCREATE TABLE audit (id TEXT);";
pub const POSTS_SQL: &str = "CREATE TABLE posts (id TEXT PRIMARY KEY);";
pub const THIRD_SQL: &str = "CREATE TABLE c (id TEXT);";
type FixtureResult<T> = Result<T, Box<dyn std::error::Error>>;
pub fn migrations_dir() -> FixtureResult<(tempfile::TempDir, String)> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("migrations");
std::fs::create_dir_all(&path)?;
Ok((dir, path.to_str().ok_or("non-UTF8 path")?.to_owned()))
}
pub fn write_migrations(dir: &str, files: &[(&str, &str)]) -> FixtureResult<()> {
let mut journal = Journal::empty();
for (name, sql) in files {
std::fs::write(format!("{dir}/{name}"), sql)?;
journal.add_entry(name, &compute_hash(sql));
}
journal.write_to_path(&format!("{dir}/_journal.json"))?;
Ok(())
}