Edit Migration and Migrator to make embedded migrations possible

This commit is contained in:
Raphaël Thériault 2020-07-23 12:57:48 -04:00
parent eba6f3973d
commit 60c3ece671
2 changed files with 14 additions and 16 deletions

View File

@ -2,18 +2,8 @@ use std::borrow::Cow;
#[derive(Debug, Clone)]
pub struct Migration {
pub(crate) version: i64,
pub(crate) description: Cow<'static, str>,
pub(crate) sql: Cow<'static, str>,
pub(crate) checksum: Cow<'static, [u8]>,
}
impl Migration {
pub fn version(&self) -> i64 {
self.version
}
pub fn description(&self) -> &str {
&*self.description
}
pub version: i64,
pub description: Cow<'static, str>,
pub sql: Cow<'static, str>,
pub checksum: Cow<'static, [u8]>,
}

View File

@ -1,11 +1,12 @@
use crate::acquire::Acquire;
use crate::migrate::{Migrate, MigrateError, Migration, MigrationSource};
use std::borrow::Cow;
use std::ops::Deref;
use std::slice;
#[derive(Debug)]
pub struct Migrator {
migrations: Vec<Migration>,
migrations: Cow<'static, [Migration]>,
}
impl Migrator {
@ -31,10 +32,17 @@ impl Migrator {
S: MigrationSource<'s>,
{
Ok(Self {
migrations: source.resolve().await.map_err(MigrateError::Source)?,
migrations: Cow::Owned(source.resolve().await.map_err(MigrateError::Source)?),
})
}
/// Creates a new instance from a static slice of migrations.
pub async fn from_static(migrations: &'static [Migration]) -> Self {
Self {
migrations: Cow::Borrowed(migrations),
}
}
/// Get an iterator over all known migrations.
pub fn iter(&self) -> slice::Iter<'_, Migration> {
self.migrations.iter()