use std::sync::Arc;
use sha2::{Digest, Sha256};
use crate::checksum::verify_checksum;
use crate::db::{with_transaction, Db};
use crate::error::{MigratromError, Result};
use crate::graph::plan_order;
use crate::logger::{ConsoleLogger, MigrationLogger};
use crate::sql::SqlDialect;
use crate::types::{ApplyResult, Migration, Operation};
use super::executor::{run_operation, OpOutcome};
use super::history::{
ensure_history_table, read_applied_ids, read_applied_records, record_migration,
DEFAULT_HISTORY_TABLE,
};
pub struct ApplyOptions {
pub history_table: Option<String>,
pub dry_run: bool,
pub advisory_lock: bool,
pub logger: Option<Arc<dyn MigrationLogger>>,
}
impl Default for ApplyOptions {
fn default() -> Self {
Self {
history_table: None,
dry_run: false,
advisory_lock: true,
logger: None,
}
}
}
pub async fn apply_migrations(
migrations: &[Migration],
db: &dyn Db,
dialect: &dyn SqlDialect,
options: &ApplyOptions,
) -> Result<ApplyResult> {
let history_table = options
.history_table
.clone()
.unwrap_or_else(|| DEFAULT_HISTORY_TABLE.to_string());
if options.advisory_lock && dialect.capabilities().advisory_locks {
let key = advisory_lock_key(&history_table);
dialect.acquire_lock(db, key).await?;
let result = apply_unlocked(migrations, db, dialect, options, &history_table).await;
let _ = dialect.release_lock(db, key).await;
result
} else {
apply_unlocked(migrations, db, dialect, options, &history_table).await
}
}
async fn apply_unlocked(
migrations: &[Migration],
db: &dyn Db,
dialect: &dyn SqlDialect,
options: &ApplyOptions,
history_table: &str,
) -> Result<ApplyResult> {
let logger: Arc<dyn MigrationLogger> = options
.logger
.clone()
.unwrap_or_else(|| Arc::new(ConsoleLogger));
ensure_history_table(db, history_table, dialect).await?;
let applied_ids = read_applied_ids(db, history_table, dialect).await?;
let stored = read_applied_records(db, history_table, dialect).await?;
for m in migrations {
if applied_ids.contains(&m.id) {
if let Some(record) = stored.get(&m.id) {
verify_checksum(&record.checksum, &m.operations, m.id)?;
}
}
}
let pending = plan_order(migrations, &applied_ids)?;
let mut applied = Vec::new();
let mut skipped_ops = Vec::new();
for migration in &pending {
logger.info(&format!("migration {}", migration.id));
let result = apply_one(
db,
migration,
dialect,
history_table,
logger.as_ref(),
options.dry_run,
&mut skipped_ops,
)
.await;
match result {
Ok(()) => {
if !options.dry_run {
applied.push(migration.id);
}
}
Err(cause) => {
return Err(MigratromError::MigrationFailed {
id: migration.id,
cause: Box::new(cause),
})
}
}
}
Ok(ApplyResult {
applied,
skipped_ops,
})
}
async fn apply_one(
db: &dyn Db,
migration: &Migration,
dialect: &dyn SqlDialect,
history_table: &str,
log: &dyn MigrationLogger,
dry_run: bool,
skipped_ops: &mut Vec<String>,
) -> Result<()> {
let has_outside = migration.operations.iter().any(|o| o.outside_transaction);
if dry_run {
for op in &migration.operations {
if run_operation(db, op, log, true).await? == OpOutcome::Skipped {
skipped_ops.push(op.id.clone());
}
}
return Ok(());
}
if has_outside {
return run_segmented(db, migration, dialect, history_table, log, skipped_ops).await;
}
with_transaction(db, || async {
for op in &migration.operations {
if run_operation(db, op, log, false).await? == OpOutcome::Skipped {
skipped_ops.push(op.id.clone());
}
}
record_migration(db, history_table, migration, dialect).await?;
Ok::<(), MigratromError>(())
})
.await
}
async fn run_segmented(
db: &dyn Db,
migration: &Migration,
dialect: &dyn SqlDialect,
history_table: &str,
log: &dyn MigrationLogger,
skipped_ops: &mut Vec<String>,
) -> Result<()> {
let mut transactional: Vec<&Operation> = Vec::new();
for op in &migration.operations {
if op.outside_transaction {
flush_segment(
db,
&transactional,
false,
dialect,
history_table,
migration,
log,
skipped_ops,
)
.await?;
transactional.clear();
if run_operation(db, op, log, false).await? == OpOutcome::Skipped {
skipped_ops.push(op.id.clone());
}
} else {
transactional.push(op);
}
}
flush_segment(
db,
&transactional,
true,
dialect,
history_table,
migration,
log,
skipped_ops,
)
.await
}
#[allow(clippy::too_many_arguments)]
async fn flush_segment(
db: &dyn Db,
batch: &[&Operation],
record: bool,
dialect: &dyn SqlDialect,
history_table: &str,
migration: &Migration,
log: &dyn MigrationLogger,
skipped_ops: &mut Vec<String>,
) -> Result<()> {
if batch.is_empty() {
return Ok(());
}
with_transaction(db, || async {
for op in batch {
if run_operation(db, op, log, false).await? == OpOutcome::Skipped {
skipped_ops.push(op.id.clone());
}
}
if record {
record_migration(db, history_table, migration, dialect).await?;
}
Ok::<(), MigratromError>(())
})
.await
}
pub fn advisory_lock_key(history_table: &str) -> i64 {
let digest = Sha256::digest(history_table.as_bytes());
let mut bytes = [0u8; 8];
bytes.copy_from_slice(&digest[..8]);
i64::from_be_bytes(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db::Row;
use crate::sql::SqliteDialect;
use crate::types::{Check, ExecuteStep};
use async_trait::async_trait;
use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;
#[derive(Default)]
struct FakeState {
executed: Vec<String>,
bool_results: HashMap<String, bool>,
bool_seqs: HashMap<String, VecDeque<bool>>,
depth: u32,
snapshot: Vec<String>,
rolled_back: bool,
}
#[derive(Default)]
struct FakeDb {
state: Mutex<FakeState>,
}
impl FakeDb {
fn set_bool(&self, sql: &str, value: bool) {
self.state
.lock()
.unwrap()
.bool_results
.insert(sql.to_string(), value);
}
fn set_bool_seq(&self, sql: &str, values: &[bool]) {
self.state
.lock()
.unwrap()
.bool_seqs
.insert(sql.to_string(), values.iter().copied().collect());
}
fn executed(&self) -> Vec<String> {
self.state.lock().unwrap().executed.clone()
}
fn rolled_back(&self) -> bool {
self.state.lock().unwrap().rolled_back
}
}
#[async_trait]
impl Db for FakeDb {
async fn query_bool(&self, sql: &str) -> Result<bool> {
let mut state = self.state.lock().unwrap();
if let Some(seq) = state.bool_seqs.get_mut(sql) {
if let Some(v) = seq.pop_front() {
return Ok(v);
}
}
Ok(state.bool_results.get(sql).copied().unwrap_or(false))
}
async fn execute(&self, sql: &str) -> Result<()> {
self.state.lock().unwrap().executed.push(sql.to_string());
Ok(())
}
async fn query_rows(&self, _sql: &str) -> Result<Vec<Box<dyn Row>>> {
Ok(Vec::new())
}
async fn begin(&self) -> Result<()> {
let mut state = self.state.lock().unwrap();
if state.depth == 0 {
state.snapshot = state.executed.clone();
state.rolled_back = false;
}
state.depth += 1;
Ok(())
}
async fn commit(&self) -> Result<()> {
self.state.lock().unwrap().depth -= 1;
Ok(())
}
async fn rollback(&self) -> Result<()> {
let mut state = self.state.lock().unwrap();
state.depth -= 1;
if state.depth == 0 {
state.executed = state.snapshot.clone();
state.rolled_back = true;
}
Ok(())
}
}
fn op(id: &str, pre: &str, exec: &str, post: &str) -> Operation {
Operation {
id: id.to_string(),
label: id.to_string(),
precheck: vec![Check::new("pre", pre)],
execute: vec![ExecuteStep::new("do", exec)],
postcheck: vec![Check::new("post", post)],
outside_transaction: false,
}
}
fn options() -> ApplyOptions {
ApplyOptions {
advisory_lock: false,
..Default::default()
}
}
#[tokio::test]
async fn applies_and_records_a_migration() {
let db = FakeDb::default();
db.set_bool("PRE", true);
db.set_bool_seq("POST", &[false, true]); let migration = Migration::new(1, None, vec![op("op.one", "PRE", "DO", "POST")]);
let result = apply_migrations(&[migration], &db, &SqliteDialect, &options())
.await
.unwrap();
assert_eq!(result.applied, vec![1]);
assert!(result.skipped_ops.is_empty());
assert!(db.executed().iter().any(|s| s == "DO"));
assert!(db.executed().iter().any(|s| s.starts_with("INSERT INTO")));
}
#[tokio::test]
async fn skips_already_satisfied_operation() {
let db = FakeDb::default();
db.set_bool("POST", true); let migration = Migration::new(1, None, vec![op("op.one", "PRE", "DO", "POST")]);
let result = apply_migrations(&[migration], &db, &SqliteDialect, &options())
.await
.unwrap();
assert_eq!(result.applied, vec![1]);
assert_eq!(result.skipped_ops, vec!["op.one".to_string()]);
assert!(!db.executed().iter().any(|s| s == "DO"));
}
#[tokio::test]
async fn dry_run_executes_and_records_nothing() {
let db = FakeDb::default();
db.set_bool("PRE", true);
db.set_bool("POST", false);
let migration = Migration::new(1, None, vec![op("op.one", "PRE", "DO", "POST")]);
let result = apply_migrations(
&[migration],
&db,
&SqliteDialect,
&ApplyOptions {
dry_run: true,
advisory_lock: false,
..Default::default()
},
)
.await
.unwrap();
assert!(result.applied.is_empty());
assert!(!db.executed().iter().any(|s| s == "DO"));
assert!(!db.executed().iter().any(|s| s.starts_with("INSERT INTO")));
}
#[tokio::test]
async fn rolls_back_when_postcheck_fails() {
let db = FakeDb::default();
db.set_bool("PRE", true);
db.set_bool("POST", false); let migration = Migration::new(1, None, vec![op("op.one", "PRE", "DO", "POST")]);
let err = apply_migrations(&[migration], &db, &SqliteDialect, &options())
.await
.unwrap_err();
assert!(matches!(err, MigratromError::MigrationFailed { id: 1, .. }));
assert!(db.rolled_back());
assert!(!db.executed().iter().any(|s| s == "DO"));
}
#[test]
fn advisory_lock_key_is_stable() {
assert_eq!(advisory_lock_key("a"), advisory_lock_key("a"));
assert_ne!(advisory_lock_key("a"), advisory_lock_key("b"));
}
}