Skip to content

Commit

Permalink
Merge pull request #254 from LemmyNet/main
Browse files Browse the repository at this point in the history
[pull] master from LemmyNet:main
  • Loading branch information
pull[bot] authored Jan 29, 2025
2 parents dc46a30 + c0e156d commit 2467fd4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 446 deletions.
30 changes: 24 additions & 6 deletions crates/db_schema/src/schema_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -52,6 +52,7 @@ struct MigrationHarnessWrapper<'a> {
conn: &'a mut PgConnection,
#[cfg(test)]
diff_checked_migration_name: Option<String>,
options: &'a Options,
}

impl MigrationHarnessWrapper<'_> {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -128,6 +129,7 @@ pub struct Options {
enable_diff_check: bool,
revert: bool,
run: bool,
print_output: bool,
limit: Option<u64>,
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -227,7 +244,7 @@ pub fn run(options: Options) -> LemmyResult<Branch> {
Branch::ReplaceableSchemaNotRebuilt
};

debug!("Database migrations complete.");
options.print("Database migrations complete.");

Ok(output)
}
Expand Down Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions crates/db_schema/src/source/local_user_language.rs

This file was deleted.

Loading

0 comments on commit 2467fd4

Please sign in to comment.