use cratefield_core::Harness;
pub fn apply(harness: &Harness, dialect: &str, url: &str) -> Result<(), String> {
if dialect != "postgres" {
return Err(format!(
"dialect {dialect:?} is not supported for apply (only postgres, issue #18)"
));
}
#[cfg(not(feature = "postgres"))]
{
let _ = (harness, url);
Err(
"this fz binary was built without cratefield-cli's `postgres` feature — \
rebuild the fz bin with `--features cratefield-cli/postgres` to apply \
migrations to Postgres (sqlite/D1 migrations run through \
`wrangler d1 migrations apply`)"
.to_owned(),
)
}
#[cfg(feature = "postgres")]
apply_postgres(harness, url)
}
#[cfg(feature = "postgres")]
fn apply_postgres(harness: &Harness, url: &str) -> Result<(), String> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|err| format!("cannot start the async runtime: {err}"))?;
runtime.block_on(async {
let db = cratefield_adapter_postgres::Postgres::connect(url)
.await
.map_err(|err| format!("cannot connect (check --url): {err}"))?;
db.apply_harness_migrations(harness)
.await
.map_err(|err| format!("migrations failed: {err}"))
})
}