This commit is contained in:
Dull Bananas 2024-05-11 15:32:00 +00:00
parent e8e354c9b2
commit 402ab1414f

View file

@ -1,4 +1,4 @@
use crate::{schema::previously_run_sql}; use crate::schema::previously_run_sql;
use anyhow::Context; use anyhow::Context;
use diesel::{ use diesel::{
connection::SimpleConnection, connection::SimpleConnection,
@ -6,9 +6,10 @@ use diesel::{
update, update,
Connection, Connection,
ExpressionMethods, ExpressionMethods,
NullableExpressionMethods,
PgConnection, PgConnection,
QueryDsl, QueryDsl,
RunQueryDsl,NullableExpressionMethods RunQueryDsl,
}; };
use diesel_migrations::{EmbeddedMigrations, MigrationHarness}; use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -64,8 +65,8 @@ pub fn run(db_url: &str) -> Result<(), LemmyError> {
if sql_unchanged { if sql_unchanged {
debug_assert_eq!( debug_assert_eq!(
unfiltered_pending_migrations unfiltered_pending_migrations
.get(0) .get(0)
.map(|m| m.name().version()), .map(|m| m.name().version()),
Some(FORBID_DIESEL_CLI_MIGRATION_VERSION.into()) Some(FORBID_DIESEL_CLI_MIGRATION_VERSION.into())
); );
return Ok(()); return Ok(());
@ -116,24 +117,22 @@ pub fn run(db_url: &str) -> Result<(), LemmyError> {
let name = migration.name(); let name = migration.name();
// TODO measure time on database // TODO measure time on database
let start_time = Instant::now(); let start_time = Instant::now();
conn.run_migration(migration) conn.run_migration(migration).map_err(|e| anyhow::anyhow!("Couldn't run migration {name}: {e}"))?;
.map_err(|e| anyhow::anyhow!("Couldn't run migration {name}: {e}"))?; let duration = start_time.elapsed().as_millis();
let duration = start_time.elapsed().as_millis();
info!("{duration}ms {name}"); info!("{duration}ms {name}");
} }
// Run replaceable_schema // Run replaceable_schema
conn.batch_execute(&new_sql) conn.batch_execute(&new_sql).context("Couldn't run SQL files in crates/db_schema/replaceable_schema")?;
.context("Couldn't run SQL files in crates/db_schema/replaceable_schema")?;
let num_rows_updated = update(previously_run_sql::table) let num_rows_updated = update(previously_run_sql::table)
.set(previously_run_sql::content.eq(new_sql)) .set(previously_run_sql::content.eq(new_sql))
.execute(conn)?; .execute(conn)?;
debug_assert_eq!(num_rows_updated, 1); debug_assert_eq!(num_rows_updated, 1);
Ok(()) Ok(())
})?; })?;
info!("Database migrations complete."); info!("Database migrations complete.");
Ok(()) Ok(())