From a323e5f2a1f993840dfce0da3d58b21f7691e960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Mon, 6 May 2024 21:43:48 +0200 Subject: [PATCH] db agnostic --- .gitignore | 1 + Cargo.lock | 108 ++++++++------- Cargo.toml | 2 +- init-postgres.sh | 10 ++ src/components/mod.rs | 14 +- src/components/trips/todos/mod.rs | 50 +++---- src/{sqlite.rs => db/mod.rs} | 74 ++--------- src/db/postgres.rs | 1 + src/db/sqlite.rs | 59 ++++++++ src/elements/list.rs | 2 +- src/lib.rs | 4 +- src/main.rs | 21 +-- src/models/inventory.rs | 94 ++++++------- src/models/trips/mod.rs | 214 +++++++++++++++--------------- src/models/user.rs | 16 +-- 15 files changed, 351 insertions(+), 319 deletions(-) create mode 100755 init-postgres.sh rename src/{sqlite.rs => db/mod.rs} (80%) create mode 100644 src/db/postgres.rs create mode 100644 src/db/sqlite.rs diff --git a/.gitignore b/.gitignore index bd04656..1c4e09f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.sqlite-wal *.sqlite-shm *.sqlite-journal +/pgdata diff --git a/Cargo.lock b/Cargo.lock index f2d65cc..51ebf81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,47 +47,48 @@ checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "anstream" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" dependencies = [ "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.2" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", "windows-sys 0.52.0", @@ -95,9 +96,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.82" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" +checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3" [[package]] name = "async-stream" @@ -143,9 +144,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "axum" @@ -305,9 +306,9 @@ checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "base64" -version = "0.22.0" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64ct" @@ -359,9 +360,9 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" -version = "1.0.95" +version = "1.0.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4" [[package]] name = "cfg-if" @@ -411,9 +412,9 @@ checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "colorchoice" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "console-api" @@ -629,9 +630,9 @@ checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6" [[package]] name = "flate2" -version = "1.0.29" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4556222738635b7a3417ae6130d8f52201e45a0c4d1a907f0826383adb5f85e7" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" dependencies = [ "crc32fast", "miniz_oxide", @@ -775,9 +776,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", @@ -842,9 +843,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash", "allocator-api2", @@ -856,7 +857,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -1100,7 +1101,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -1109,6 +1110,12 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.12.1" @@ -1144,9 +1151,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.153" +version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" [[package]] name = "libm" @@ -1277,7 +1284,7 @@ checksum = "8b07a5eb561b8cbc16be2d216faf7757f9baf3bfb94dbb0fae3df8387a5bb47f" dependencies = [ "crossbeam-epoch", "crossbeam-utils", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "metrics", "num_cpus", "quanta", @@ -1370,9 +1377,9 @@ dependencies = [ [[package]] name = "num-iter" -version = "0.1.44" +version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d869c01cc0c455284163fd0092f1f93835385ccab5a98a0dcc497b2f8bf055a9" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" dependencies = [ "autocfg", "num-integer", @@ -1381,9 +1388,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", @@ -1510,7 +1517,7 @@ dependencies = [ "async-trait", "axum 0.7.5", "axum-prometheus", - "base64 0.22.0", + "base64 0.22.1", "clap", "console-subscriber", "futures", @@ -1780,9 +1787,9 @@ dependencies = [ [[package]] name = "raw-cpuid" -version = "11.0.1" +version = "11.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d86a7c4638d42c44551f4791a20e687dbb4c3de1f33c43dd71e355cd429def1" +checksum = "e29830cbb1290e404f24c73af91c5d8d631ce7e128691e9477556b540cd01ecd" dependencies = [ "bitflags 2.5.0", ] @@ -1963,18 +1970,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.199" +version = "1.0.200" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" +checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.199" +version = "1.0.200" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" +checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" dependencies = [ "proc-macro2", "quote", @@ -2087,9 +2094,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -2529,16 +2536,15 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" dependencies = [ "bytes", "futures-core", "futures-sink", "pin-project-lite", "tokio", - "tracing", ] [[package]] @@ -3114,18 +3120,18 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "zerocopy" -version = "0.7.32" +version = "0.7.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +checksum = "087eca3c1eaf8c47b94d02790dd086cd594b912d2043d4de4bfdd466b3befb7c" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.32" +version = "0.7.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +checksum = "6f4b6c273f496d8fd4eaf18853e6b448760225dc030ff2c485a786859aea6393" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index a0dbd1c..89bad8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,8 +100,8 @@ version = "0.7" #path = "./vendor/sqlx" features = [ "runtime-tokio-rustls", - # "offline", "sqlite", + "postgres", "macros", "time", "migrate", diff --git a/init-postgres.sh b/init-postgres.sh new file mode 100755 index 0000000..2f5fbbf --- /dev/null +++ b/init-postgres.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -o nounset + +if [[ ! -e "./pgdata" ]] ; then + initdb --locale=C.UTF-8 --encoding=UTF8 -D './pgdata' --user postgres + mkdir ./pgdata/run +fi + +postgres -D ./pgdata -k run -h "" diff --git a/src/components/mod.rs b/src/components/mod.rs index 76279c2..1598c91 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -3,7 +3,7 @@ pub mod trips; pub mod crud { use async_trait::async_trait; - use crate::{models::Error, sqlite, Context}; + use crate::{db, models::Error, Context}; #[async_trait] pub trait Create: Sized { @@ -15,7 +15,7 @@ pub mod crud { async fn create( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, container: Self::Container, info: Self::Info, ) -> Result; @@ -28,13 +28,13 @@ pub mod crud { async fn findall( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, container: Self::Container, ) -> Result, Error>; async fn find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Self::Reference, ) -> Result, Error>; } @@ -46,7 +46,7 @@ pub mod crud { async fn update( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Self::Reference, update: Self::UpdateElement, ) -> Result, Error>; @@ -84,7 +84,7 @@ pub mod crud { async fn delete_all<'c>( ctx: &Context, - pool: &'c sqlite::Pool, + pool: &'c db::Pool, container: Self::Container, ids: Vec, ) -> Result { @@ -112,7 +112,7 @@ pub mod crud { async fn set( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Self::Reference, value: bool, ) -> Result<(), crate::Error>; diff --git a/src/components/trips/todos/mod.rs b/src/components/trips/todos/mod.rs index 5fe728c..ff64e9e 100644 --- a/src/components/trips/todos/mod.rs +++ b/src/components/trips/todos/mod.rs @@ -20,10 +20,10 @@ use crate::{ route::{self, Toggle}, view::{self, View}, }, - htmx, + db, htmx, models::{user::User, Error}, routing::get_referer, - sqlite, AppState, Context, RequestError, + AppState, Context, RequestError, }; use async_trait::async_trait; @@ -140,16 +140,16 @@ impl crud::Read for Todo { async fn findall( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, container: Container, ) -> Result, Error> { let trip_id_param = container.trip_id.to_string(); let user_id = ctx.user.id.to_string(); let todos: Vec = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Todo, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Todo, }, pool, TodoRow, @@ -177,16 +177,16 @@ impl crud::Read for Todo { #[tracing::instrument] async fn find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Reference, ) -> Result, Error> { let trip_id_param = reference.container.trip_id.to_string(); let todo_id_param = reference.id.0.to_string(); let user_id = ctx.user.id.to_string(); crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Todo, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Todo, }, pool, TodoRow, @@ -228,7 +228,7 @@ impl crud::Create for Todo { async fn create( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, container: Self::Container, info: Self::Info, ) -> Result { @@ -238,9 +238,9 @@ impl crud::Create for Todo { let id_param = id.to_string(); let trip_id_param = container.trip_id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Todo, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Todo, }, pool, r#" @@ -305,7 +305,7 @@ impl crud::Update for Todo { #[tracing::instrument] async fn update( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Self::Reference, update_element: Self::UpdateElement, ) -> Result, Error> { @@ -317,9 +317,9 @@ impl crud::Update for Todo { let done = state == State::Done.into(); let result = crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, TodoRow, @@ -351,9 +351,9 @@ impl crud::Update for Todo { let todo_id_param = reference.id.to_string(); let result = crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Todo, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Todo, }, pool, TodoRow, @@ -400,9 +400,9 @@ impl crud::Delete for Todo { let trip_id_param = reference.container.trip_id.to_string(); let results = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Delete, - component: sqlite::Component::Todo, + &db::QueryClassification { + query_type: db::QueryType::Delete, + component: db::Component::Todo, }, &mut *(db.acquire().await?), r#" @@ -945,7 +945,7 @@ impl crud::Toggle for StateUpdate { async fn set( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, reference: Self::Reference, value: bool, ) -> Result<(), crate::Error> { diff --git a/src/sqlite.rs b/src/db/mod.rs similarity index 80% rename from src/sqlite.rs rename to src/db/mod.rs index 5d8ac80..a9cbee4 100644 --- a/src/sqlite.rs +++ b/src/db/mod.rs @@ -1,68 +1,12 @@ -use std::fmt; -use std::time; - use base64::Engine as _; use sha2::{Digest, Sha256}; -use tracing::Instrument; +use std::fmt; -use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; -use sqlx::ConnectOptions; -pub use sqlx::{Pool as SqlitePool, Sqlite}; - -use std::str::FromStr as _; - -pub use sqlx::Type; - -use crate::StartError; +pub mod postgres; +pub mod sqlite; pub type Pool = sqlx::Pool; -pub fn int_to_bool(value: i32) -> bool { - match value { - 0 => false, - 1 => true, - _ => panic!("got invalid boolean from sqlite"), - } -} - -#[tracing::instrument] -pub async fn init_database_pool(url: &str) -> Result { - async { - SqlitePoolOptions::new() - .max_connections(5) - .connect_with( - SqliteConnectOptions::from_str(url)? - .log_statements(log::LevelFilter::Debug) - .log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100)) - .pragma("foreign_keys", "1"), - ) - .await - } - .instrument(tracing::info_span!("packager::sql::pool")) - .await - .map_err(Into::into) -} - -#[tracing::instrument] -pub async fn migrate(url: &str) -> Result<(), StartError> { - async { - let pool = SqlitePoolOptions::new() - .max_connections(5) - .connect_with( - SqliteConnectOptions::from_str(url)? - .pragma("foreign_keys", "0") - .log_statements(log::LevelFilter::Debug), - ) - .await?; - - sqlx::migrate!().run(&pool).await - } - .instrument(tracing::info_span!("packager::sql::migrate")) - .await?; - - Ok(()) -} - pub enum QueryType { Insert, Update, @@ -171,7 +115,7 @@ macro_rules! query_all { use tracing::Instrument as _; use futures::TryStreamExt as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: Result, Error> = sqlx::query_as!( $struct_row, $query, @@ -197,7 +141,7 @@ macro_rules! query_one { { use tracing::Instrument as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: Result, Error> = sqlx::query_as!( $struct_row, $query, @@ -221,7 +165,7 @@ macro_rules! query_exists { { use tracing::Instrument as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: bool = sqlx::query!( $query, $( $args )* @@ -250,7 +194,7 @@ macro_rules! execute { { use tracing::Instrument as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: Result = sqlx::query!( $query, $( $args )* @@ -298,7 +242,7 @@ macro_rules! execute_returning { use tracing::Instrument as _; use futures::TryFutureExt as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: Result<$t, Error> = sqlx::query!( $query, $( $args )* @@ -323,7 +267,7 @@ macro_rules! execute_returning_uuid { use tracing::Instrument as _; use futures::TryFutureExt as _; async { - $crate::sqlite::sqlx_query($class, $query, &[]); + $crate::db::sqlx_query($class, $query, &[]); let result: Result = sqlx::query!( $query, $( $args )* diff --git a/src/db/postgres.rs b/src/db/postgres.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/db/postgres.rs @@ -0,0 +1 @@ + diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs new file mode 100644 index 0000000..33f1acf --- /dev/null +++ b/src/db/sqlite.rs @@ -0,0 +1,59 @@ +use std::time; + +use tracing::Instrument; + +use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; +use sqlx::ConnectOptions; +pub use sqlx::{Pool as SqlitePool, Sqlite}; + +use std::str::FromStr as _; + +pub use sqlx::Type; + +use crate::StartError; + +pub fn int_to_bool(value: i32) -> bool { + match value { + 0 => false, + 1 => true, + _ => panic!("got invalid boolean from sqlite"), + } +} + +#[tracing::instrument] +pub async fn init_database_pool(url: &str) -> Result, StartError> { + async { + SqlitePoolOptions::new() + .max_connections(5) + .connect_with( + SqliteConnectOptions::from_str(url)? + .log_statements(log::LevelFilter::Debug) + .log_slow_statements(log::LevelFilter::Warn, time::Duration::from_millis(100)) + .pragma("foreign_keys", "1"), + ) + .await + } + .instrument(tracing::info_span!("packager::sql::pool")) + .await + .map_err(Into::into) +} + +#[tracing::instrument] +pub async fn migrate(url: &str) -> Result<(), StartError> { + async { + let pool = SqlitePoolOptions::new() + .max_connections(5) + .connect_with( + SqliteConnectOptions::from_str(url)? + .pragma("foreign_keys", "0") + .log_statements(log::LevelFilter::Debug), + ) + .await?; + + sqlx::migrate!().run(&pool).await + } + .instrument(tracing::info_span!("packager::sql::migrate")) + .await?; + + Ok(()) +} diff --git a/src/elements/list.rs b/src/elements/list.rs index ea5679a..33c8979 100644 --- a/src/elements/list.rs +++ b/src/elements/list.rs @@ -24,7 +24,7 @@ pub struct Cell<'a> { } impl<'a> Cell<'a> { - fn render(self, is_edit: bool) -> Markup { + fn render(self, _is_edit: bool) -> Markup { match self.cell_type { CellType::Text(text) => html!( td diff --git a/src/lib.rs b/src/lib.rs index 09edea1..313f5a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,12 +5,12 @@ use std::fmt; pub mod auth; pub mod cli; pub mod components; +pub mod db; pub mod elements; pub mod error; pub mod htmx; pub mod models; pub mod routing; -pub mod sqlite; pub mod telemetry; mod view; @@ -19,7 +19,7 @@ pub use error::{AuthError, CommandError, Error, RequestError, StartError}; #[derive(Clone, Debug)] pub struct AppState { - pub database_pool: sqlite::Pool, + pub database_pool: db::Pool, pub client_state: ClientState, pub auth_config: auth::Config, } diff --git a/src/main.rs b/src/main.rs index 5cd2eec..97e15d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::process::ExitCode; use std::str::FromStr; use packager::{ - auth, cli, models, routing, sqlite, telemetry, AppState, ClientState, Error, StartError, + auth, cli, db, models, routing, telemetry, AppState, ClientState, Error, StartError, }; use tokio::net::TcpListener; @@ -59,12 +59,12 @@ async fn main() -> MainResult { Box::pin(async move { match args.command { cli::Command::Serve(serve_args) => { - if let Err(e) = sqlite::migrate(&args.database_url).await { + if let Err(e) = db::sqlite::migrate(&args.database_url).await { return <_ as Into>::into(e).into(); } let database_pool = - match sqlite::init_database_pool(&args.database_url).await { + match db::sqlite::init_database_pool(&args.database_url).await { Ok(pool) => pool, Err(e) => return <_ as Into>::into(e).into(), }; @@ -152,11 +152,14 @@ async fn main() -> MainResult { cli::Command::Admin(admin_command) => match admin_command { cli::Admin::User(cmd) => match cmd { cli::UserCommand::Create(user) => { - let database_pool = - match sqlite::init_database_pool(&args.database_url).await { - Ok(pool) => pool, - Err(e) => return <_ as Into>::into(e).into(), - }; + let database_pool = match db::sqlite::init_database_pool( + &args.database_url, + ) + .await + { + Ok(pool) => pool, + Err(e) => return <_ as Into>::into(e).into(), + }; let id = match models::user::create( &database_pool, @@ -188,7 +191,7 @@ async fn main() -> MainResult { }, }, cli::Command::Migrate => { - if let Err(e) = sqlite::migrate(&args.database_url).await { + if let Err(e) = db::sqlite::migrate(&args.database_url).await { return <_ as Into>::into(e).into(); } diff --git a/src/models/inventory.rs b/src/models/inventory.rs index a80292f..716482a 100644 --- a/src/models/inventory.rs +++ b/src/models/inventory.rs @@ -1,5 +1,5 @@ use super::Error; -use crate::{sqlite, Context}; +use crate::{db, Context}; use uuid::Uuid; @@ -9,13 +9,13 @@ pub struct Inventory { impl Inventory { #[tracing::instrument] - pub async fn load(ctx: &Context, pool: &sqlite::Pool) -> Result { + pub async fn load(ctx: &Context, pool: &db::Pool) -> Result { let user_id = ctx.user.id.to_string(); let mut categories = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, DbCategoryRow, @@ -69,15 +69,15 @@ impl Category { #[tracing::instrument] pub async fn _find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, ) -> Result, Error> { let id_param = id.to_string(); let user_id = ctx.user.id.to_string(); crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, DbCategoryRow, @@ -97,14 +97,14 @@ impl Category { } #[tracing::instrument] - pub async fn save(ctx: &Context, pool: &sqlite::Pool, name: &str) -> Result { + pub async fn save(ctx: &Context, pool: &db::Pool, name: &str) -> Result { let id = Uuid::new_v4(); let id_param = id.to_string(); let user_id = ctx.user.id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Inventory, }, pool, "INSERT INTO inventory_items_categories @@ -136,14 +136,14 @@ impl Category { pub async fn populate_items( &mut self, ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, ) -> Result<(), Error> { let id = self.id.to_string(); let user_id = ctx.user.id.to_string(); let items = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, DbInventoryItemsRow, @@ -232,14 +232,18 @@ impl TryFrom for InventoryItem { impl InventoryItem { #[tracing::instrument] - pub async fn find(ctx: &Context, pool: &sqlite::Pool, id: Uuid) -> Result, Error> { + pub async fn find( + ctx: &Context, + pool: &db::Pool, + id: Uuid, + ) -> Result, Error> { let id_param = id.to_string(); let user_id = ctx.user.id.to_string(); crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, DbInventoryItemRow, @@ -273,14 +277,14 @@ impl InventoryItem { #[tracing::instrument] pub async fn name_exists( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, name: &str, ) -> Result { let user_id = ctx.user.id.to_string(); crate::query_exists!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, "SELECT id @@ -295,13 +299,13 @@ impl InventoryItem { } #[tracing::instrument] - pub async fn delete(ctx: &Context, pool: &sqlite::Pool, id: Uuid) -> Result { + pub async fn delete(ctx: &Context, pool: &db::Pool, id: Uuid) -> Result { let id_param = id.to_string(); let user_id = ctx.user.id.to_string(); let results = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Delete, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Delete, + component: db::Component::Inventory, }, pool, "DELETE FROM inventory_items @@ -319,7 +323,7 @@ impl InventoryItem { #[tracing::instrument] pub async fn update( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, name: &str, weight: u32, @@ -329,9 +333,9 @@ impl InventoryItem { let id_param = id.to_string(); crate::execute_returning_uuid!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Inventory, }, pool, "UPDATE inventory_items AS item @@ -354,7 +358,7 @@ impl InventoryItem { #[tracing::instrument] pub async fn save( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, name: &str, category_id: Uuid, weight: u32, @@ -365,9 +369,9 @@ impl InventoryItem { let category_id_param = category_id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Inventory, }, pool, "INSERT INTO inventory_items @@ -389,15 +393,15 @@ impl InventoryItem { #[tracing::instrument] pub async fn get_category_max_weight( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, category_id: Uuid, ) -> Result { let user_id = ctx.user.id.to_string(); let category_id_param = category_id.to_string(); let weight = crate::execute_returning!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, " @@ -455,15 +459,15 @@ impl Item { #[tracing::instrument] pub async fn _get_category_total_picked_weight( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, category_id: Uuid, ) -> Result { let user_id = ctx.user.id.to_string(); let category_id_param = category_id.to_string(); crate::execute_returning!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Inventory, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Inventory, }, pool, " diff --git a/src/models/trips/mod.rs b/src/models/trips/mod.rs index ff315eb..39e519b 100644 --- a/src/models/trips/mod.rs +++ b/src/models/trips/mod.rs @@ -8,7 +8,7 @@ use super::{ inventory, }; -use crate::{sqlite, Context}; +use crate::{db, Context}; use serde::{Deserialize, Serialize}; use time; @@ -38,7 +38,7 @@ use uuid::Uuid; // } // } -#[derive(sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)] +#[derive(db::sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)] pub enum TripState { Init, Planning, @@ -156,7 +156,7 @@ impl TripCategory { #[tracing::instrument] pub async fn find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, category_id: Uuid, ) -> Result, Error> { @@ -222,9 +222,9 @@ impl TripCategory { } let mut rows = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, Row, @@ -346,7 +346,7 @@ impl TripItem { #[tracing::instrument] pub async fn find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, item_id: Uuid, ) -> Result, Error> { @@ -354,9 +354,9 @@ impl TripItem { let item_id_param = item_id.to_string(); let trip_id_param = trip_id.to_string(); crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, DbTripsItemsRow, @@ -389,7 +389,7 @@ impl TripItem { #[tracing::instrument] pub async fn set_state( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, item_id: Uuid, key: TripItemStateKey, @@ -401,9 +401,9 @@ impl TripItem { let result = match key { TripItemStateKey::Pick => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips_items @@ -420,9 +420,9 @@ impl TripItem { } TripItemStateKey::Pack => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips_items @@ -439,9 +439,9 @@ impl TripItem { } TripItemStateKey::Ready => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips_items @@ -533,12 +533,12 @@ pub enum TripAttribute { impl Trip { #[tracing::instrument] - pub async fn all(ctx: &Context, pool: &sqlite::Pool) -> Result, Error> { + pub async fn all(ctx: &Context, pool: &db::Pool) -> Result, Error> { let user_id = ctx.user.id.to_string(); let mut trips = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, DbTripRow, @@ -566,15 +566,15 @@ impl Trip { #[tracing::instrument] pub async fn find( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, ) -> Result, Error> { let trip_id_param = trip_id.to_string(); let user_id = ctx.user.id.to_string(); crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, DbTripRow, @@ -600,7 +600,7 @@ impl Trip { #[tracing::instrument] pub async fn trip_type_remove( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, type_id: Uuid, ) -> Result { @@ -609,9 +609,9 @@ impl Trip { let type_id_param = type_id.to_string(); let results = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Delete, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Delete, + component: db::Component::Trips, }, pool, "DELETE FROM trips_to_trips_types AS ttt @@ -635,7 +635,7 @@ impl Trip { #[tracing::instrument] pub async fn trip_type_add( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, type_id: Uuid, ) -> Result<(), Error> { @@ -645,9 +645,9 @@ impl Trip { let type_id_param = type_id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, pool, "INSERT INTO @@ -673,16 +673,16 @@ impl Trip { #[tracing::instrument] pub async fn set_state( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, new_state: &TripState, ) -> Result { let user_id = ctx.user.id.to_string(); let trip_id_param = id.to_string(); let result = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -700,16 +700,16 @@ impl Trip { #[tracing::instrument] pub async fn set_comment( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, new_comment: &str, ) -> Result { let user_id = ctx.user.id.to_string(); let trip_id_param = id.to_string(); let result = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -727,7 +727,7 @@ impl Trip { #[tracing::instrument] pub async fn set_attribute( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, attribute: TripAttribute, value: &str, @@ -737,9 +737,9 @@ impl Trip { let result = match attribute { TripAttribute::Name => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -754,9 +754,9 @@ impl Trip { TripAttribute::DateStart => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -770,9 +770,9 @@ impl Trip { } TripAttribute::DateEnd => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -786,9 +786,9 @@ impl Trip { } TripAttribute::Location => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -802,9 +802,9 @@ impl Trip { } TripAttribute::TempMin => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -818,9 +818,9 @@ impl Trip { } TripAttribute::TempMax => { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips @@ -844,7 +844,7 @@ impl Trip { #[tracing::instrument] pub async fn save( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, name: &str, date_start: time::Date, date_end: time::Date, @@ -861,9 +861,9 @@ impl Trip { let mut transaction = pool.begin().await?; crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, &mut *transaction, "INSERT INTO trips @@ -882,9 +882,9 @@ impl Trip { if let Some(copy_from_trip_id) = copy_from { let copy_from_trip_id_param = copy_from_trip_id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, &mut *transaction, r#"INSERT INTO trips_items ( @@ -912,9 +912,9 @@ impl Trip { .await?; } else { crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, &mut *transaction, r#"INSERT INTO trips_items ( @@ -949,15 +949,15 @@ impl Trip { #[tracing::instrument] pub async fn find_total_picked_weight( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, trip_id: Uuid, ) -> Result { let user_id = ctx.user.id.to_string(); let trip_id_param = trip_id.to_string(); let weight = crate::execute_returning!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, " @@ -1018,7 +1018,11 @@ impl Trip { } #[tracing::instrument] - pub async fn load_todos(&mut self, ctx: &Context, pool: &sqlite::Pool) -> Result<(), Error> { + pub async fn load_todos( + &mut self, + ctx: &Context, + pool: &db::Pool, + ) -> Result<(), Error> { self.todos = Some( crate::components::trips::todos::Todo::findall( ctx, @@ -1034,14 +1038,14 @@ impl Trip { pub async fn load_trips_types( &mut self, ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, ) -> Result<(), Error> { let user_id = ctx.user.id.to_string(); let id = self.id.to_string(); let types = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, TripTypeRow, @@ -1078,7 +1082,7 @@ impl Trip { pub async fn sync_trip_items_with_inventory( &mut self, ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, ) -> Result<(), Error> { // we need to get all items that are part of the inventory but not // part of the trip items @@ -1107,9 +1111,9 @@ impl Trip { } let unsynced_items: Vec = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, Row, @@ -1137,9 +1141,9 @@ impl Trip { for unsynced_item in &unsynced_items { let item_id = unsynced_item.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, pool, " @@ -1175,7 +1179,7 @@ impl Trip { pub async fn load_categories( &mut self, ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, ) -> Result<(), Error> { let mut categories: Vec = vec![]; // we can ignore the return type as we collect into `categories` @@ -1241,9 +1245,9 @@ impl Trip { } let rows = crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, Row, @@ -1408,12 +1412,12 @@ impl TryFrom for TripType { impl TripsType { #[tracing::instrument] - pub async fn all(ctx: &Context, pool: &sqlite::Pool) -> Result, Error> { + pub async fn all(ctx: &Context, pool: &db::Pool) -> Result, Error> { let user_id = ctx.user.id.to_string(); crate::query_all!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::Trips, }, pool, DbTripsTypesRow, @@ -1429,14 +1433,14 @@ impl TripsType { } #[tracing::instrument] - pub async fn save(ctx: &Context, pool: &sqlite::Pool, name: &str) -> Result { + pub async fn save(ctx: &Context, pool: &db::Pool, name: &str) -> Result { let user_id = ctx.user.id.to_string(); let id = Uuid::new_v4(); let id_param = id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::Trips, }, pool, "INSERT INTO trips_types @@ -1455,7 +1459,7 @@ impl TripsType { #[tracing::instrument] pub async fn set_name( ctx: &Context, - pool: &sqlite::Pool, + pool: &db::Pool, id: Uuid, new_name: &str, ) -> Result { @@ -1463,9 +1467,9 @@ impl TripsType { let id_param = id.to_string(); let result = crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Update, - component: sqlite::Component::Trips, + &db::QueryClassification { + query_type: db::QueryType::Update, + component: db::Component::Trips, }, pool, "UPDATE trips_types diff --git a/src/models/user.rs b/src/models/user.rs index 0da0697..7a3a919 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -1,7 +1,7 @@ use super::Error; use uuid::Uuid; -use crate::sqlite; +use crate::db; #[derive(Debug, Clone)] pub struct User { @@ -42,9 +42,9 @@ impl User { name: &str, ) -> Result, Error> { crate::query_one!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Select, - component: sqlite::Component::User, + &db::QueryClassification { + query_type: db::QueryType::Select, + component: db::Component::User, }, pool, DbUserRow, @@ -57,14 +57,14 @@ impl User { } #[tracing::instrument] -pub async fn create(pool: &sqlite::Pool, user: NewUser<'_>) -> Result { +pub async fn create(pool: &db::Pool, user: NewUser<'_>) -> Result { let id = Uuid::new_v4(); let id_param = id.to_string(); crate::execute!( - &sqlite::QueryClassification { - query_type: sqlite::QueryType::Insert, - component: sqlite::Component::User, + &db::QueryClassification { + query_type: db::QueryType::Insert, + component: db::Component::User, }, pool, "INSERT INTO users