It do be compiling

This commit is contained in:
Nadja Reitzenstein 2023-04-30 00:46:45 +02:00
parent a4f6c77b26
commit 187a9f6781
14 changed files with 274 additions and 41 deletions

137
Cargo.lock generated
View File

@ -1040,6 +1040,40 @@ dependencies = [
"syn",
]
[[package]]
name = "diesel"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72eb77396836a4505da85bae0712fa324b74acfe1876d7c2f7e694ef3d0ee373"
dependencies = [
"diesel_derives",
"libsqlite3-sys",
"r2d2",
]
[[package]]
name = "diesel_derives"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ad74fdcf086be3d4fdd142f67937678fe60ed431c3b2f08599e7687269410c4"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "diesel_migrations"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9ae22beef5e9d6fab9225ddb073c1c6c1a7a6ded5019d5da11d1e5c5adc34e2"
dependencies = [
"diesel",
"migrations_internals",
"migrations_macros",
]
[[package]]
name = "diflouroborane"
version = "0.4.2"
@ -1059,6 +1093,8 @@ dependencies = [
"clap 3.2.23",
"console",
"desfire",
"diesel",
"diesel_migrations",
"dirs",
"erased-serde",
"executor",
@ -1085,7 +1121,6 @@ dependencies = [
"rkyv_typename",
"rsasl",
"rumqttc",
"rusqlite",
"rust-argon2",
"rustls",
"rustls-native-certs",
@ -1216,18 +1251,6 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fastrand"
version = "1.8.0"
@ -1527,15 +1550,6 @@ dependencies = [
"ahash",
]
[[package]]
name = "hashlink"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69fe1fcf8b4278d860ad0548329f892a3631fb63f82574df68275f34cdbe0ffa"
dependencies = [
"hashbrown",
]
[[package]]
name = "hdrhistogram"
version = "7.5.2"
@ -2000,6 +2014,27 @@ dependencies = [
"syn",
]
[[package]]
name = "migrations_internals"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c493c09323068c01e54c685f7da41a9ccf9219735c3766fbfd6099806ea08fbc"
dependencies = [
"serde",
"toml",
]
[[package]]
name = "migrations_macros"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a8ff27a350511de30cdabb77147501c36ef02e0451d957abea2f30caffb2b58"
dependencies = [
"migrations_internals",
"proc-macro2",
"quote",
]
[[package]]
name = "mime"
version = "0.3.16"
@ -2362,6 +2397,30 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.51"
@ -2433,6 +2492,17 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "r2d2"
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93"
dependencies = [
"log",
"parking_lot",
"scheduled-thread-pool",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -2665,20 +2735,6 @@ dependencies = [
"webpki",
]
[[package]]
name = "rusqlite"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01e213bc3ecb39ac32e81e51ebe31fd888a940515173e3a18a35f8c6e896422a"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]]
name = "rust-argon2"
version = "0.8.3"
@ -2763,6 +2819,15 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "scheduled-thread-pool"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19"
dependencies = [
"parking_lot",
]
[[package]]
name = "scopeguard"
version = "1.1.0"

View File

@ -68,6 +68,8 @@ rkyv_dyn = "0.7"
inventory = "0.1"
linkme = "0.2.10"
chrono = { version = "0.4", features = ["serde"] }
diesel = { version = "2.0.4", features = ["r2d2", "sqlite"] }
diesel_migrations = "2.0.0"
# Password hashing for internal users
rust-argon2 = "0.8.3"
@ -112,13 +114,15 @@ rustls-native-certs = "0.6.1"
shadow-rs = "0.11"
rusqlite = "0.28"
[dependencies.rsasl]
version = "2.0.0"
default_features = false
features = ["unstable_custom_mechanism", "provider", "registry_static", "config_builder", "plain"]
[features]
default = ["sqlite"]
sqlite = ["diesel/sqlite"]
[dev-dependencies]
futures-test = "0.3.16"
tempfile = "3.2"

@ -1 +1 @@
Subproject commit 19f20f5154f0eced6288ff56cac840025ee51da1
Subproject commit cde4677575f8e133ac764663e131c80fc891d545

View File

@ -16,6 +16,7 @@ pub mod config;
/// Internal Databases build on top of LMDB, a mmap()'ed B-tree DB optimized for reads
pub mod db;
pub mod sql;
/// Shared error type
pub mod error;

5
bffhd/sql/mod.rs Normal file
View File

@ -0,0 +1,5 @@
mod schema;
mod models;
#[cfg(feature = "sqlite")]
pub mod sqlite;

16
bffhd/sql/models.rs Normal file
View File

@ -0,0 +1,16 @@
use diesel::{Insertable, Queryable};
use super::schema::users;
#[derive(Queryable)]
pub struct User {
pub id: i32,
pub name: String,
pub password: Option<String>,
}
#[derive(Insertable)]
#[diesel(table_name = users)]
pub struct NewUser<'a> {
pub name: &'a str,
pub password: Option<&'a str>,
}

35
bffhd/sql/schema.rs Normal file
View File

@ -0,0 +1,35 @@
// @generated automatically by Diesel CLI.
diesel::table! {
user_kv (id) {
id -> Integer,
user -> Nullable<Integer>,
key -> Text,
value -> Binary,
}
}
diesel::table! {
user_roles (id) {
id -> Integer,
user -> Nullable<Integer>,
role -> Text,
}
}
diesel::table! {
users (id) {
id -> Integer,
name -> Text,
password -> Nullable<Text>,
}
}
diesel::joinable!(user_kv -> users (user));
diesel::joinable!(user_roles -> users (user));
diesel::allow_tables_to_appear_in_same_query!(
user_kv,
user_roles,
users,
);

76
bffhd/sql/sqlite.rs Normal file
View File

@ -0,0 +1,76 @@
use std::error::Error;
use thiserror::Error;
use diesel::{Connection, ExpressionMethods, Insertable, OptionalExtension, QueryDsl, RunQueryDsl, SqliteConnection};
use diesel::associations::HasTable;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sqlite::Sqlite;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use crate::sql::models::{NewUser, User};
use crate::sql::schema::users::password;
use crate::users::UserDb;
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
pub struct SqliteUserDB {
connection: Pool<ConnectionManager<SqliteConnection>>,
}
#[derive(Debug, Error)]
pub enum SqliteError {
#[error("failed to open new connection")]
PoolError(#[from] #[source] diesel::r2d2::PoolError),
#[error("failed to execute SQL commands")]
SqlError(#[from] #[source] diesel::r2d2::Error),
#[error("failed to apply migration")]
MigrationError(#[source] Box<dyn Error + Send + Sync>),
}
impl SqliteUserDB {
fn new(connection: Pool<ConnectionManager<SqliteConnection>>) -> Self {
Self { connection }
}
fn run_migrations(db: &mut impl MigrationHarness<Sqlite>) -> Result<(), SqliteError> {
let applied_migrations = db
.run_pending_migrations(MIGRATIONS)
.map_err(SqliteError::MigrationError)?;
for version in applied_migrations {
tracing::info!(?version, "applied migration");
}
Ok(())
}
pub fn setup(url: impl Into<String>) -> Result<Self, SqliteError> {
let mut manager = ConnectionManager::<SqliteConnection>::new(url);
let pool = Pool::builder()
.test_on_check_out(true)
.build(manager)?;
let mut conn = pool.get()?;
Self::run_migrations(&mut conn)?;
Ok(Self::new(pool))
}
pub fn lookup(&self, uid: &str) -> Result<Option<User>, SqliteError> {
use super::schema::users::dsl::*;
let mut conn = self.connection.get()?;
users
.filter(name.eq(uid))
.first(&mut conn)
.optional()
.map_err(|e| SqliteError::SqlError(diesel::r2d2::Error::QueryError(e)))
}
pub fn insert(&self, new_user: NewUser) -> Result<(), SqliteError> {
use super::schema::users::dsl::*;
let mut conn = self.connection.get()?;
new_user
.insert_into(users)
.execute(&mut conn)
.map(|_| ())
.map_err(|e| SqliteError::SqlError(diesel::r2d2::Error::QueryError(e)))
}
}

View File

@ -13,9 +13,9 @@ use std::path::Path;
use std::sync::Arc;
use thiserror::Error;
use crate::sql::sqlite::SqliteUserDB;
pub mod db;
mod sqlite;
use crate::users::db::UserData;
use crate::UserDB;

View File

@ -1,4 +1,5 @@
fn main() {
// Extract build-time information using the `shadow-rs` crate
shadow_rs::new();
println!("cargo:rerun-if-changed=migrations");
}

8
diesel.toml Normal file
View File

@ -0,0 +1,8 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "bffhd/sql/schema.rs"
[migrations_directory]
dir = "migrations"

View File

@ -0,0 +1,3 @@
DROP TABLE user_roles;
DROP TABLE user_kv;
DROP TABLE users;

View File

@ -0,0 +1,20 @@
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
password TEXT
);
CREATE TABLE IF NOT EXISTS user_kv (
id INTEGER PRIMARY KEY NOT NULL,
user INTEGER NOT NULL,
key TEXT NOT NULL,
value BLOB NOT NULL,
FOREIGN KEY (user) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS user_roles (
id INTEGER PRIMARY KEY NOT NULL,
user INTEGER NOT NULL,
role TEXT NOT NULL,
FOREIGN KEY (user) REFERENCES users(id)
);

View File

@ -6,7 +6,6 @@ use crate::state::*;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::marker::{PhantomData, Unpin};
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::ptr::NonNull;
use std::sync::atomic::Ordering;