libsalmo/workflows/
commit.rs

1use crate::{salmo_contex::SalmoContext, migration_data::committed::{Commit, CommittedFile}, backend::{MigrationWithStatus, MigrationStatus}};
2use anyhow::anyhow;
3
4pub fn commit_migration(ctx: &SalmoContext, migration_id: &str) -> anyhow::Result<Commit> {
5  let migrations = ctx.migrations()?;
6  let migration = migrations.db[migration_id].clone();
7  let commits = ctx.commits()?;
8  let mut backends = ctx.environments.iter().map(|e| e.backend()).collect::<anyhow::Result<Vec<_>>>()?;
9  let status = backends.iter_mut().map(|b| {
10      Ok(b.migration_status(&commits, &[migration.clone()])?.into_iter().next().unwrap())
11  }).collect::<anyhow::Result<Vec<MigrationWithStatus>>>()?;
12
13  if !status.iter().all(|m| matches!(m.status, MigrationStatus::Tried { up_to_date: _ })) {
14      return Err(anyhow!("Migration `{}` has not been tried in an environment. Commit aborted.", migration_id))
15  }
16
17  if !status.iter().all(|m| match m.status {
18      MigrationStatus::Tried { up_to_date } => up_to_date,
19      _ => false
20  } ) {
21      return Err(anyhow!("Migration `{}` was changed after last being tried. Retry it in order to commit. Commit aborted.", migration_id))
22  }
23
24  let commit = CommittedFile::add_commit(ctx, migration_id)?;
25
26  for (mut backend, m) in backends.into_iter().zip(status) {
27    backend.execute_migrations(&[MigrationWithStatus {
28        migration: m.migration.clone(),
29        status: MigrationStatus::Committed { tried: true }
30    }])?;
31    backend.untry_migrations(&[m], false)?;
32  }
33
34  Ok(commit)
35}