Only print migration output when running migrate command (#5363)

* Revert "Change println! to debug! (#5346)"

This reverts commit 8ee81a3967.

* Using a custom print command.
This commit is contained in:
Dessalines 2025-01-29 13:51:23 -05:00 committed by GitHub
parent 2847040b3f
commit c0e156dfa7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 10 deletions

View file

@ -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<String>,
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<Pg> 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<u64>,
}
@ -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<Branch> {
// 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> {
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

View file

@ -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 {