Skip to content

Commit

Permalink
Remove diesel_migrations_async (#362)
Browse files Browse the repository at this point in the history
* Remove `diesel_migrations_async`

* move pool construction after blocking task
  • Loading branch information
aumetra authored Oct 2, 2023
1 parent 77699cb commit e028205
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
27 changes: 14 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions crates/kitsune-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ build = "build.rs"

[dependencies]
diesel = { version = "2.1.2", features = ["uuid"] }
diesel-async = { version = "0.4.1", features = ["deadpool", "postgres"] }
diesel_full_text_search = { version = "2.1.0", default-features = false }
diesel_migrations_async = { git = "https://github.com/aumetra/diesel_migrations_async.git", rev = "8e7085e7c93baca31b817b1814e14c4ac2beb2c4", features = [
diesel-async = { version = "0.4.1", features = [
"async-connection-wrapper",
"deadpool",
"postgres",
"tokio",
] }
diesel_full_text_search = { version = "2.1.0", default-features = false }
diesel_migrations = "2.1.0"
iso8601-timestamp = { version = "0.2.12", features = ["diesel-pg"] }
kitsune-language = { path = "../kitsune-language" }
kitsune-type = { path = "../kitsune-type" }
Expand All @@ -20,5 +23,6 @@ serde = { version = "1.0.188", features = ["derive"] }
simd-json = "0.11.1"
speedy-uuid = { path = "../../lib/speedy-uuid", features = ["diesel"] }
thiserror = "1.0.49"
tokio = { version = "1.32.0", features = ["rt"] }
tracing-log = "0.1.3"
typed-builder = "0.16.2"
6 changes: 6 additions & 0 deletions crates/kitsune-db/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ pub enum Error {
#[error(transparent)]
Diesel(#[from] diesel::result::Error),

#[error(transparent)]
DieselConnection(#[from] diesel::result::ConnectionError),

#[error(transparent)]
Migration(BoxError),

#[error(transparent)]
Pool(#[from] PoolError),

#[error(transparent)]
TokioJoin(#[from] tokio::task::JoinError),
}
23 changes: 19 additions & 4 deletions crates/kitsune-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
forbidden_lint_groups
)]

use diesel::Connection;
use diesel_async::{
async_connection_wrapper::AsyncConnectionWrapper,
pooled_connection::{deadpool::Pool, AsyncDieselConnectionManager},
AsyncPgConnection,
};
use diesel_migrations_async::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use tracing_log::LogTracer;

pub use crate::{
Expand All @@ -37,16 +39,29 @@ pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
pub async fn connect(conn_str: &str, max_pool_size: usize) -> Result<PgPool> {
LogTracer::init().ok();

tokio::task::spawn_blocking({
let conn_str = conn_str.to_string();

move || {
let mut migration_conn =
AsyncConnectionWrapper::<AsyncPgConnection>::establish(conn_str.as_str())?;

migration_conn
.run_pending_migrations(MIGRATIONS)
.map_err(Error::Migration)?;

Ok::<_, Error>(())
}
})
.await??;

let config = AsyncDieselConnectionManager::<AsyncPgConnection>::new(conn_str);
let pool = Pool::builder(config)
.max_size(max_pool_size)
.build()
.unwrap();

let mut conn = pool.get().await?;
conn.run_pending_migrations(MIGRATIONS)
.await
.map_err(Error::Migration)?;

kitsune_language::generate_postgres_enum(&mut conn, "language_iso_code").await?;
kitsune_language::generate_regconfig_function(
Expand Down

0 comments on commit e028205

Please sign in to comment.