Removing an existing deferrable constraint, and fail test if any constraint is deferrable. (#5806)

* Removing an existing deferrable constraint.

- Also adding a check to make sure the final dump contains no DEFERs.

* Spelling error
This commit is contained in:
Dessalines 2025-06-20 03:49:44 -04:00 committed by GitHub
parent fb762a3f9b
commit e07e251ada
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 2 deletions

View file

@ -13,7 +13,7 @@ use std::{
/// Returns almost all things currently in the database, represented as SQL statements that would
/// recreate them.
pub fn get_dump() -> String {
pub(crate) fn get_dump() -> String {
let db_url = SETTINGS.get_database_url();
let output = Command::new("pg_dump")
.args([
@ -58,7 +58,7 @@ pub fn get_dump() -> String {
/// not `dumps[1]` to `dumps[0]`. This requires the two `dumps` elements being in an order that fits
/// with `label_of_change_from_0_to_1`. This does not necessarily match the order in which the dumps
/// were created.
pub fn check_dump_diff(dumps: [&str; 2], label_of_change_from_0_to_1: &str) {
pub(crate) fn check_dump_diff(dumps: [&str; 2], label_of_change_from_0_to_1: &str) {
let [sorted_statements_in_0, sorted_statements_in_1] = dumps.map(|dump| {
dump
.split("\n\n")
@ -195,6 +195,13 @@ fn display_change([before, after]: [&str; 2]) -> impl Iterator<Item = &str> {
})
}
/// Makes sure the after dump does not contain any DEFERRABLE constraints.
pub(crate) fn deferr_constraint_check(dump: &str) {
if dump.contains(" DEFERR") {
panic!("Schema should not have DEFER constraints.")
}
}
// `#[cfg(test)]` would be redundant here
mod tests {
#[test]

View file

@ -234,6 +234,8 @@ pub fn run(options: Options) -> LemmyResult<Branch> {
let after = diff_check::get_dump();
diff_check::check_dump_diff([&before, &after], "The code in crates/db_schema/replaceable_schema incorrectly created or modified things outside of the `r` schema, causing these changes to be left behind after dropping the schema:");
diff_check::deferr_constraint_check(&after);
}
run_replaceable_schema(&mut conn)?;

View file

@ -0,0 +1,3 @@
ALTER TABLE community
ALTER CONSTRAINT community_instance_id_fkey DEFERRABLE INITIALLY DEFERRED;

View file

@ -0,0 +1,6 @@
-- We should remove existing deferrable constraints, as they're potentially dangerous.
--
-- This is the only one I could find after doing a DB dump.
ALTER TABLE community
ALTER CONSTRAINT community_instance_id_fkey NOT DEFERRABLE;