This commit is contained in:
Dull Bananas 2025-03-11 20:48:56 -07:00
parent 28bb1c7497
commit 7d5652945f
2 changed files with 44 additions and 19 deletions

View file

@ -278,10 +278,7 @@ fn run_selected_migrations(
conn: &mut PgConnection,
options: &Options,
) -> diesel::migration::Result<()> {
let mut wrapper = MigrationHarnessWrapper {
conn,
options,
};
let mut wrapper = MigrationHarnessWrapper { conn, options };
if options.revert {
if let Some(limit) = options.limit {

View file

@ -1,10 +1,12 @@
#![cfg(test)]
#![expect(clippy::expect_used)]
use lemmy_utils::settings::SETTINGS;
use std::collections::HashSet;
use std::{
borrow::Cow,
collections::btree_set::{self, BTreeSet},
collections::{
btree_set::{self, BTreeSet},
HashSet,
},
process::{Command, Stdio},
};
@ -53,11 +55,35 @@ pub fn check_dump_diff(before: String, after: String, label: &str) {
return;
}
let mut match_before_len = before.as_bytes().into_iter().zip(after.as_bytes()).position(|(a, b)| a != b).unwrap_or(0);
let mut match_after_len = before.as_bytes().into_iter().rev().zip(after.as_bytes().into_iter().rev()).position(|(a, b)| a != b).unwrap_or(0);
match_before_len = before.get(0..match_before_len).and_then(|s|s.rfind("\n\n")).unwrap_or(0);
match_after_len -= before.get((before.len() - match_after_len)..).and_then(|s|s.find("\n\n")).unwrap_or(0);
let [before_chunks, after_chunks] = [&before, &after].map(|dump| dump.get(match_before_len..(dump.len()-match_after_len)).unwrap_or(dump.as_str()).split("\n\n").filter_map(normalize_chunk).collect::<Vec<_>>());
let mut match_before_len = before
.as_bytes()
.into_iter()
.zip(after.as_bytes())
.position(|(a, b)| a != b)
.unwrap_or(0);
let mut match_after_len = before
.as_bytes()
.into_iter()
.rev()
.zip(after.as_bytes().into_iter().rev())
.position(|(a, b)| a != b)
.unwrap_or(0);
match_before_len = before
.get(0..match_before_len)
.and_then(|s| s.rfind("\n\n"))
.unwrap_or(0);
match_after_len -= before
.get((before.len() - match_after_len)..)
.and_then(|s| s.find("\n\n"))
.unwrap_or(0);
let [before_chunks, after_chunks] = [&before, &after].map(|dump| {
dump
.get(match_before_len..(dump.len() - match_after_len))
.unwrap_or(dump.as_str())
.split("\n\n")
.filter_map(normalize_chunk)
.collect::<Vec<_>>()
});
let mut before_diff = BTreeSet::new();
let mut after_diff = BTreeSet::new();
let diff_results = diff::slice(&before_chunks, &after_chunks);
@ -65,8 +91,12 @@ pub fn check_dump_diff(before: String, after: String, label: &str) {
for res in diff_results {
match res {
diff::Result::Both(_, _) => (),
diff::Result::Left(chunk) => {before_diff.insert((&**chunk));},
diff::Result::Right(chunk) =>{ after_diff.insert((&**chunk));},
diff::Result::Left(chunk) => {
before_diff.insert((&**chunk));
}
diff::Result::Right(chunk) => {
after_diff.insert((&**chunk));
}
}
}
let diffs = [before_diff, after_diff];
@ -174,9 +204,9 @@ fn similarity(chunk: &str, other_chunk: &str) -> usize {
fn normalize_chunk(mut chunk: &str) -> Option<Cow<'_, str>> {
chunk = chunk.trim();
while let Some(s) = remove_skipped_item_from_beginning(chunk) {
chunk = s.trim_start();
}
if chunk.is_empty() ||
chunk = s.trim_start();
}
if chunk.is_empty() ||
// Skip old views and fast table triggers
chunk.strip_prefix("CREATE ").is_some_and(|c| {
c
@ -186,12 +216,10 @@ fn normalize_chunk(mut chunk: &str) -> Option<Cow<'_, str>> {
|| c.strip_prefix("FUNCTION public.")
.and_then(after_skipped_trigger_name)
.is_some_and(|a| a.starts_with('('))
||
||
c.strip_prefix("TRIGGER ")
.and_then(after_skipped_trigger_name)
.is_some_and(|a| a.starts_with(' '))
}) {
return None;
}