Database setup

This commit is contained in:
Bat 2018-06-19 17:29:54 +01:00
parent d00688e526
commit f5d70ddfd4
2 changed files with 69 additions and 2 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
BASE_URL=plu.me

View File

@ -1,8 +1,9 @@
use colored::Colorize; use colored::Colorize;
use diesel::{pg::PgConnection, r2d2::{ConnectionManager, Pool}}; use diesel::{pg::PgConnection, r2d2::{ConnectionManager, Pool}};
use dotenv::dotenv; use dotenv::dotenv;
use std::fs; use std::fs::{self, File};
use std::io; use std::io;
use std::path::Path;
use std::process::{exit, Command}; use std::process::{exit, Command};
use rpassword; use rpassword;
@ -32,6 +33,7 @@ pub fn check() -> PgPool {
} }
Err(_) => panic!("Couldn't connect to database") Err(_) => panic!("Couldn't connect to database")
} }
migrate();
pool pool
} else { } else {
run_setup(None); run_setup(None);
@ -49,7 +51,8 @@ fn run_setup(conn: Option<DbConn>) {
); );
read_line(); read_line();
check_native_deps(); 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{}", println!("{}\n{}\n{}",
"Your Plume instance is now ready to be used.".magenta(), "Your Plume instance is now ready to be used.".magenta(),
@ -58,6 +61,64 @@ fn run_setup(conn: Option<DbConn>) {
); );
} }
fn setup_db(conn: Option<DbConn>) -> 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) { fn setup_type(conn: DbConn) {
println!("\nDo you prefer a simple setup, or to customize everything?\n"); println!("\nDo you prefer a simple setup, or to customize everything?\n");
println!(" 1 - Simple setup"); println!(" 1 - Simple setup");
@ -170,5 +231,9 @@ fn read_line_or(or: &str) -> String {
} }
fn write_to_dotenv(var: &'static str, val: 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"); fs::write(".env", format!("{}\n{}={}", fs::read_to_string(".env").expect("Unable to read .env"), var, val)).expect("Unable to write .env");
} }