diff options
author | 2025-08-17 09:42:25 +0100 | |
---|---|---|
committer | 2025-08-17 10:25:53 +0100 | |
commit | 1148a73b8880fbe7bba71b1159233fddc6f59ce0 (patch) | |
tree | 373200b513fafd8a58cb774ebad1febd7bc29b26 | |
parent | e9b472eb0b7e4f832d9df96b674dc8da73b34b94 (diff) | |
download | luz-db-refactor.tar.gz luz-db-refactor.tar.bz2 luz-db-refactor.zip |
feat: db loggingdb-refactor
Diffstat (limited to '')
-rw-r--r-- | filamento/Cargo.toml | 2 | ||||
-rw-r--r-- | filamento/src/db.rs | 54 |
2 files changed, 52 insertions, 4 deletions
diff --git a/filamento/Cargo.toml b/filamento/Cargo.toml index b89c577..dea1c79 100644 --- a/filamento/Cargo.toml +++ b/filamento/Cargo.toml @@ -35,6 +35,7 @@ uuid = { workspace = true, features = ["v4"] } rusqlite = { git = "https://github.com/Spxg/rusqlite.git", branch = "wasm-demo", features = [ "uuid", "chrono", + "trace", ] } tracing = { workspace = true } chrono = { workspace = true } @@ -69,6 +70,7 @@ wasm-bindgen-futures = { workspace = true } rusqlite = { git = "https://github.com/Spxg/rusqlite.git", branch = "wasm-demo", features = [ "uuid", "chrono", + "trace", "precompiled-wasm", ] } tokio_with_wasm = { workspace = true, features = ["sync", "time", "rt"] } diff --git a/filamento/src/db.rs b/filamento/src/db.rs index 79fdd9a..0b94f0c 100644 --- a/filamento/src/db.rs +++ b/filamento/src/db.rs @@ -1,8 +1,11 @@ use core::fmt::Display; +use std::ffi::c_int; use std::{collections::HashSet, ops::Deref, path::Path, sync::Arc}; use chrono::{DateTime, Utc}; use jid::{BareJID, FullJID, JID}; +use rusqlite::trace::TraceEventCodes; +use rusqlite::trace::config_log; use rusqlite::{Connection, OptionalExtension}; use tokio::sync::{Mutex, MutexGuard}; use tokio::sync::{mpsc, oneshot}; @@ -53,6 +56,41 @@ macro_rules! impl_db_sends { } } +unsafe fn enable_log() -> Result<(), DatabaseOpenError> { + unsafe { + config_log(Some(|_, log| debug!("rusqlite (db): {}", log)))?; + } + Ok(()) +} + +fn enable_trace(connection: &mut Connection) { + connection.trace_v2( + TraceEventCodes::all(), + Some(|trace| match trace { + rusqlite::trace::TraceEvent::Stmt(stmt_ref, _) => { + debug!( + "rusqlite (db) statement: {}", + stmt_ref.expanded_sql().unwrap_or_default() + ) + } + rusqlite::trace::TraceEvent::Profile(stmt_ref, duration) => {} + rusqlite::trace::TraceEvent::Row(stmt_ref) => { + debug!( + "rusqlite (db) row: {}", + stmt_ref.expanded_sql().unwrap_or_default() + ) + } + rusqlite::trace::TraceEvent::Close(conn_ref) => { + debug!( + "rusqlite (db) connection closed: {}", + conn_ref.db_filename().unwrap_or_default() + ) + } + _ => {} + }), + ); +} + impl Db { #[cfg(not(target_arch = "wasm32"))] pub async fn create_connect_and_migrate( @@ -709,7 +747,9 @@ impl DbActor { // let db = SqlitePool::connect(&url).await?; // migrate!().run(&db).await?; // Ok(Self { db }) - let db = Connection::open(url)?; + unsafe { enable_log()? } + let mut db = Connection::open(url)?; + enable_trace(&mut db); db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { db, receiver }) } @@ -719,7 +759,9 @@ impl DbActor { pub(crate) fn new_memory( receiver: mpsc::UnboundedReceiver<DbCommand>, ) -> Result<Self, DatabaseOpenError> { - let db = Connection::open_in_memory()?; + unsafe { enable_log()? } + let mut db = Connection::open_in_memory()?; + enable_trace(&mut db); db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { db, receiver }) } @@ -729,7 +771,9 @@ impl DbActor { pub fn new_memory( receiver: mpsc::UnboundedReceiver<DbCommand>, ) -> Result<Self, DatabaseOpenError> { - let db = Connection::open("mem.db")?; + unsafe { enable_log()? } + let mut db = Connection::open("mem.db")?; + enable_trace(&mut db); db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { db, receiver }) } @@ -740,7 +784,9 @@ impl DbActor { file_name: impl AsRef<Path>, receiver: mpsc::UnboundedReceiver<DbCommand>, ) -> Result<Self, DatabaseOpenError> { - let db = Connection::open(file_name)?; + unsafe { enable_log()? } + let mut db = Connection::open(file_name)?; + enable_trace(&mut db); db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { db, receiver }) } |