From 1089196226916ae2ca03606096568eadfbd4bbb3 Mon Sep 17 00:00:00 2001 From: Stefan Menner Date: Tue, 17 Jun 2025 14:46:48 +0200 Subject: [PATCH] Update --- .gitignore | 8 ++--- .vscode/settings.json | 4 +++ Cargo.toml | 5 ++- migrations/2025-06-14-100926_init/up.sql | 8 ++--- src/db/mod.rs | 30 ++++++++++++++++++ src/db/models.rs | 40 ++++++++++++++---------- src/db/schema.rs | 15 +++------ src/main.rs | 5 +++ 8 files changed, 80 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index d37733d..fb9b776 100644 --- a/.gitignore +++ b/.gitignore @@ -20,8 +20,8 @@ Cargo.lock # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ - .env - .env.* - !.env.example - !.env.*.example +.env +.env.* +!.env.example +!.env.*.example # Ignore the .env file that contains sensitive information \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 204a869..9bd6472 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,15 @@ { "cSpell.words": [ + "dotenv", "Herstellung", "Kochen", + "MSVC", "Quelle", "Raffination", + "rustc", "serde", "Stück", + "VARCHAR", "Verwendung" ] } diff --git a/Cargo.toml b/Cargo.toml index 981271e..d4728a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,11 @@ edition = "2024" publish = ["merlin"] description = "Utility functions to read environment with fallback and values from a file" license = "MIT" -repository = "ssh://git@gitea.merlinserver.de:2222/Stefan/merlin_env_helper.git" +repository = "ssh://git@gitea.merlinserver.de:2222/Stefan/no-man-sky-wiki.git" [dependencies] diesel = { version= "2.2.10", features = ["serde_json", "postgres", "uuid"]} +dotenv = "0.15.0" env_logger = "0.11.8" log = "0.4.27" merlin_env_helper = { version = "0.2.0", registry = "merlin" } @@ -16,3 +17,5 @@ regex = "1.11.1" reqwest = {version="0.12.15", features=["blocking"]} #scraper = "0.23.1" select = "0.6.1" +serde_json = "1.0.140" +uuid = "1.17.0" diff --git a/migrations/2025-06-14-100926_init/up.sql b/migrations/2025-06-14-100926_init/up.sql index bb511dc..1d345ac 100644 --- a/migrations/2025-06-14-100926_init/up.sql +++ b/migrations/2025-06-14-100926_init/up.sql @@ -5,7 +5,7 @@ CREATE TABLE "icon"( "url" VARCHAR(512), "width" INT4, "height" INT4, - "state" JSON + "state" JSONB ); CREATE TABLE "resource"( @@ -14,7 +14,7 @@ CREATE TABLE "resource"( "title" VARCHAR(255) NOT NULL, "url" VARCHAR(512), "icon" UUID, - "state" JSON, + "state" JSONB, FOREIGN KEY ("icon") REFERENCES "icon"("id") ); @@ -23,7 +23,7 @@ CREATE TABLE "recipe"( "resource" UUID NOT NULL, "recipe_type" VARCHAR(50) NOT NULL, "duration" INT4 NOT NULL, - "state" JSON, + "state" JSONB, FOREIGN KEY ("resource") REFERENCES "resource"("id") ); @@ -31,7 +31,7 @@ CREATE TABLE "ingredient"( "id" UUID NOT NULL PRIMARY KEY, "resource" UUID NOT NULL, "quantity" INT4 NOT NULL, - "state" JSON, + "state" JSONB, "recipe" UUID NOT NULL, FOREIGN KEY ("resource") REFERENCES "resource"("id"), FOREIGN KEY ("recipe") REFERENCES "recipe"("id") diff --git a/src/db/mod.rs b/src/db/mod.rs index 7609346..127c54c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,2 +1,32 @@ pub mod models; +pub mod schema; +use diesel::{Connection, PgConnection}; +use merlin_env_helper::{EnvKey, get_env_value}; pub use models::*; + +fn _read_key() -> String { + get_env_value(&EnvKey::secure_key("DATABASE_URL", "DATABASE_URL_FILE")) + .expect("Failed to read DATABASE_URL") +} + +pub fn establish_connection() -> PgConnection { + let database_url = _read_key(); + PgConnection::establish(&database_url) + .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) +} + +pub fn test() { + let conn = &mut establish_connection(); + use diesel::prelude::*; + + use crate::db::schema::icon::dsl::*; + + let results: Vec = icon + .select(DbIcon::as_select()) + .load(conn) + .expect("Error loading icons"); + + for resource in results { + println!("Found resource: {:?}", resource); + } +} diff --git a/src/db/models.rs b/src/db/models.rs index 34145ff..a1d4fbe 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -2,46 +2,54 @@ #![allow(unused)] #![allow(clippy::all)] -use diesel::{ - prelude::Queryable, - sql_types::{Json, Uuid}, -}; -#[derive(Queryable, Debug)] -pub struct RowIcon { +use diesel::{pg::Pg, prelude::*}; + +use uuid::Uuid; + +#[derive(Queryable, Selectable, Debug)] +#[diesel(table_name = crate::db::schema::icon)] +#[diesel(check_for_backend(Pg))] +pub struct DbIcon { pub id: Uuid, pub name: String, pub content_type: Option, pub url: Option, pub width: Option, pub height: Option, - pub state: Option, + pub state: Option, } -#[derive(Queryable, Debug)] -pub struct RowIngredient { +#[derive(Queryable, Selectable, Debug)] +#[diesel(table_name = crate::db::schema::ingredient)] +#[diesel(check_for_backend(Pg))] +pub struct DbIngredient { pub id: Uuid, pub resource: Uuid, pub quantity: i32, - pub state: Option, + pub state: Option, pub recipe: Uuid, } -#[derive(Queryable, Debug)] -pub struct RowRecipe { +#[derive(Queryable, Selectable, Debug)] +#[diesel(table_name = crate::db::schema::recipe)] +#[diesel(check_for_backend(Pg))] +pub struct DbRecipe { pub id: Uuid, pub resource: Uuid, pub recipe_type: String, pub duration: i32, - pub state: Option, + pub state: Option, } -#[derive(Queryable, Debug)] -pub struct RowResource { +#[derive(Queryable, Selectable, Debug)] +#[diesel(table_name = crate::db::schema::resource)] +#[diesel(check_for_backend(Pg))] +pub struct DbResource { pub id: Uuid, pub name: String, pub title: String, pub url: Option, pub icon: Option, - pub state: Option, + pub state: Option, } diff --git a/src/db/schema.rs b/src/db/schema.rs index 74e65d8..2b48537 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -11,7 +11,7 @@ diesel::table! { url -> Nullable, width -> Nullable, height -> Nullable, - state -> Nullable, + state -> Nullable, } } @@ -20,7 +20,7 @@ diesel::table! { id -> Uuid, resource -> Uuid, quantity -> Int4, - state -> Nullable, + state -> Nullable, recipe -> Uuid, } } @@ -32,7 +32,7 @@ diesel::table! { #[max_length = 50] recipe_type -> Varchar, duration -> Int4, - state -> Nullable, + state -> Nullable, } } @@ -46,7 +46,7 @@ diesel::table! { #[max_length = 512] url -> Nullable, icon -> Nullable, - state -> Nullable, + state -> Nullable, } } @@ -55,9 +55,4 @@ diesel::joinable!(ingredient -> resource (resource)); diesel::joinable!(recipe -> resource (resource)); diesel::joinable!(resource -> icon (icon)); -diesel::allow_tables_to_appear_in_same_query!( - icon, - ingredient, - recipe, - resource, -); +diesel::allow_tables_to_appear_in_same_query!(icon, ingredient, recipe, resource,); diff --git a/src/main.rs b/src/main.rs index 09539a7..45d21c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,17 @@ mod parse; mod types; use std::{fs::File, io::Read}; +use dotenv::dotenv; use parse::parse; +use crate::db::test; + fn main() -> Result<(), Box> { + dotenv().ok(); env_logger::init(); let html = read("test_mordit.html")?; parse(&html); + test(); Ok(()) }