Skip to content

Commit

Permalink
iko: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 9, 2022
1 parent 8ca5ab3 commit 2938a66
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions iko/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::future::Future;

use sqlx::AnyPool;

use crate::{migration::Migration, Error};
use crate::{migration::Migration, migration_status::Version, Error};

pub struct Iter<'a> {
inner: std::slice::Iter<'a, Migration>,
Expand All @@ -20,31 +20,33 @@ impl<'a> Iterator for Iter<'a> {
pub struct Migrations(Vec<Migration>);

impl Migrations {
pub(crate) fn iter(&self) -> Iter<'_> {
pub(crate) fn iter(&self) -> Iter {
Iter {
inner: self.0.iter(),
}
}

pub fn push<Fut>(
&mut self,
version: u32,
migrate: impl Fn(AnyPool) -> Fut + 'static,
) -> Result<(), Error>
pub fn push<F, Fut>(&mut self, version: u32, migrate: F) -> Result<(), Error>
where
F: Fn(AnyPool) -> Fut + 'static,
Fut: Future<Output = sqlx::Result<()>> + 'static,
{
if version == 0 {
return Err(Error::ReservedVersion);
}
if let Some(last_migration_version) = self.0.last().map(|m| u32::from(m.version())) {
if version <= last_migration_version {
return Err(Error::IncorrectVersionOrder);
}
if Version::from(version) <= self.last_migration_version() {
return Err(Error::IncorrectVersionOrder);
}
self.0.push(Migration::from((version, migrate)));
Ok(())
}

fn last_migration_version(&self) -> Version {
self.0
.last()
.map(Migration::version)
.unwrap_or_else(|| Version::from(0))
}
}

#[cfg(test)]
Expand Down

0 comments on commit 2938a66

Please sign in to comment.