From f5d70ddfd451355774e534784056ca6583eded13 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 19 Jun 2018 17:29:54 +0100 Subject: [PATCH] Database setup --- .env | 2 ++ src/setup.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 00000000..ee8fce89 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ + +BASE_URL=plu.me \ No newline at end of file diff --git a/src/setup.rs b/src/setup.rs index c87fc0f3..e171ba26 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,8 +1,9 @@ use colored::Colorize; use diesel::{pg::PgConnection, r2d2::{ConnectionManager, Pool}}; use dotenv::dotenv; -use std::fs; +use std::fs::{self, File}; use std::io; +use std::path::Path; use std::process::{exit, Command}; use rpassword; @@ -32,6 +33,7 @@ pub fn check() -> PgPool { } Err(_) => panic!("Couldn't connect to database") } + migrate(); pool } else { run_setup(None); @@ -49,7 +51,8 @@ fn run_setup(conn: Option) { ); read_line(); check_native_deps(); - setup_type(conn.expect("Couldn't connect to the Plume database")); + let conn = setup_db(conn); + setup_type(conn); println!("{}\n{}\n{}", "Your Plume instance is now ready to be used.".magenta(), @@ -58,6 +61,64 @@ fn run_setup(conn: Option) { ); } +fn setup_db(conn: Option) -> DbConn { + match conn { + Some(conn) => conn, + None => { + println!("\n{}\n", "We are going to setup the database.".magenta()); + println!("{}\n", "About to create a new PostgreSQL user named 'plume'".blue()); + Command::new("createuser") + .arg("-d") + .arg("-P") + .arg("plume") + .status() + .map(|s| { + if s.success() { + println!("{}\n", " ✔️ Done".green()); + } + }) + .expect("Couldn't create new user"); + + println!("{}\n", "About to create a new PostgreSQL table named 'plume'".blue()); + Command::new("createdb") + .arg("-O") + .arg("plume") + .arg("plume") + .status() + .map(|s| { + if s.success() { + println!("{}\n", " ✔️ Done".green()); + } + }) + .expect("Couldn't create new table"); + + migrate(); + + init_pool() + .expect("Couldn't init DB pool") + .get() + .map(|c| DbConn(c)) + .expect("Couldn't connect to the database") + } + } +} + +fn migrate() { + println!("{}\n", "Running migrations…".blue()); + Command::new("diesel") + .arg("migration") + .arg("run") + .arg("--database-url") + .arg(DB_URL.as_str()) + .status() + .map(|s| { + if s.success() { + println!("{}\n", " ✔️ Done".green()); + } + }) + .expect("Couldn't run migrations"); +} + fn setup_type(conn: DbConn) { println!("\nDo you prefer a simple setup, or to customize everything?\n"); println!(" 1 - Simple setup"); @@ -170,5 +231,9 @@ fn read_line_or(or: &str) -> String { } fn write_to_dotenv(var: &'static str, val: String) { + if !Path::new(".env").exists() { + File::create(".env").expect("Error while creating .env file"); + } + fs::write(".env", format!("{}\n{}={}", fs::read_to_string(".env").expect("Unable to read .env"), var, val)).expect("Unable to write .env"); }