Skip to content

Commit e0ace86

Browse files
authored
Allow MigrationSource to be implemented externally (launchbadge#511)
1 parent 05ffcb3 commit e0ace86

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

sqlx-core/src/migrate/migration.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
use std::borrow::Cow;
22

3+
use sha2::{Digest, Sha384};
4+
35
#[derive(Debug, Clone)]
46
pub struct Migration {
57
pub version: i64,
68
pub description: Cow<'static, str>,
79
pub sql: Cow<'static, str>,
810
pub checksum: Cow<'static, [u8]>,
911
}
12+
13+
impl Migration {
14+
pub fn new(version: i64, description: Cow<'static, str>, sql: Cow<'static, str>) -> Self {
15+
let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
16+
17+
Migration {
18+
version,
19+
description,
20+
sql,
21+
checksum,
22+
}
23+
}
24+
}

sqlx-core/src/migrate/source.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::error::BoxDynError;
22
use crate::migrate::Migration;
33
use futures_core::future::BoxFuture;
44
use futures_util::TryStreamExt;
5-
use sha2::{Digest, Sha384};
65
use sqlx_rt::fs;
76
use std::borrow::Cow;
87
use std::fmt::Debug;
@@ -44,14 +43,11 @@ impl<'s> MigrationSource<'s> for &'s Path {
4443

4544
let sql = fs::read_to_string(&entry.path()).await?;
4645

47-
let checksum = Vec::from(Sha384::digest(sql.as_bytes()).as_slice());
48-
49-
migrations.push(Migration {
46+
migrations.push(Migration::new(
5047
version,
51-
description: Cow::Owned(description),
52-
sql: Cow::Owned(sql),
53-
checksum: Cow::Owned(checksum),
54-
})
48+
Cow::Owned(description),
49+
Cow::Owned(sql),
50+
));
5551
}
5652

5753
// ensure that we are sorted by `VERSION ASC`

0 commit comments

Comments
 (0)