diff --git a/crates/db_schema/src/schema_setup.rs b/crates/db_schema/src/schema_setup.rs index 868659e30..b7799b139 100644 --- a/crates/db_schema/src/schema_setup.rs +++ b/crates/db_schema/src/schema_setup.rs @@ -20,7 +20,7 @@ use diesel::{ use diesel_migrations::MigrationHarness; use lemmy_utils::{error::LemmyResult, settings::SETTINGS}; use std::time::Instant; -use tracing::log::debug; +use tracing::debug; diesel::table! { pg_namespace (nspname) { @@ -52,6 +52,7 @@ struct MigrationHarnessWrapper<'a> { conn: &'a mut PgConnection, #[cfg(test)] diff_checked_migration_name: Option, + options: &'a Options, } impl MigrationHarnessWrapper<'_> { @@ -67,7 +68,7 @@ impl MigrationHarnessWrapper<'_> { .map(|d| d.to_string()) .unwrap_or_default(); let name = migration.name(); - debug!("{duration} run {name}"); + self.options.print(&format!("{duration} run {name}")); result } @@ -112,7 +113,7 @@ impl MigrationHarness for MigrationHarnessWrapper<'_> { .map(|d| d.to_string()) .unwrap_or_default(); let name = migration.name(); - debug!("{duration} revert {name}"); + self.options.print(&format!("{duration} revert {name}")); result } @@ -128,6 +129,7 @@ pub struct Options { enable_diff_check: bool, revert: bool, run: bool, + print_output: bool, limit: Option, } @@ -152,6 +154,21 @@ impl Options { self.limit = Some(limit); self } + + /// If print_output is true, use println!. + /// Otherwise, use debug! + pub fn print_output(mut self) -> Self { + self.print_output = true; + self + } + + fn print(&self, text: &str) { + if self.print_output { + println!("{text}"); + } else { + debug!("{text}"); + } + } } /// Checked by tests @@ -192,9 +209,9 @@ pub fn run(options: Options) -> LemmyResult { // Block concurrent attempts to run migrations until `conn` is closed, and disable the // trigger that prevents the Diesel CLI from running migrations - debug!("Waiting for lock..."); + options.print("Waiting for lock..."); conn.batch_execute("SELECT pg_advisory_lock(0);")?; - debug!("Running Database migrations (This may take a long time)..."); + options.print("Running Database migrations (This may take a long time)..."); // Drop `r` schema, so migrations don't need to be made to work both with and without things in // it existing @@ -227,7 +244,7 @@ pub fn run(options: Options) -> LemmyResult { Branch::ReplaceableSchemaNotRebuilt }; - debug!("Database migrations complete."); + options.print("Database migrations complete."); Ok(output) } @@ -265,6 +282,7 @@ fn run_selected_migrations( ) -> diesel::migration::Result<()> { let mut wrapper = MigrationHarnessWrapper { conn, + options, #[cfg(test)] diff_checked_migration_name: options .enable_diff_check diff --git a/src/lib.rs b/src/lib.rs index d271dc1dc..b7995e47c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,9 +138,6 @@ enum MigrationSubcommand { /// Placing the main function in lib.rs allows other crates to import it and embed Lemmy pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> { - // Print version number to log - println!("Starting Lemmy v{VERSION}"); - if let Some(CmdSubcommand::Migration { subcommand, all, @@ -150,7 +147,8 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> { let mut options = match subcommand { MigrationSubcommand::Run => schema_setup::Options::default().run(), MigrationSubcommand::Revert => schema_setup::Options::default().revert(), - }; + } + .print_output(); if !all { options = options.limit(number); @@ -161,6 +159,9 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> { return Ok(()); } + // Print version number to log + println!("Starting Lemmy v{VERSION}"); + // return error 503 while running db migrations and startup tasks let mut startup_server_handle = None; if !args.disable_http_server {