Skip to content

Commit af1066d

Browse files
authored
Merge pull request #102 from weiznich/feature/implement_migration_connection
Implement `MigrationConnection` for the `AsyncConnectionWrapper` type
2 parents fead869 + 8c941d2 commit af1066d

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tokio = {version = "1.12.0", features = ["rt", "macros", "rt-multi-thread"]}
3232
cfg-if = "1"
3333
chrono = "0.4"
3434
diesel = { version = "2.1.0", default-features = false, features = ["chrono"]}
35+
diesel_migrations = "2.1.0"
3536

3637
[features]
3738
default = []

src/async_connection_wrapper.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ pub type AsyncConnectionWrapper<C, B = self::implementation::Tokio> =
100100
pub use self::implementation::AsyncConnectionWrapper;
101101

102102
mod implementation {
103+
use diesel::connection::SimpleConnection;
104+
103105
use super::*;
104106

105107
pub struct AsyncConnectionWrapper<C, B> {
@@ -277,6 +279,17 @@ mod implementation {
277279
}
278280
}
279281

282+
impl<C, B> diesel::migration::MigrationConnection for AsyncConnectionWrapper<C, B>
283+
where
284+
B: BlockOn,
285+
Self: diesel::Connection,
286+
{
287+
fn setup(&mut self) -> diesel::QueryResult<usize> {
288+
self.batch_execute(diesel::migration::CREATE_MIGRATIONS_TABLE)
289+
.map(|()| 0)
290+
}
291+
}
292+
280293
#[cfg(feature = "tokio")]
281294
pub struct Tokio {
282295
handle: Option<tokio::runtime::Handle>,

tests/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ type TestConnection = AsyncMysqlConnection;
9292
#[cfg(feature = "postgres")]
9393
type TestConnection = AsyncPgConnection;
9494

95+
#[allow(dead_code)]
96+
type TestBackend = <TestConnection as AsyncConnection>::Backend;
97+
9598
#[tokio::test]
9699
async fn test_basic_insert_and_load() -> QueryResult<()> {
97100
let conn = &mut connection().await;

tests/sync_wrapper.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use diesel::migration::Migration;
12
use diesel::prelude::*;
23
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
34

@@ -24,3 +25,15 @@ async fn test_sync_wrapper_under_runtime() {
2425
.await
2526
.unwrap();
2627
}
28+
29+
#[test]
30+
fn check_run_migration() {
31+
use diesel_migrations::MigrationHarness;
32+
33+
let db_url = std::env::var("DATABASE_URL").unwrap();
34+
let migrations: Vec<Box<dyn Migration<crate::TestBackend>>> = Vec::new();
35+
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();
36+
37+
// just use `run_migrations` here because that's the easiest one without additional setup
38+
conn.run_migrations(&migrations).unwrap();
39+
}

0 commit comments

Comments
 (0)