From 6c343e5cb9e6e50e94bb6e1a502f01351670421e Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 26 Jun 2024 16:12:49 +0200 Subject: [PATCH] a lot of early diesel stuff --- Cargo.lock | 110 ++++++++++++++++++ members/diesel-demo/.env | 1 + members/diesel-demo/Cargo.toml | 6 + members/diesel-demo/README.md | 19 +++ members/diesel-demo/data/dieseldemo.db | Bin 0 -> 12288 bytes members/diesel-demo/diesel.toml | 9 ++ members/diesel-demo/migrations/.keep | 0 .../2024-06-26-135835_create_posts/down.sql | 2 + .../2024-06-26-135835_create_posts/up.sql | 8 ++ members/diesel-demo/src/lib.rs | 18 +++ members/diesel-demo/src/main.rs | 17 ++- members/diesel-demo/src/schema.rs | 12 ++ 12 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 members/diesel-demo/.env create mode 100644 members/diesel-demo/README.md create mode 100644 members/diesel-demo/data/dieseldemo.db create mode 100644 members/diesel-demo/diesel.toml create mode 100644 members/diesel-demo/migrations/.keep create mode 100644 members/diesel-demo/migrations/2024-06-26-135835_create_posts/down.sql create mode 100644 members/diesel-demo/migrations/2024-06-26-135835_create_posts/up.sql create mode 100644 members/diesel-demo/src/lib.rs create mode 100644 members/diesel-demo/src/schema.rs diff --git a/Cargo.lock b/Cargo.lock index e103934..b0725e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,41 @@ dependencies = [ "regex-syntax 0.7.5", ] +[[package]] +name = "darling" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622687fe0bac72a04e5599029151f5796111b90f1baaa9b544d807a5e31cd120" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "data-encoding" version = "2.6.0" @@ -565,9 +600,52 @@ dependencies = [ "syn", ] +[[package]] +name = "diesel" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62d6dcd069e7b5fe49a302411f759d4cf1cf2c27fe798ef46fb8baefc053dd2b" +dependencies = [ + "diesel_derives", + "libsqlite3-sys", + "serde_json", + "time", + "uuid", +] + [[package]] name = "diesel-demo" version = "0.1.0" +dependencies = [ + "anyhow", + "diesel", + "dotenvy", + "libpt", + "serde", + "serde_json", +] + +[[package]] +name = "diesel_derives" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59de76a222c2b8059f789cbe07afbfd8deb8c31dd0bc2a21f85e256c1def8259" +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" @@ -591,12 +669,32 @@ dependencies = [ "syn", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "drain_filter_polyfill" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "669a445ee724c5c69b1b06fe0b63e70a1c84bc9bb7d9696cd4f4e3ec45050408" +[[package]] +name = "dsl_auto_type" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0892a17df262a24294c382f0d5997571006e7a4348b4327557c4ff1cd4a8bccc" +dependencies = [ + "darling", + "either", + "heck 0.5.0", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "echargs" version = "0.1.0" @@ -1069,6 +1167,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 = "idna" version = "0.5.0" @@ -2590,6 +2694,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d023da39d1fde5a8a3fe1f3e01ca9632ada0a63e9797de55a879d6e2236277be" + [[package]] name = "valuable" version = "0.1.0" diff --git a/members/diesel-demo/.env b/members/diesel-demo/.env new file mode 100644 index 0000000..a289be5 --- /dev/null +++ b/members/diesel-demo/.env @@ -0,0 +1 @@ +export DATABASE_URL=./data/dieseldemo.db diff --git a/members/diesel-demo/Cargo.toml b/members/diesel-demo/Cargo.toml index 5da64e2..98f0385 100644 --- a/members/diesel-demo/Cargo.toml +++ b/members/diesel-demo/Cargo.toml @@ -4,3 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow.workspace = true +diesel = { version = "2.2.1", features = ["serde_json", "sqlite", "uuid"] } +dotenvy = "0.15.7" +libpt.workspace = true +serde = { workspace = true, features = ["serde_derive"] } +serde_json.workspace = true diff --git a/members/diesel-demo/README.md b/members/diesel-demo/README.md new file mode 100644 index 0000000..0bb427a --- /dev/null +++ b/members/diesel-demo/README.md @@ -0,0 +1,19 @@ +# Diesel seems more complex + +This project uses sqlite as I can't be bothered to host a postgres instance +just for this. + +## Setup + +* Install diesel and the diesel CLI tool. +* `DATABASE_URL=./data/dieseldemo.db diesel setup` +* We create migrations up/down and fill them with sql to do something and undo something +* Alternatively, we can use a rust macro to define schemes that become sql + through magic. I like this way better for this project because I learn more + diesel and also because it avoids having to deal with incompatibilities of the + database types (maybe). + * Run `diesel migration generate --diff-schema create_posts` + +## Ressources + +* [Official Guide](https://diesel.rs/guides/getting-started) diff --git a/members/diesel-demo/data/dieseldemo.db b/members/diesel-demo/data/dieseldemo.db new file mode 100644 index 0000000000000000000000000000000000000000..85e66ea95a89564f1f2df04bf8f2e3bb6b0244d6 GIT binary patch literal 12288 zcmeI#y-ve05C?F(AV8J+v30$%l@J0UCKgOFq9P5&br7AbKuw9%50Ru5D@?oxFVjb2 zBrZy~(7pPfWJjkvNAWMy_3ynXwe&KJCt*sbtj;)R7etIPo~@m1<>B((JjtFe-xQo3 zExszXA6BX^S#??a-gE$U2tWV=5P$##AOHafKmY;|fWUtOACG*$Aq4+CPs69NHD(yu z#Ewn!{AwqmnM9*FOrzN}DXx^-N(wDWi%U;ZaUV6y^cM>oC)qpcL8$hPP|dUB7WGu8 zE7X8)<$(M$1o9hppXT@!Gw;><^0SG_<0uX=z1Rwwb2tWV=5ZG3M ivbz}O|84!fxEBN<009U<00Izz00bZa0SG`~CGZO=rEjwU literal 0 HcmV?d00001 diff --git a/members/diesel-demo/diesel.toml b/members/diesel-demo/diesel.toml new file mode 100644 index 0000000..1e81692 --- /dev/null +++ b/members/diesel-demo/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] + +[migrations_directory] +dir = "/home/cscherr/Documents/code/rust/rs-basic/members/diesel-demo/migrations" diff --git a/members/diesel-demo/migrations/.keep b/members/diesel-demo/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/members/diesel-demo/migrations/2024-06-26-135835_create_posts/down.sql b/members/diesel-demo/migrations/2024-06-26-135835_create_posts/down.sql new file mode 100644 index 0000000..ba649bc --- /dev/null +++ b/members/diesel-demo/migrations/2024-06-26-135835_create_posts/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE IF EXISTS `posts`; diff --git a/members/diesel-demo/migrations/2024-06-26-135835_create_posts/up.sql b/members/diesel-demo/migrations/2024-06-26-135835_create_posts/up.sql new file mode 100644 index 0000000..e7e843f --- /dev/null +++ b/members/diesel-demo/migrations/2024-06-26-135835_create_posts/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE `posts`( + `id` INT4 NOT NULL PRIMARY KEY, + `title` VARCHAR NOT NULL, + `body` TEXT NOT NULL, + `published` BOOL NOT NULL +); + diff --git a/members/diesel-demo/src/lib.rs b/members/diesel-demo/src/lib.rs new file mode 100644 index 0000000..6310f34 --- /dev/null +++ b/members/diesel-demo/src/lib.rs @@ -0,0 +1,18 @@ +pub mod schema; + +use std::env; + +use diesel::prelude::*; +use diesel::sqlite::SqliteConnection; + +use dotenvy::dotenv; + +use libpt::log::error; // load envars from a `.env` file + +pub fn establish_connection() -> anyhow::Result { + dotenv()?; + + let database_url = env::var("DATABASE_URL")?; + Ok(SqliteConnection::establish(&database_url) + .inspect_err(|e| error!("Error connecting to {}:\n{e:#?}", database_url))?) +} diff --git a/members/diesel-demo/src/main.rs b/members/diesel-demo/src/main.rs index e7a11a9..508a06c 100644 --- a/members/diesel-demo/src/main.rs +++ b/members/diesel-demo/src/main.rs @@ -1,3 +1,16 @@ -fn main() { - println!("Hello, world!"); +use libpt::log::{self, debug}; + +use diesel_demo as lib; + +fn main() -> anyhow::Result<()> { + let _logger = log::Logger::builder() + .max_level(log::Level::TRACE) + .uptime(true) + .build(); + debug!("logger initialized"); + + let conn = lib::establish_connection()?; + debug!("db connection established"); + + Ok(()) } diff --git a/members/diesel-demo/src/schema.rs b/members/diesel-demo/src/schema.rs new file mode 100644 index 0000000..5115db3 --- /dev/null +++ b/members/diesel-demo/src/schema.rs @@ -0,0 +1,12 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + posts (id) { + id -> Int4, // what the hell is an Int4???? I guess it must + // be a 4 byte integer (u32?), no way it would be a nibble. + title -> Varchar, + body -> Text, // What is the difference between Text and Varchar? I know Varchar + // from various DB stuff but idk Text + published -> Bool, + } +}