From 31f15dc02bdbb815ce2d53f10d710a65b0b7bf0b Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 21 Feb 2025 23:01:59 +0100 Subject: feat(yt/cli): Make running the migrations of the database optional --- yt/src/app.rs | 13 +++++++++---- yt/src/cli.rs | 5 +++++ yt/src/main.rs | 7 +++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/yt/src/app.rs b/yt/src/app.rs index 937e58f..bf8ad0f 100644 --- a/yt/src/app.rs +++ b/yt/src/app.rs @@ -9,6 +9,7 @@ // If not, see . use anyhow::{Context, Result}; +use log::warn; use sqlx::{SqlitePool, sqlite::SqliteConnectOptions}; use crate::{config::Config, storage::migrate::migrate_db}; @@ -20,7 +21,7 @@ pub struct App { } impl App { - pub async fn new(config: Config) -> Result { + pub async fn new(config: Config, should_migrate_db: bool) -> Result { let options = SqliteConnectOptions::new() .filename(&config.paths.database_path) .optimize_on_close(true, None) @@ -35,9 +36,13 @@ impl App { config, }; - migrate_db(&app) - .await - .context("Failed to migrate db to new version")?; + if should_migrate_db { + migrate_db(&app) + .await + .context("Failed to migrate db to new version")?; + } else { + warn!("Skipping database migration."); + } Ok(app) } diff --git a/yt/src/cli.rs b/yt/src/cli.rs index 3afce3f..73121d1 100644 --- a/yt/src/cli.rs +++ b/yt/src/cli.rs @@ -34,6 +34,11 @@ pub struct CliArgs { #[arg(long, short = 'V', action= ArgAction::SetTrue)] pub version: bool, + /// Do not perform database migration before starting. + /// Setting this could cause runtime database access errors. + #[arg(long, short, action=ArgAction::SetTrue, default_value_t = false)] + pub no_migrate_db: bool, + /// Increase message verbosity #[arg(long="verbose", short = 'v', action = ArgAction::Count)] pub verbosity: u8, diff --git a/yt/src/main.rs b/yt/src/main.rs index 0178488..930eb5b 100644 --- a/yt/src/main.rs +++ b/yt/src/main.rs @@ -88,10 +88,9 @@ async fn main() -> Result<()> { } }); - let app = { - let config = Config::from_config_file(args.db_path, args.config_path, args.color)?; - App::new(config).await? - }; + let config = Config::from_config_file(args.db_path, args.config_path, args.color)?; + + let app = App::new(config, !args.no_migrate_db).await?; match args.command.unwrap_or(Command::default()) { Command::Download { -- cgit 1.4.1