diff --git a/crates/crates_io_database/src/schema.rs b/crates/crates_io_database/src/schema.rs index 00a6ccfb642..6e22797e84a 100644 --- a/crates/crates_io_database/src/schema.rs +++ b/crates/crates_io_database/src/schema.rs @@ -761,7 +761,7 @@ diesel::table! { /// (Automatically generated by Diesel.) avatar -> Nullable, /// Unique organization ID on the GitHub API. When organizations are recreated with the same name then they will still get a different ID, so this allows us to avoid potential name reuse attacks. - org_id -> Nullable, + org_id -> Int4, } } diff --git a/migrations/2025-02-13-084853_not-null-org-id/down.sql b/migrations/2025-02-13-084853_not-null-org-id/down.sql new file mode 100644 index 00000000000..3545675f32e --- /dev/null +++ b/migrations/2025-02-13-084853_not-null-org-id/down.sql @@ -0,0 +1 @@ +alter table teams alter column org_id drop not null; diff --git a/migrations/2025-02-13-084853_not-null-org-id/up.sql b/migrations/2025-02-13-084853_not-null-org-id/up.sql new file mode 100644 index 00000000000..06a7c0ff97d --- /dev/null +++ b/migrations/2025-02-13-084853_not-null-org-id/up.sql @@ -0,0 +1,19 @@ +-- Delete crate owners that are associated with teams that have no `org_id` +with broken_crate_owners as ( + select crate_id, owner_id, owner_kind + from crate_owners + left join teams on teams.id = crate_owners.owner_id + where owner_kind = 1 + and teams.org_id is null +) +delete from crate_owners + using broken_crate_owners +where crate_owners.crate_id = broken_crate_owners.crate_id + and crate_owners.owner_id = broken_crate_owners.owner_id + and crate_owners.owner_kind = broken_crate_owners.owner_kind; + +-- Delete teams that have no `org_id` +delete from teams where org_id is null; + +-- Make `org_id` not null +alter table teams alter column org_id set not null; diff --git a/src/models/team.rs b/src/models/team.rs index d8606b93271..f2ca4092631 100644 --- a/src/models/team.rs +++ b/src/models/team.rs @@ -30,7 +30,7 @@ pub struct Team { pub name: Option, pub avatar: Option, /// The GitHub Organization ID this team sits under - pub org_id: Option, + pub org_id: i32, } #[derive(Insertable, AsChangeset, Debug, Builder)] @@ -169,17 +169,7 @@ impl Team { gh_login: &str, token: &AccessToken, ) -> Result { - match self.org_id { - Some(org_id) => { - team_with_gh_id_contains_user(gh_client, org_id, self.github_id, gh_login, token) - .await - } - // This means we don't have an org_id on file for the `self` team. It much - // probably was deleted from github by the time we backfilled the database. - // Short-circuiting to false since a non-existent team cannot contain any - // user - None => Ok(false), - } + team_with_gh_id_contains_user(gh_client, self.org_id, self.github_id, gh_login, token).await } pub async fn owning(krate: &Crate, conn: &mut AsyncPgConnection) -> QueryResult> {