From 787a32eabc734a051ce7c2ee90f5743c462ba41d Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 10 Feb 2024 23:34:53 -0400 Subject: [PATCH] Add postgres DB with diesel. Possibly this was a mistake. --- Cargo.toml | 2 ++ diesel.toml | 9 +++++ .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../2024-02-11-020849_create_users/down.sql | 1 + .../2024-02-11-020849_create_users/up.sql | 8 +++++ src/main.rs | 28 ++++++++++++--- src/models.rs | 13 +++++++ src/schema.rs | 12 +++++++ 9 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 diesel.toml create mode 100644 migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 migrations/2024-02-11-020849_create_users/down.sql create mode 100644 migrations/2024-02-11-020849_create_users/up.sql create mode 100644 src/models.rs create mode 100644 src/schema.rs diff --git a/Cargo.toml b/Cargo.toml index ce86395..adcd8e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ axum = "0.7" askama = "0.12" askama_axum = "0.4" tower-http = { version = "0.5", features = ["fs"] } +diesel = { version = "2.1", features = ["postgres", "chrono"] } +deadpool-diesel = {version = "0.5", features = ["postgres"]} diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/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"] + +[migrations_directory] +dir = "migrations" diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/migrations/2024-02-11-020849_create_users/down.sql b/migrations/2024-02-11-020849_create_users/down.sql new file mode 100644 index 0000000..cc1f647 --- /dev/null +++ b/migrations/2024-02-11-020849_create_users/down.sql @@ -0,0 +1 @@ +DROP TABLE users; diff --git a/migrations/2024-02-11-020849_create_users/up.sql b/migrations/2024-02-11-020849_create_users/up.sql new file mode 100644 index 0000000..9647fe6 --- /dev/null +++ b/migrations/2024-02-11-020849_create_users/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR NOT NULL, + real_name VARCHAR NOT NULL, + email VARCHAR NOT NULL, + password_hash VARCHAR NOT NULL, + password_salt VARCHAR NOT NULL +); diff --git a/src/main.rs b/src/main.rs index 34dd09e..b3b2561 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,23 @@ -use std::env; -use askama::Template; -use axum::{routing::get, Router}; -use tower_http::services::ServeDir; +use { + askama::Template, + axum::{extract::State, routing::get, Router}, + deadpool_diesel::postgres::{Manager, Pool, Runtime}, + std::env, + tower_http::services::ServeDir, +}; + +mod models; +mod schema; + #[tokio::main] async fn main() -> Result<(), Box> { + let db_pool_manager = Manager::new(env::var("LOCALHUB_DATABASE_URL")?, Runtime::Tokio1); + let db_pool = Pool::builder(db_pool_manager).max_size(8).build()?; + let app = Router::new() .route("/", get(root)) + .with_state(db_pool) .nest_service( "/static", ServeDir::new(env::var("LOCALHUB_STATIC_FILE_PATH")?), @@ -23,6 +34,13 @@ struct IndexTemplate<'a> { title: &'a str, } -async fn root<'a>() -> IndexTemplate<'a> { +async fn root<'a>(State(db_pool): State) -> IndexTemplate<'a> { + use self::models::*; + use diesel::prelude::*; + let db_conn = db_pool.get().await.unwrap(); + db_conn.interact(|conn| { + let results = schema::users::table.select(User::as_select()).load(conn).unwrap(); + println!("Found {} users", results.len()); + }).await.unwrap(); IndexTemplate { title: "LocalHub" } } diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..33673d3 --- /dev/null +++ b/src/models.rs @@ -0,0 +1,13 @@ +use diesel::prelude::*; + +#[derive(Queryable, Selectable)] +#[diesel(table_name = crate::schema::users)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct User { + pub id: i32, + pub username: String, + pub real_name: String, + pub email: String, + pub password_hash: String, + pub password_salt: String, +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..ab1af24 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,12 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> Int4, + username -> Varchar, + real_name -> Varchar, + email -> Varchar, + password_hash -> Varchar, + password_salt -> Varchar, + } +}