use super::apply;
use crate::database::migrations::CustomMigrationError;
use crate::database::versions::ops::{model::Migration, plan};
use crate::database::versions::{history, naming, query, record};
use crate::database::DatabaseConnection;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Started {
pub version: String,
pub schema: String,
pub backfilled: Vec<String>,
}
pub async fn start(
connection: &DatabaseConnection,
schema: &str,
version: &str,
migration: &Migration,
) -> Result<Started, CustomMigrationError> {
history::ensure_table(connection).await?;
if let Some(active) = query::active(connection).await? {
return Err(super::conflict(format!(
"migration '{active}' is already in progress; complete or roll it back first"
)));
}
let version_schema = naming::versioned_schema(schema, version);
let plan = plan(migration, &version_schema);
record::start(connection, version).await?;
match apply::apply(connection, schema, version, &plan).await {
Ok(backfilled) => Ok(Started {
version: version.to_string(),
schema: version_schema,
backfilled,
}),
Err(error) => {
record::discard(connection, version).await?;
Err(error)
}
}
}