feat(db): add diesel and sqlite3 as dependencies

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-03-01 21:30:37 -06:00
parent e4dfea45c5
commit 32b7632258
12 changed files with 79 additions and 1 deletions

View File

@ -1 +1,2 @@
RUST_LOG=
DATABASE_URL=

View File

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

5
diesel.toml Normal file
View File

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

0
migrations/.gitkeep Normal file
View File

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE game_file

View File

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

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE game_save_location

View File

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

14
src/db.rs Normal file
View File

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

View File

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

8
src/models.rs Normal file
View File

@ -0,0 +1,8 @@
use std::path::PathBuf;
#[derive(Queryable)]
pub struct GameFile {
pub id: i32,
pub original_path: PathBuf,
pub file_hash: u64,
}

23
src/schema.rs Normal file
View File

@ -0,0 +1,23 @@
table! {
game_file (id) {
id -> Nullable<Integer>,
original_path -> Binary,
file_hash -> Integer,
game_save_id -> Integer,
}
}
table! {
game_save_location (id) {
id -> Nullable<Integer>,
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,
);