a lot of early diesel stuff

This commit is contained in:
Christoph J. Scherr 2024-06-26 16:12:49 +02:00
parent c0b0d16622
commit 6c343e5cb9
12 changed files with 200 additions and 2 deletions

110
Cargo.lock generated
View File

@ -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"

1
members/diesel-demo/.env Normal file
View File

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

View File

@ -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

View File

@ -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)

Binary file not shown.

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/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
[migrations_directory]
dir = "/home/cscherr/Documents/code/rust/rs-basic/members/diesel-demo/migrations"

View File

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS `posts`;

View File

@ -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
);

View File

@ -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<SqliteConnection> {
dotenv()?;
let database_url = env::var("DATABASE_URL")?;
Ok(SqliteConnection::establish(&database_url)
.inspect_err(|e| error!("Error connecting to {}:\n{e:#?}", database_url))?)
}

View File

@ -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(())
}

View File

@ -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,
}
}