From e6bdcb4816cd54b7477f50cdebf06b07e7b9c58e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 22 Feb 2025 10:57:38 +0100 Subject: feat(yt/version): Show _current_ database version `yt status` also displays the database version, but this happens after the database was already migrate to the latest version. --- yt/src/main.rs | 9 ++++----- yt/src/version/mod.rs | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/yt/src/main.rs b/yt/src/main.rs index 930eb5b..7c550af 100644 --- a/yt/src/main.rs +++ b/yt/src/main.rs @@ -59,11 +59,6 @@ pub mod watch; async fn main() -> Result<()> { let args = cli::CliArgs::parse(); - if args.version { - version::show()?; - return Ok(()); - } - // The default verbosity is 1 (Warn) let verbosity: u8 = args.verbosity + 1; @@ -89,6 +84,10 @@ async fn main() -> Result<()> { }); let config = Config::from_config_file(args.db_path, args.config_path, args.color)?; + if args.version { + version::show(&config).await?; + return Ok(()); + } let app = App::new(config, !args.no_migrate_db).await?; diff --git a/yt/src/version/mod.rs b/yt/src/version/mod.rs index 369541e..5e5b9a7 100644 --- a/yt/src/version/mod.rs +++ b/yt/src/version/mod.rs @@ -1,6 +1,9 @@ use std::process::Command; use anyhow::{Context, Result}; +use sqlx::{SqlitePool, sqlite::SqliteConnectOptions}; + +use crate::{config::Config, storage::migrate::get_version_db}; fn get_cmd_version(cmd: &str) -> Result { let out = String::from_utf8( @@ -15,13 +18,31 @@ fn get_cmd_version(cmd: &str) -> Result { Ok(out.trim().to_owned()) } -pub fn show() -> Result<()> { +pub async fn show(config: &Config) -> Result<()> { + let db_version = { + let options = SqliteConnectOptions::new() + .filename(&config.paths.database_path) + .optimize_on_close(true, None) + .create_if_missing(true); + + let pool = SqlitePool::connect_with(options) + .await + .context("Failed to connect to database!")?; + + get_version_db(&pool) + .await + .context("Failed to determine database version")? + }; + + // TODO(@bpeetz): Use `pyo3`'s build in mechanism instead of executing the python CLI <2025-02-21> let python_version = get_cmd_version("python")?; let yt_dlp_version = get_cmd_version("yt-dlp")?; println!( "{}: {} +db version: {db_version} + python: {python_version} yt-dlp: {yt_dlp_version}", env!("CARGO_PKG_NAME"), -- cgit 1.4.1