use crate::{error_handler::CustomError, airports::{Airport, Airports}}; use diesel::pg::PgConnection; use diesel::r2d2::ConnectionManager; use lazy_static::lazy_static; use log::{error, info, debug}; use r2d2; use std::env; type Pool = r2d2::Pool>; pub type DbConnection = r2d2::PooledConnection>; diesel_migrations::embed_migrations!(); lazy_static! { static ref POOL: Pool = { let username = env::var("DATABASE_USER").expect("Database username is not set"); let password = env::var("DATABASE_PASSWORD").expect("Database password is not set"); let name = env::var("DATABASE_NAME").expect("Database name is not set"); let url = format!("postgres://{}:{}@localhost:5433/{}", username, password, name); let manager = ConnectionManager::::new(url); Pool::new(manager).expect("Failed to create db pool") }; } pub fn init() { lazy_static::initialize(&POOL); let conn = connection().expect("Failed to get db connection"); match embedded_migrations::run(&conn) { Ok(_) => info!("Database initialized"), Err(err) => error!("Failed to initialize database; {}", err), }; } pub fn connection() -> Result { POOL.get() .map_err(|e| CustomError::new(500, format!("Failed getting db connection: {}", e))) } pub fn import_data() { let contents: String = std::fs::read_to_string("airport-codes.json").expect("Failed to read file"); let airports: Vec = serde_json::from_str(&contents).expect("JSON was not well formed."); for airport in airports { match Airports::create(airport) { Ok(_) => {}, Err(err) => error!("Error inserting airport; {}", err) }; } debug!("Imported data"); }