Skip to content

Commit 12c64bc

Browse files
committed
Auto merge of #1840 - smarnach:fk-badges-crate-id, r=sgrif
Add missing foreign key constraint for columns badges.crate_id. Since we may already have entries in the `badges` table referring to crates that don't exist, we need to delete these entries before creating the foreign key constraint. This is a destructive operation in the sense that it cannot be reverted, but it should only delete rows that should have been deleted together with the crates they belong to. We have a test to verify that all columns called `crate_id` are actually foreign keys referring to `crates.id`. However, the query for the relevant columns contains the filter `contype = 'f'`, effectively limiting the result to columns that already have foreign key constraints. This change fixes the query to also allow `contype IS NULL`. In addition, I modified the query to only verify tables in the schema `public`. This is useful for an integration test for the database dumps in #1800.
2 parents 6f5e217 + 7ef85f5 commit 12c64bc

File tree

5 files changed

+10
-2
lines changed

5 files changed

+10
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE badges DROP CONSTRAINT fk_badges_crate_id;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DELETE FROM badges WHERE crate_id NOT IN (SELECT id FROM crates);
2+
ALTER TABLE badges
3+
ADD CONSTRAINT fk_badges_crate_id FOREIGN KEY (crate_id) REFERENCES crates(id) ON DELETE CASCADE;

src/schema.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ index df884e4..18e08cd 100644
5656
/// Representation of the `reserved_crate_names` table.
5757
///
5858
@@ -881,23 +901,25 @@ table! {
59-
}
6059

6160
joinable!(api_tokens -> users (user_id));
61+
joinable!(badges -> crates (crate_id));
6262
joinable!(crate_owner_invitations -> crates (crate_id));
6363
joinable!(crate_owners -> crates (crate_id));
6464
-joinable!(crate_owners -> users (created_by));

src/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ table! {
961961
}
962962

963963
joinable!(api_tokens -> users (user_id));
964+
joinable!(badges -> crates (crate_id));
964965
joinable!(crate_owner_invitations -> crates (crate_id));
965966
joinable!(crate_owners -> crates (crate_id));
966967
joinable!(crate_owners -> teams (owner_id));

src/tests/load_foreign_key_constraints.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ SELECT relname, conname, pg_get_constraintdef(pg_constraint.oid, true) AS defini
44
FROM pg_attribute
55
INNER JOIN pg_class ON pg_class.oid = attrelid
66
LEFT JOIN pg_constraint ON pg_class.oid = conrelid AND ARRAY[attnum] = conkey
7+
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
78
WHERE attname = $1
8-
AND contype = 'f'
9+
AND relkind = 'r'
10+
AND (contype IS NULL OR contype = 'f')
11+
AND nspname = 'public';

0 commit comments

Comments
 (0)