Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions crates/diesel_utils/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::schema_setup::MIGRATIONS;
use deadpool::Runtime;
use diesel::result::{
ConnectionError,
ConnectionResult,
Error::{self as DieselError, QueryBuilderError},
use diesel::{
connection::SimpleConnection,
result::{
ConnectionError,
ConnectionResult,
Error::{self as DieselError, QueryBuilderError},
},
};
use diesel_async::{
AsyncConnection,
Expand All @@ -14,6 +18,7 @@ use diesel_async::{
},
scoped_futures::ScopedBoxFuture,
};
use diesel_migrations::MigrationHarness;
use futures_util::{FutureExt, future::BoxFuture};
use lemmy_utils::{
error::{LemmyError, LemmyResult},
Expand All @@ -37,7 +42,7 @@ use std::{
sync::Arc,
time::Duration,
};
use tracing::error;
use tracing::{debug, error};

pub type ActualDbPool = Pool<AsyncPgConnection>;

Expand Down Expand Up @@ -179,7 +184,27 @@ pub fn build_db_pool() -> LemmyResult<ActualDbPool> {
}))
.build()?;

crate::schema_setup::run(crate::schema_setup::Options::default().run(), &db_url)?;
let mut inner_harness = crate::schema_setup::MigrationHarnessWrapper::new(&db_url)?;
let mut harness =
TimedHarnessWithOutput::write_to_tracing(&mut inner_harness, tracing::Level::DEBUG);

// If possible, skip getting a lock and recreating the "r" schema, so lemmy_server processes in a
// horizontally scaled setup can start without causing locks
if harness.need_schema_setup()? {
// Block concurrent attempts to run migrations until `conn` is closed (otherwise, in a
// horizontally scaled setup, it's possible for multiple processes to try running the same
// migration, which would throw a unique violation error)
debug!("Waiting for lock...");
harness.conn.batch_execute("SELECT pg_advisory_lock(0);")?;

debug!("Running Database migrations (This may take a long time)...");
harness
.run_pending_migrations(MIGRATIONS)
.map_err(anyhow::Error::from_boxed)?;
harness.run_replaceable_schema()?;

debug!("Database migrations complete.");
}

Ok(pool)
}
Expand Down
12 changes: 9 additions & 3 deletions crates/diesel_utils/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/// Very minimal wrapper around `lemmy_diesel_utils::run` to allow running migrations without
use diesel_migrations::MigrationHarness;

/// Very minimal wrapper to allow running migrations without
/// compiling everything.
fn main() -> anyhow::Result<()> {
if std::env::args().len() > 1 {
anyhow::bail!("To set parameters for running migrations, use the lemmy_server command.");
}

lemmy_diesel_utils::schema_setup::run(
lemmy_diesel_utils::schema_setup::Options::default().run(),
// todo: set the application_name
let mut harness = lemmy_diesel_utils::schema_setup::MigrationHarnessWrapper::new(
&std::env::var("LEMMY_DATABASE_URL")?,
)?;
harness
.run_pending_migrations(MIGRATIONS)
.map_err(lemmy_diesel_utils::schema_setup::convert_err)?;
lemmy_diesel_utils::schema_setup::run_replaceable_schema(&mut harness.conn)?;

Ok(())
}
Loading