Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/crates_io_database/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ diesel::table! {
/// (Automatically generated by Diesel.)
avatar -> Nullable<Varchar>,
/// 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<Int4>,
org_id -> Int4,
}
}

Expand Down
1 change: 1 addition & 0 deletions migrations/2025-02-13-084853_not-null-org-id/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table teams alter column org_id drop not null;
19 changes: 19 additions & 0 deletions migrations/2025-02-13-084853_not-null-org-id/up.sql
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 2 additions & 12 deletions src/models/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Team {
pub name: Option<String>,
pub avatar: Option<String>,
/// The GitHub Organization ID this team sits under
pub org_id: Option<i32>,
pub org_id: i32,
}

#[derive(Insertable, AsChangeset, Debug, Builder)]
Expand Down Expand Up @@ -169,17 +169,7 @@ impl Team {
gh_login: &str,
token: &AccessToken,
) -> Result<bool, GitHubError> {
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<Vec<Owner>> {
Expand Down
Loading