Allow MigrationSource to be implemented externally (#511)

This commit is contained in:
Kaleb Elwert 2020-07-26 20:23:44 -07:00 committed by GitHub
parent 05ffcb312d
commit e0ace861c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View file

@ -1,5 +1,7 @@
use std::borrow::Cow;
use sha2::{Digest, Sha384};
#[derive(Debug, Clone)]
pub struct Migration {
pub version: i64,
@ -7,3 +9,16 @@ pub struct Migration {
pub sql: Cow<'static, str>,
pub checksum: Cow<'static, [u8]>,
}
impl Migration {
pub fn new(version: i64, description: Cow<'static, str>, sql: Cow<'static, str>) -> Self {
let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
Migration {
version,
description,
sql,
checksum,
}
}
}

View file

@ -2,7 +2,6 @@ use crate::error::BoxDynError;
use crate::migrate::Migration;
use futures_core::future::BoxFuture;
use futures_util::TryStreamExt;
use sha2::{Digest, Sha384};
use sqlx_rt::fs;
use std::borrow::Cow;
use std::fmt::Debug;
@ -44,14 +43,11 @@ impl<'s> MigrationSource<'s> for &'s Path {
let sql = fs::read_to_string(&entry.path()).await?;
let checksum = Vec::from(Sha384::digest(sql.as_bytes()).as_slice());
migrations.push(Migration {
migrations.push(Migration::new(
version,
description: Cow::Owned(description),
sql: Cow::Owned(sql),
checksum: Cow::Owned(checksum),
})
Cow::Owned(description),
Cow::Owned(sql),
));
}
// ensure that we are sorted by `VERSION ASC`