From 32b7632258c022a3481b5478d1f8aed9899ce95e Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 1 Mar 2021 21:30:37 -0600 Subject: [PATCH] feat(db): add diesel and sqlite3 as dependencies --- .env.example | 3 ++- Cargo.toml | 2 ++ diesel.toml | 5 ++++ migrations/.gitkeep | 0 .../down.sql | 2 ++ .../up.sql | 8 +++++++ .../down.sql | 2 ++ .../up.sql | 7 ++++++ src/db.rs | 14 +++++++++++ src/lib.rs | 6 +++++ src/models.rs | 8 +++++++ src/schema.rs | 23 +++++++++++++++++++ 12 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 diesel.toml create mode 100644 migrations/.gitkeep create mode 100644 migrations/2021-03-02-022949_create_game_file_table/down.sql create mode 100644 migrations/2021-03-02-022949_create_game_file_table/up.sql create mode 100644 migrations/2021-03-02-025744_create_game_save_loc_table/down.sql create mode 100644 migrations/2021-03-02-025744_create_game_save_loc_table/up.sql create mode 100644 src/db.rs create mode 100644 src/models.rs create mode 100644 src/schema.rs diff --git a/.env.example b/.env.example index c0a1380..1f36879 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -RUST_LOG = \ No newline at end of file +RUST_LOG= +DATABASE_URL= \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 1517063..3d642ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,7 @@ members = ["server", "client"] uuid = { version = "^0.8", features = ["v4"] } twox-hash = "^1.6" thiserror = "^1.0" +diesel = { version = "^1.4", features = ["sqlite"] } +dotenv = "^0.15" [dev-dependencies] diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/2021-03-02-022949_create_game_file_table/down.sql b/migrations/2021-03-02-022949_create_game_file_table/down.sql new file mode 100644 index 0000000..790e63a --- /dev/null +++ b/migrations/2021-03-02-022949_create_game_file_table/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE game_file \ No newline at end of file diff --git a/migrations/2021-03-02-022949_create_game_file_table/up.sql b/migrations/2021-03-02-022949_create_game_file_table/up.sql new file mode 100644 index 0000000..b4d4faa --- /dev/null +++ b/migrations/2021-03-02-022949_create_game_file_table/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE game_file ( + id INTEGER PRIMARY KEY, + original_path BLOB NOT NULL, -- TEXT assumes a Unicode Encoding + file_hash INTEGER NOT NULL, -- u64, but that's not + game_save_id INTEGER NOT NULL, + FOREIGN KEY (game_save_id) REFERENCES game_save_location (id) +) \ No newline at end of file diff --git a/migrations/2021-03-02-025744_create_game_save_loc_table/down.sql b/migrations/2021-03-02-025744_create_game_save_loc_table/down.sql new file mode 100644 index 0000000..503f787 --- /dev/null +++ b/migrations/2021-03-02-025744_create_game_save_loc_table/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE game_save_location \ No newline at end of file diff --git a/migrations/2021-03-02-025744_create_game_save_loc_table/up.sql b/migrations/2021-03-02-025744_create_game_save_loc_table/up.sql new file mode 100644 index 0000000..2efd290 --- /dev/null +++ b/migrations/2021-03-02-025744_create_game_save_loc_table/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here +CREATE TABLE game_save_location ( + id INTEGER PRIMARY KEY, + friendly_name TEXT -- This can be null + original_PATH BLOB NOT NULL, + uuid BLOB NOT NULL +) \ No newline at end of file diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..c3f9a32 --- /dev/null +++ b/src/db.rs @@ -0,0 +1,14 @@ +use diesel::prelude::*; +use diesel::sqlite::SqliteConnection; +use dotenv::dotenv; +use std::env; + +pub fn establish_connection() -> SqliteConnection { + dotenv().ok(); + + // TODO: Consider whether it is best practice to panic here + // or have establish_connection return a Result with a thiserror enum + + let db_url = env::var("DATABASE_URL").expect("$DATABASE_URL was not set"); + SqliteConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url)) +} diff --git a/src/lib.rs b/src/lib.rs index bfbe1ff..a94397d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,10 @@ +#[macro_use] +extern crate diesel; + +pub mod db; pub mod game; +pub mod models; +pub mod schema; #[cfg(test)] mod tests { diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..91ad98a --- /dev/null +++ b/src/models.rs @@ -0,0 +1,8 @@ +use std::path::PathBuf; + +#[derive(Queryable)] +pub struct GameFile { + pub id: i32, + pub original_path: PathBuf, + pub file_hash: u64, +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..549c02f --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,23 @@ +table! { + game_file (id) { + id -> Nullable, + original_path -> Binary, + file_hash -> Integer, + game_save_id -> Integer, + } +} + +table! { + game_save_location (id) { + id -> Nullable, + friendly_name -> Text, + uuid -> Binary, + } +} + +joinable!(game_file -> game_save_location (game_save_id)); + +allow_tables_to_appear_in_same_query!( + game_file, + game_save_location, +);