diff --git a/Cargo.toml b/Cargo.toml index 70dcd75..2922a48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ dotenv = "0.15.0" serde = { version = "1.0", features = ["derive"] } tokio = { version = "1", features = ["full"] } #sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "mysql" ] } -diesel = { version = "1.4.4", features = ["mysql"] } +diesel = { version = "1.4.4", features = ["mysql", "chrono"] } +diesel_migrations = { version = "1.4.0"} actix-web = "4" chrono = "0.4" \ No newline at end of file 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/mysql/down.sql b/migrations/mysql/down.sql new file mode 100644 index 0000000..ef17d92 --- /dev/null +++ b/migrations/mysql/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE user \ No newline at end of file diff --git a/migrations/mysql/up.sql b/migrations/mysql/up.sql new file mode 100644 index 0000000..88525bb --- /dev/null +++ b/migrations/mysql/up.sql @@ -0,0 +1,5 @@ +-- Your SQL goes here +CREATE TABLE IF NOT EXISTS user ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL +) \ No newline at end of file diff --git a/src/dao/main_dao.rs b/src/dao/main_dao.rs index e021fdf..ff8bfd4 100644 --- a/src/dao/main_dao.rs +++ b/src/dao/main_dao.rs @@ -2,7 +2,9 @@ use std::collections::HashMap; use diesel::{MysqlConnection, Connection, ConnectionError}; -pub async fn start_database_connection(env_vars: &HashMap) -> Result{ +use crate::embedded_migrations; + +pub fn start_database_connection(env_vars: &HashMap) -> Result{ let db_user = match env_vars.get("DB_USER") { Some(str) => str, None => panic!("DB_USER env var not found") @@ -21,4 +23,11 @@ pub async fn start_database_connection(env_vars: &HashMap) -> Re }; let formatted_db_url = &format!("mysql://{db_user}:{db_pass}@{db_host}/{db_database_name}"); MysqlConnection::establish(formatted_db_url) +} +pub fn run_all_migrations(conn: &mut MysqlConnection){ + match embedded_migrations::run(conn){ + Ok(()) => {println!("{}", "Successfully ran migrations.")} + Err(e) => {panic!("Error happened while trying to run migrations Error: {}", e)} + } + //TODO: log output like this: embedded_migrations::run_with_output(&connection, &mut std::io::stdout()); } \ No newline at end of file diff --git a/src/dao/user_dao.rs b/src/dao/user_dao.rs index 9e76f40..f09aa98 100644 --- a/src/dao/user_dao.rs +++ b/src/dao/user_dao.rs @@ -1,9 +1,10 @@ -use crate::r#do::user::User; +use diesel::prelude::*; -pub async fn _create_user_table_if_not_exists() -> Result<(), ()>{ +use crate::{schema::user, r#do::user::User}; - Ok(()) +pub fn _insert_user(conn: &mut MysqlConnection, user: User) -> Result{ + diesel::insert_into(user::table).values(&user).execute(conn) } -pub async fn _insert_user( _user: User) -> Result<(), ()>{ +pub fn _find_user_by_id(_conn: &mut MysqlConnection, _id: i32) -> Result<(), ()>{ Ok(()) } \ No newline at end of file diff --git a/src/do/user.rs b/src/do/user.rs index a025e47..f9aaf5e 100644 --- a/src/do/user.rs +++ b/src/do/user.rs @@ -1,8 +1,11 @@ -use diesel::Queryable; +use diesel::{Queryable, Insertable}; use serde::Serialize; -#[derive(Serialize, Queryable)] +use crate::schema::*; + +#[derive(Serialize, Queryable, Insertable)] +#[table_name = "user"] pub struct User{ - pub id: u16, + pub id: i32, pub name: String } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2556cc8..8a7b571 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,20 @@ mod r#do; mod dao; mod routes; mod service; -mod util; +mod util; pub mod schema; use r#do::shared_state::SharedStateObj; use util::env_util; use routes::main_router::{start_all_routes, after_startup_fn}; -use dao::main_dao; +use dao::main_dao::{self, run_all_migrations}; + +// Include diesel_migrations so embed_migrations! macro can be called. +#[macro_use] +extern crate diesel_migrations; +#[macro_use] +extern crate diesel; +// Run all migrations inside the path +embed_migrations!(); + #[tokio::main] async fn main() -> Result<(), std::io::Error> { @@ -14,11 +23,14 @@ async fn main() -> Result<(), std::io::Error> { let env_vars = env_util::get_dot_env_map(); // Start database - let db_conn = match main_dao::start_database_connection(&env_vars).await { + let mut db_conn = match main_dao::start_database_connection(&env_vars) { Ok(conn) => conn, Err(e) => panic!("Failure starting the database. Reason: {}", e) }; + // Run all migrations + run_all_migrations(&mut db_conn); + // Put db connection and env variables in shared state let shared_state_obj = SharedStateObj {db_conn, env_vars }; diff --git a/src/routes/user_routes.rs b/src/routes/user_routes.rs index 99f3cf6..160d302 100644 --- a/src/routes/user_routes.rs +++ b/src/routes/user_routes.rs @@ -3,10 +3,11 @@ use std::sync::Mutex; use actix_web::{get, web::{self, Path, Data}, HttpResponse, post}; use diesel::MysqlConnection; -use crate::r#do::user::User; +use crate::{r#do::user::User, dao::user_dao::_insert_user}; #[get("/user/{id}")] -pub async fn get_user_from_db(id: Path, _data: Data>) -> HttpResponse { +pub async fn get_user_from_db(id: Path, _data: Data>) -> HttpResponse { + _insert_user(&mut _data.lock().unwrap(), User { id: *id, name: "nigga".to_string() }); HttpResponse::Ok().json(web::Json(User {id: *id, name: "nigga".to_string()})) } diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..f2e0999 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,6 @@ +table! { + user (id) { + id -> Integer, + name -> Varchar, + } +}