Successfully added an insert query with diesel.

This commit is contained in:
franklinblanco 2022-06-24 19:54:37 -04:00
parent 38e4bbf3c2
commit 7a8847ed5b
11 changed files with 59 additions and 14 deletions

View File

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

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 user

5
migrations/mysql/up.sql Normal file
View File

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

View File

@ -2,7 +2,9 @@ use std::collections::HashMap;
use diesel::{MysqlConnection, Connection, ConnectionError};
pub async fn start_database_connection(env_vars: &HashMap<String, String>) -> Result<MysqlConnection, ConnectionError>{
use crate::embedded_migrations;
pub fn start_database_connection(env_vars: &HashMap<String, String>) -> Result<MysqlConnection, ConnectionError>{
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<String, String>) -> 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());
}

View File

@ -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<usize, diesel::result::Error>{
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(())
}

View File

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

View File

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

View File

@ -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<u16>, _data: Data<Mutex<MysqlConnection>>) -> HttpResponse {
pub async fn get_user_from_db(id: Path<i32>, _data: Data<Mutex<MysqlConnection>>) -> 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()}))
}

6
src/schema.rs Normal file
View File

@ -0,0 +1,6 @@
table! {
user (id) {
id -> Integer,
name -> Varchar,
}
}