feat: add diesel setup to project

image-uploads
Jef Roosens 2025-01-13 13:06:36 +01:00
parent faeebf8376
commit 18a321853a
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
14 changed files with 203 additions and 1 deletions

1
.env 100644
View File

@ -0,0 +1 @@
DATABASE_URL=data/db.sqlite3

97
Cargo.lock generated
View File

@ -340,6 +340,7 @@ dependencies = [
"axum-extra",
"chrono",
"clap",
"diesel",
"r2d2",
"r2d2_sqlite",
"rand",
@ -520,6 +521,41 @@ dependencies = [
"typenum",
]
[[package]]
name = "darling"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -535,6 +571,39 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00"
[[package]]
name = "diesel"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf1bedf64cdb9643204a36dd15b19a6ce8e7aa7f7b105868e9f1fad5ffa7d12"
dependencies = [
"diesel_derives",
"libsqlite3-sys",
"time",
]
[[package]]
name = "diesel_derives"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f2c3de51e2ba6bf2a648285696137aaf0f5f487bcbea93972fe8a364e131a4"
dependencies = [
"diesel_table_macro_syntax",
"dsl_auto_type",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "diesel_table_macro_syntax"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25"
dependencies = [
"syn",
]
[[package]]
name = "digest"
version = "0.10.7"
@ -546,6 +615,26 @@ dependencies = [
"subtle",
]
[[package]]
name = "dsl_auto_type"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5d9abe6314103864cc2d8901b7ae224e0ab1a103a0a416661b4097b0779b607"
dependencies = [
"darling",
"either",
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@ -816,6 +905,12 @@ dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "ignore"
version = "0.4.23"
@ -1911,7 +2006,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]

View File

@ -13,6 +13,7 @@ axum = { version = "0.8.0", features = ["macros"] }
axum-extra = { version = "0.10.0", features = ["cookie"] }
chrono = { version = "0.4.39", features = ["serde"] }
clap = { version = "4.5.26", features = ["derive", "env"] }
diesel = { version = "2.2.6", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
r2d2 = "0.8.10"
r2d2_sqlite = "0.25.0"
rand = "0.8.5"

9
diesel.toml 100644
View File

@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/db/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
[migrations_directory]
dir = "/home/jef/dev/calathea/migrations"

View File

@ -0,0 +1 @@
drop table plants;

View File

@ -0,0 +1,6 @@
create table plants (
id integer primary key not null,
name text not null,
species text not null,
description text not null
);

View File

@ -0,0 +1 @@
drop table comments;

View File

@ -0,0 +1,5 @@
create table comments (
id integer primary key not null,
plant_id integer references plants (id),
comment text not null
);

View File

@ -0,0 +1 @@
drop table events;

View File

@ -0,0 +1,9 @@
create table events (
id integer primary key not null,
plant_id integer not null
references plants (id)
on delete cascade,
event_type text not null,
date text not null,
description text not null
);

View File

@ -0,0 +1,2 @@
drop table sessions;
drop table users;

View File

@ -0,0 +1,14 @@
create table users (
id integer primary key not null,
username text unique not null,
password_hash text not null,
admin boolean not null
);
create table sessions (
id integer primary key not null,
user_id integer not null
references users (id)
on delete cascade,
unique (id, user_id)
);

View File

@ -1,6 +1,7 @@
mod comment;
mod event;
mod plant;
mod schema;
mod session;
mod user;

56
src/db/schema.rs 100644
View File

@ -0,0 +1,56 @@
// @generated automatically by Diesel CLI.
diesel::table! {
comments (id) {
id -> Integer,
plant_id -> Nullable<Integer>,
comment -> Text,
}
}
diesel::table! {
events (id) {
id -> Integer,
plant_id -> Integer,
event_type -> Text,
date -> Text,
description -> Text,
}
}
diesel::table! {
plants (id) {
id -> Integer,
name -> Text,
species -> Text,
description -> Text,
}
}
diesel::table! {
sessions (id) {
id -> Integer,
user_id -> Integer,
}
}
diesel::table! {
users (id) {
id -> Integer,
username -> Text,
password_hash -> Text,
admin -> Bool,
}
}
diesel::joinable!(comments -> plants (plant_id));
diesel::joinable!(events -> plants (plant_id));
diesel::joinable!(sessions -> users (user_id));
diesel::allow_tables_to_appear_in_same_query!(
comments,
events,
plants,
sessions,
users,
);