make initial migrations work (no db file exists).
This commit is contained in:
parent
e8ff696006
commit
a4d5982e3c
@ -8,11 +8,10 @@ CREATE TABLE IF NOT EXISTS users (
|
|||||||
email VARCHAR NOT NULL,
|
email VARCHAR NOT NULL,
|
||||||
password VARCHAR NOT NULL,
|
password VARCHAR NOT NULL,
|
||||||
role INTEGER DEFAULT 1 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 (
|
CREATE TABLE IF NOT EXISTS links (
|
||||||
id INTEGER PRIMARY KEY NOT NULL,
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
title VARCHAR NOT NULL,
|
title VARCHAR NOT NULL,
|
||||||
|
32
src/cli.rs
32
src/cli.rs
@ -5,6 +5,7 @@ use clap::{
|
|||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use sqlx::{migrate::Migrator, Pool, Sqlite};
|
use sqlx::{migrate::Migrator, Pool, Sqlite};
|
||||||
use std::{
|
use std::{
|
||||||
|
fs::File,
|
||||||
io::{self, BufRead, Write},
|
io::{self, BufRead, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
@ -213,14 +214,16 @@ async fn parse_args_to_config(config: ArgMatches<'_>, log: Logger) -> ServerConf
|
|||||||
pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError> {
|
pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
|
// initiallize the logger
|
||||||
let decorator = slog_term::TermDecorator::new().build();
|
let decorator = slog_term::TermDecorator::new().build();
|
||||||
let drain = slog_term::FullFormat::new(decorator).build().fuse();
|
let drain = slog_term::FullFormat::new(decorator).build().fuse();
|
||||||
let drain = slog_async::Async::new(drain).build().fuse();
|
let drain = slog_async::Async::new(drain).build().fuse();
|
||||||
|
|
||||||
let log = slog::Logger::root(drain, slog_o!("name" => "Pslink"));
|
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, "Launching Pslink a 'Private short link generator'");
|
||||||
slog_info!(log, ".env file setup, logging initialized");
|
slog_info!(log, "logging initialized");
|
||||||
|
|
||||||
let app = generate_cli();
|
let app = generate_cli();
|
||||||
|
|
||||||
@ -236,17 +239,22 @@ pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError>
|
|||||||
.parse::<PathBuf>()
|
.parse::<PathBuf>()
|
||||||
.expect("Failed to parse Database path.");
|
.expect("Failed to parse Database path.");
|
||||||
if !db.exists() {
|
if !db.exists() {
|
||||||
let msg = format!(
|
if config.subcommand_matches("migrate-database").is_none() {
|
||||||
concat!(
|
let msg = format!(
|
||||||
"Database not found at {}!",
|
concat!(
|
||||||
" Create a new database with: `pslink migrate-database`",
|
"Database not found at {}!",
|
||||||
"or adjust the databasepath."
|
" Create a new database with: `pslink migrate-database`",
|
||||||
),
|
"or adjust the databasepath."
|
||||||
db.display()
|
),
|
||||||
);
|
db.display()
|
||||||
slog_error!(log, "{}", msg);
|
);
|
||||||
eprintln!("{}", msg);
|
slog_error!(log, "{}", msg);
|
||||||
return Ok(None);
|
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;
|
let server_config: crate::ServerConfig = parse_args_to_config(config.clone(), log).await;
|
||||||
|
Loading…
Reference in New Issue
Block a user