mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-27 06:41:18 +00:00
parent
6709882e14
commit
d4bdda5d11
6 changed files with 12 additions and 53 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -2833,7 +2833,6 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_with",
|
"serde_with",
|
||||||
"serial_test",
|
"serial_test",
|
||||||
"string-interner",
|
|
||||||
"strum",
|
"strum",
|
||||||
"strum_macros",
|
"strum_macros",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -5229,17 +5228,6 @@ dependencies = [
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "string-interner"
|
|
||||||
version = "0.17.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"hashbrown 0.14.3",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "string_cache"
|
name = "string_cache"
|
||||||
version = "0.8.7"
|
version = "0.8.7"
|
||||||
|
|
|
@ -85,7 +85,6 @@ moka.workspace = true
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serial_test = { workspace = true }
|
serial_test = { workspace = true }
|
||||||
pretty_assertions = { workspace = true }
|
pretty_assertions = { workspace = true }
|
||||||
string-interner = { version = "0.17.0", features = ["inline-more", "backends"] }
|
|
||||||
|
|
||||||
[package.metadata.cargo-machete]
|
[package.metadata.cargo-machete]
|
||||||
ignored = ["strum"]
|
ignored = ["strum"]
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
#[cfg(test)]
|
|
||||||
mod diff_checker;
|
|
||||||
|
|
||||||
use crate::schema::previously_run_sql;
|
use crate::schema::previously_run_sql;
|
||||||
use anyhow::{anyhow, Context};
|
use anyhow::{anyhow, Context};
|
||||||
use diesel::{
|
use diesel::{
|
||||||
|
@ -20,8 +17,6 @@ use diesel_migrations::MigrationHarness;
|
||||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use lemmy_utils::settings::SETTINGS;
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
// In production, include migrations in the binary
|
// In production, include migrations in the binary
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
|
@ -101,40 +96,20 @@ impl<'a, T: MigrationSource<Pg>> MigrationSource<Pg> for MigrationSourceRef<&'a
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct DiffChecker {
|
pub struct Options {
|
||||||
/// Maps a migration name to the schema that exists before the migration is applied
|
|
||||||
schema_before: BTreeMap<String, Schema>,
|
|
||||||
/// Stores strings
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct Schema {
|
|
||||||
indexes: BTreeMap<String, >
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Options<'a> {
|
|
||||||
enable_forbid_diesel_cli_trigger: bool,
|
enable_forbid_diesel_cli_trigger: bool,
|
||||||
revert: bool,
|
revert: bool,
|
||||||
revert_amount: Option<u64>,
|
revert_amount: Option<u64>,
|
||||||
redo_after_revert: bool,
|
redo_after_revert: bool,
|
||||||
#[cfg(test)]
|
|
||||||
diff_checker: Option<&'a mut diff_checker::DiffChecker>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Options<'a> {
|
impl Options {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn enable_forbid_diesel_cli_trigger(mut self) -> Self {
|
fn enable_forbid_diesel_cli_trigger(mut self) -> Self {
|
||||||
self.enable_forbid_diesel_cli_trigger = true;
|
self.enable_forbid_diesel_cli_trigger = true;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn diff_checker(mut self, diff_checker: &'a mut diff_checker::DiffChecker) -> Self {
|
|
||||||
self.diff_checker = Some(diff_checker);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn revert(mut self, amount: Option<u64>) -> Self {
|
pub fn revert(mut self, amount: Option<u64>) -> Self {
|
||||||
self.revert = true;
|
self.revert = true;
|
||||||
self.revert_amount = amount;
|
self.revert_amount = amount;
|
||||||
|
@ -147,15 +122,14 @@ impl<'a> Options<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(options: Options) -> LemmyResult<()> {
|
pub fn run(db_url: &str, options: Options) -> LemmyResult<()> {
|
||||||
let db_url = SETTINGS.get_database_url();
|
|
||||||
|
|
||||||
// Migrations don't support async connection, and this function doesn't need to be async
|
// 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 new_sql = REPLACEABLE_SCHEMA.join("\n");
|
let new_sql = REPLACEABLE_SCHEMA.join("\n");
|
||||||
|
|
||||||
let migration_source = get_migration_source();
|
let migration_source = get_migration_source();
|
||||||
|
|
||||||
let migration_source_ref = MigrationSourceRef(&migration_source);
|
let migration_source_ref = MigrationSourceRef(&migration_source);
|
||||||
|
|
||||||
// If possible, skip locking the migrations table and recreating the "r" schema, so
|
// If possible, skip locking the migrations table and recreating the "r" schema, so
|
||||||
|
@ -254,26 +228,24 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[serial]
|
#[serial]
|
||||||
fn test_schema_setup() -> LemmyResult<()> {
|
fn test_schema_setup() -> LemmyResult<()> {
|
||||||
let db_url = SETTINGS.get_database_url();
|
let url = SETTINGS.get_database_url();
|
||||||
let mut conn = PgConnection::establish(&db_url)?;
|
let mut conn = PgConnection::establish(&url)?;
|
||||||
let diff_checker = DiffChecker::default();
|
|
||||||
let options = || Options::default().diff_checker(&mut diff_checker);
|
|
||||||
|
|
||||||
// Start with consistent state by dropping everything
|
// Start with consistent state by dropping everything
|
||||||
conn.batch_execute("DROP OWNED BY CURRENT_USER;")?;
|
conn.batch_execute("DROP OWNED BY CURRENT_USER;")?;
|
||||||
|
|
||||||
// Run and revert all migrations, ensuring there's no mistakes in any down.sql file
|
// Run and revert all migrations, ensuring there's no mistakes in any down.sql file
|
||||||
run(options())?;
|
run(&url, Options::default())?;
|
||||||
run(options().revert(None))?;
|
run(&url, Options::default().revert(None))?;
|
||||||
|
|
||||||
// TODO also don't drop r, and maybe just directly call the migrationharness method here
|
// TODO also don't drop r, and maybe just directly call the migrationharness method here
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
run(options().enable_forbid_diesel_cli_trigger()),
|
run(&url, Options::default().enable_forbid_diesel_cli_trigger()),
|
||||||
Err(e) if e.to_string().contains("lemmy_server")
|
Err(e) if e.to_string().contains("lemmy_server")
|
||||||
));
|
));
|
||||||
|
|
||||||
// Previous run shouldn't stop this one from working
|
// Previous run shouldn't stop this one from working
|
||||||
run(options())?;
|
run(&url, Options::default())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -427,7 +427,7 @@ pub async fn build_db_pool() -> LemmyResult<ActualDbPool> {
|
||||||
}))
|
}))
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
crate::schema_setup::run(Default::default())?;
|
crate::schema_setup::run(&db_url, Default::default())?;
|
||||||
|
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,7 +133,7 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> {
|
||||||
MigrationSubcommand::Run => schema_setup::Options::default(),
|
MigrationSubcommand::Run => schema_setup::Options::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
schema_setup::run(options)?;
|
schema_setup::run(&SETTINGS.get_database_url(), options)?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue