dbtest program argument now clear the database.
It does a backup prior clearing.
This commit is contained in:
parent
0a1631e66c
commit
adcf4a5a5d
1 changed files with 24 additions and 1 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::{middleware, web, App, HttpServer};
|
use actix_web::{middleware, web, App, HttpServer};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use log::error;
|
|
||||||
|
|
||||||
use data::db;
|
use data::db;
|
||||||
|
|
||||||
|
|
@ -60,6 +61,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Args {
|
struct Args {
|
||||||
|
/// Will clear the database and insert some test data. (A backup is made first).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
dbtest: bool,
|
dbtest: bool,
|
||||||
}
|
}
|
||||||
|
|
@ -68,6 +70,27 @@ fn process_args() -> bool {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
if args.dbtest {
|
if args.dbtest {
|
||||||
|
// Make a backup of the database.
|
||||||
|
let db_path = Path::new(consts::DB_DIRECTORY).join(consts::DB_FILENAME);
|
||||||
|
if db_path.exists() {
|
||||||
|
let db_path_bckup = (1..)
|
||||||
|
.find_map(|n| {
|
||||||
|
let p = db_path.with_extension(format!("sqlite.bckup{:03}", n));
|
||||||
|
if p.exists() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(p)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
std::fs::copy(&db_path, &db_path_bckup).expect(&format!(
|
||||||
|
"Unable to make backup of {:?} to {:?}",
|
||||||
|
&db_path, &db_path_bckup
|
||||||
|
));
|
||||||
|
std::fs::remove_file(&db_path)
|
||||||
|
.expect(&format!("Unable to remove db file: {:?}", &db_path));
|
||||||
|
}
|
||||||
|
|
||||||
match db::Connection::new() {
|
match db::Connection::new() {
|
||||||
Ok(con) => {
|
Ok(con) => {
|
||||||
if let Err(error) = con.execute_file("sql/data_test.sql") {
|
if let Err(error) = con.execute_file("sql/data_test.sql") {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue