1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use postgres::{GenericClient};

use crate::migration_data::migrations::Migration;

pub struct ExecutedMigration {
  pub id: String,
}

impl ExecutedMigration {
  pub fn get_last(client: &mut impl GenericClient) -> anyhow::Result<Option<i32>> {
    Ok(
      client.query_opt("SELECT max(committed_index) FROM salmo_executed_migrations", &[])?
        .map(|r| r.try_get(0).unwrap_or(0)))
  }

  pub fn insert(client: &mut impl GenericClient, m: &Migration, commit_index: i32) -> anyhow::Result<()> {
    client.execute("INSERT INTO salmo_executed_migrations (id, committed_index) VALUES ($1, $2)", &[&m.id, &commit_index])?;
    Ok(())
  }
}