diff --git a/Cargo.lock b/Cargo.lock index 24adfaf..33f9c13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,6 +55,7 @@ dependencies = [ "custom_error", "derive-new", "diesel", + "diesel-tracing", "diesel_migrations", "dotenv", "envy", @@ -889,10 +890,23 @@ dependencies = [ "byteorder", "chrono", "diesel_derives", + "ipnetwork", + "libc", "libsqlite3-sys", "r2d2", ] +[[package]] +name = "diesel-tracing" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528c21047ca2f10fa8978ee4f17acbeeae64acaf8912f5541a4741feb9db0ee4" +dependencies = [ + "diesel", + "ipnetwork", + "tracing", +] + [[package]] name = "diesel_derives" version = "1.4.1" @@ -1366,6 +1380,15 @@ dependencies = [ "winreg", ] +[[package]] +name = "ipnetwork" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02c3eaab3ac0ede60ffa41add21970a7df7d91772c03383aac6c2c3d53cc716b" +dependencies = [ + "serde", +] + [[package]] name = "isolang" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 069af12..4709a2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ tracing-subscriber = { version = "0.2.18", features = ["fmt", "registry", "env-f tracing-futures = "0.2.5" tracing-actix-web = "0.2.1" tracing-bunyan-formatter = "0.2.4" +diesel-tracing = { version = "0.1.4", features = ["sqlite"] } [dependencies.build-info] version = "=0.0.23" diff --git a/src/actions/users.rs b/src/actions/users.rs index 0de5fad..afbab43 100644 --- a/src/actions/users.rs +++ b/src/actions/users.rs @@ -6,7 +6,7 @@ use bcrypt::{hash, verify, DEFAULT_COST}; pub fn find_user_by_uid( uid: i32, - conn: &SqliteConnection, + conn: &impl diesel::Connection, ) -> Result, errors::DomainError> { use crate::schema::users::dsl::*; @@ -21,7 +21,7 @@ pub fn find_user_by_uid( pub fn _find_user_by_name( user_name: String, - conn: &SqliteConnection, + conn: &impl diesel::Connection, ) -> Result, errors::DomainError> { let maybe_user = query::_get_user_by_name(&user_name) .first::(conn) @@ -31,7 +31,7 @@ pub fn _find_user_by_name( } pub fn get_all( - conn: &SqliteConnection, + conn: &impl diesel::Connection, ) -> Result, errors::DomainError> { use crate::schema::users::dsl::*; Ok(users @@ -42,7 +42,7 @@ pub fn get_all( /// Run query using Diesel to insert a new database row and return the result. pub fn insert_new_user( nu: models::NewUser, - conn: &SqliteConnection, + conn: &impl diesel::Connection, ) -> Result { // It is common when using Diesel with Actix web to import schema-related // modules inside a function's scope (rather than the normal module's scope) @@ -63,7 +63,7 @@ pub fn insert_new_user( pub fn verify_password( user_name: &str, given_password: &str, - conn: &SqliteConnection, + conn: &impl diesel::Connection, ) -> Result { use crate::schema::users::dsl::*; let password_hash = users diff --git a/src/main.rs b/src/main.rs index 59c975a..2637692 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![forbid(unsafe_code)] use actix_demo::{AppConfig, AppData, EnvConfig, LoggerFormat}; -use diesel::{r2d2::ConnectionManager, SqliteConnection}; +use diesel::r2d2::ConnectionManager; +use diesel_tracing::sqlite::InstrumentedSqliteConnection; use io::ErrorKind; use std::io; use tracing::subscriber::set_global_default; @@ -32,7 +33,8 @@ async fn main() -> io::Result<()> { let _ = setup_logger(env_config.logger_format)?; let connspec = &env_config.database_url; - let manager = ConnectionManager::::new(connspec); + let manager = + ConnectionManager::::new(connspec); let pool = r2d2::Pool::builder().build(manager).map_err(|err| { io::Error::new( ErrorKind::Other, diff --git a/src/types.rs b/src/types.rs index 6825b68..48ff968 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,3 +1,4 @@ -use diesel::prelude::*; use diesel::r2d2::{self, ConnectionManager}; -pub type DbPool = r2d2::Pool>; +pub type DbPool = r2d2::Pool< + ConnectionManager, +>; diff --git a/tests/integration/common/mod.rs b/tests/integration/common/mod.rs index 2d5a404..46a87b7 100644 --- a/tests/integration/common/mod.rs +++ b/tests/integration/common/mod.rs @@ -2,7 +2,6 @@ extern crate actix_demo; use actix_demo::{AppConfig, AppData, EnvConfig}; use actix_web::test; use actix_web::App; -use diesel::SqliteConnection; use diesel::r2d2::{self, ConnectionManager}; use std::io; @@ -72,7 +71,9 @@ pub async fn test_app() -> io::Result< }); let connspec = ":memory:"; - let manager = ConnectionManager::::new(connspec); + let manager = ConnectionManager::< + diesel_tracing::sqlite::InstrumentedSqliteConnection, + >::new(connspec); let pool = r2d2::Pool::builder().build(manager).map_err(|err| { io::Error::new( ErrorKind::Other,