From a4d5982e3c0bd8463cd144aad99e1a5f7628980a Mon Sep 17 00:00:00 2001 From: Dietrich Date: Sun, 21 Mar 2021 13:02:55 +0100 Subject: [PATCH] make initial migrations work (no db file exists). --- .../20210318172317_initialmigration.up.sql | 5 ++- src/cli.rs | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/migrations/20210318172317_initialmigration.up.sql b/migrations/20210318172317_initialmigration.up.sql index 4fdd3e4..514d3ba 100644 --- a/migrations/20210318172317_initialmigration.up.sql +++ b/migrations/20210318172317_initialmigration.up.sql @@ -8,11 +8,10 @@ CREATE TABLE IF NOT EXISTS users ( email VARCHAR NOT NULL, password VARCHAR NOT NULL, role INTEGER DEFAULT 1 NOT NULL, - UNIQUE(username, email) + UNIQUE(username), + UNIQUE(email) ); -CREATE UNIQUE INDEX ux_username ON users(username); - CREATE TABLE IF NOT EXISTS links ( id INTEGER PRIMARY KEY NOT NULL, title VARCHAR NOT NULL, diff --git a/src/cli.rs b/src/cli.rs index a65ddb4..12f189d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -5,6 +5,7 @@ use clap::{ use dotenv::dotenv; use sqlx::{migrate::Migrator, Pool, Sqlite}; use std::{ + fs::File, io::{self, BufRead, Write}, path::PathBuf, }; @@ -213,14 +214,16 @@ async fn parse_args_to_config(config: ArgMatches<'_>, log: Logger) -> ServerConf pub(crate) async fn setup() -> Result, ServerError> { dotenv().ok(); + // initiallize the logger let decorator = slog_term::TermDecorator::new().build(); let drain = slog_term::FullFormat::new(decorator).build().fuse(); let drain = slog_async::Async::new(drain).build().fuse(); let log = slog::Logger::root(drain, slog_o!("name" => "Pslink")); + // Print launch info slog_info!(log, "Launching Pslink a 'Private short link generator'"); - slog_info!(log, ".env file setup, logging initialized"); + slog_info!(log, "logging initialized"); let app = generate_cli(); @@ -236,17 +239,22 @@ pub(crate) async fn setup() -> Result, ServerError> .parse::() .expect("Failed to parse Database path."); if !db.exists() { - let msg = format!( - concat!( - "Database not found at {}!", - " Create a new database with: `pslink migrate-database`", - "or adjust the databasepath." - ), - db.display() - ); - slog_error!(log, "{}", msg); - eprintln!("{}", msg); - return Ok(None); + if config.subcommand_matches("migrate-database").is_none() { + let msg = format!( + concat!( + "Database not found at {}!", + " Create a new database with: `pslink migrate-database`", + "or adjust the databasepath." + ), + db.display() + ); + slog_error!(log, "{}", msg); + eprintln!("{}", msg); + return Ok(None); + } + + // create an empty database file the if above makes sure that this file does not exist. + File::create(db)?; }; let server_config: crate::ServerConfig = parse_args_to_config(config.clone(), log).await;