add Options with disable_migrations field for test

This commit is contained in:
Dull Bananas 2024-05-13 02:51:05 +00:00
parent 22ac8c5bfc
commit 27b289c339
3 changed files with 38 additions and 8 deletions

View file

@ -38,8 +38,14 @@ fn get_pending_migrations(conn: &mut PgConnection) -> LemmyResult<Vec<Box<dyn Mi
) )
} }
pub fn run(db_url: &str) -> LemmyResult<()> { #[derive(Default)]
// Migrations don't support async connection pub struct Options {
/// Only for testing
disable_migrations: bool,
}
pub fn run(db_url: &str, options: &Options) -> LemmyResult<()> {
// Migrations don't support async connection, and this function doesn't need to be async
let mut conn = PgConnection::establish(db_url).with_context(|| "Error connecting to database")?; let mut conn = PgConnection::establish(db_url).with_context(|| "Error connecting to database")?;
let test_enabled = std::env::var("LEMMY_TEST_MIGRATIONS") let test_enabled = std::env::var("LEMMY_TEST_MIGRATIONS")
@ -80,9 +86,14 @@ pub fn run(db_url: &str) -> LemmyResult<()> {
let pending_migrations = get_pending_migrations(conn)?; let pending_migrations = get_pending_migrations(conn)?;
// Drop `r` schema and disable the trigger that prevents the Diesel CLI from running migrations // Drop `r` schema and disable the trigger that prevents the Diesel CLI from running migrations
conn.batch_execute( let enable_migrations = if options.disable_migrations {
"DROP SCHEMA IF EXISTS r CASCADE; SET LOCAL lemmy.enable_migrations TO 'on';", ""
)?; } else {
"SET LOCAL lemmy.enable_migrations TO 'on';"
};
conn.batch_execute(&format!(
"DROP SCHEMA IF EXISTS r CASCADE;{enable_migrations}"
))?;
for migration in &pending_migrations { for migration in &pending_migrations {
let name = migration.name(); let name = migration.name();
@ -112,3 +123,20 @@ pub fn run(db_url: &str) -> LemmyResult<()> {
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use lemmy_utils::{error::LemmyResult, settings::SETTINGS};
#[test]
fn test_schema_setup() -> LemmyResult<()> {
let mut options = super::Options::default();
let db_url = SETTINGS.get_database_url();
// Test the forbid_diesel_cli trigger
options.disable_migrations = true;
super::run(&db_url, &options).expect_err("forbid_diesel_cli trigger should throw error");
Ok(())
}
}

View file

@ -427,7 +427,7 @@ pub async fn build_db_pool() -> LemmyResult<ActualDbPool> {
})) }))
.build()?; .build()?;
crate::schema_setup::run(&db_url)?; crate::schema_setup::run(&db_url, &Default::default())?;
Ok(pool) Ok(pool)
} }

View file

@ -40,7 +40,7 @@ use lemmy_apub::{
VerifyUrlData, VerifyUrlData,
FEDERATION_HTTP_FETCH_LIMIT, FEDERATION_HTTP_FETCH_LIMIT,
}; };
use lemmy_db_schema::{source::secret::Secret, utils::build_db_pool}; use lemmy_db_schema::{schema_setup, source::secret::Secret, utils::build_db_pool};
use lemmy_federate::{start_stop_federation_workers_cancellable, Opts}; use lemmy_federate::{start_stop_federation_workers_cancellable, Opts};
use lemmy_routes::{feeds, images, nodeinfo, webfinger}; use lemmy_routes::{feeds, images, nodeinfo, webfinger};
use lemmy_utils::{ use lemmy_utils::{
@ -129,11 +129,13 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> {
println!("Lemmy v{VERSION}"); println!("Lemmy v{VERSION}");
if let Some(CmdSubcommand::Migration { subcommand }) = args.subcommand { if let Some(CmdSubcommand::Migration { subcommand }) = args.subcommand {
let mut options = schema_setup::Options::default();
match subcommand { match subcommand {
MigrationSubcommand::Run => {} MigrationSubcommand::Run => {}
} }
lemmy_db_schema::schema_setup::run(&SETTINGS.get_database_url())?; schema_setup::run(&SETTINGS.get_database_url(), &options)?;
return Ok(()); return Ok(());
} }