Skip to content

Commit

Permalink
Merge pull request #102 from weiznich/feature/implement_migration_con…
Browse files Browse the repository at this point in the history
…nection

Implement `MigrationConnection` for the `AsyncConnectionWrapper` type
  • Loading branch information
weiznich committed Sep 1, 2023
2 parents fead869 + 8c941d2 commit af1066d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tokio = {version = "1.12.0", features = ["rt", "macros", "rt-multi-thread"]}
cfg-if = "1"
chrono = "0.4"
diesel = { version = "2.1.0", default-features = false, features = ["chrono"]}
diesel_migrations = "2.1.0"

[features]
default = []
Expand Down
13 changes: 13 additions & 0 deletions src/async_connection_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub type AsyncConnectionWrapper<C, B = self::implementation::Tokio> =
pub use self::implementation::AsyncConnectionWrapper;

mod implementation {
use diesel::connection::SimpleConnection;

use super::*;

pub struct AsyncConnectionWrapper<C, B> {
Expand Down Expand Up @@ -277,6 +279,17 @@ mod implementation {
}
}

impl<C, B> diesel::migration::MigrationConnection for AsyncConnectionWrapper<C, B>
where
B: BlockOn,
Self: diesel::Connection,
{
fn setup(&mut self) -> diesel::QueryResult<usize> {
self.batch_execute(diesel::migration::CREATE_MIGRATIONS_TABLE)
.map(|()| 0)
}
}

#[cfg(feature = "tokio")]
pub struct Tokio {
handle: Option<tokio::runtime::Handle>,
Expand Down
3 changes: 3 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type TestConnection = AsyncMysqlConnection;
#[cfg(feature = "postgres")]
type TestConnection = AsyncPgConnection;

#[allow(dead_code)]
type TestBackend = <TestConnection as AsyncConnection>::Backend;

#[tokio::test]
async fn test_basic_insert_and_load() -> QueryResult<()> {
let conn = &mut connection().await;
Expand Down
13 changes: 13 additions & 0 deletions tests/sync_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use diesel::migration::Migration;
use diesel::prelude::*;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;

Expand All @@ -24,3 +25,15 @@ async fn test_sync_wrapper_under_runtime() {
.await
.unwrap();
}

#[test]
fn check_run_migration() {
use diesel_migrations::MigrationHarness;

let db_url = std::env::var("DATABASE_URL").unwrap();
let migrations: Vec<Box<dyn Migration<crate::TestBackend>>> = Vec::new();
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();

// just use `run_migrations` here because that's the easiest one without additional setup
conn.run_migrations(&migrations).unwrap();
}

0 comments on commit af1066d

Please sign in to comment.