Skip to content

Commit 659c1c8

Browse files
authored
database: Alter teams.org_id column to be not null (#10572)
Out of the roughly 1100 teams in our database only 10 have `org_id: NULL`. These teams belong to GitHub organizations that don't exist anymore, so the corresponding teams are essentially useless. See #2796 (comment) for more context. Since the nullability of the column makes our code more complicated than it needs to be, this commit proposes to remove these teams and turn the column into a `not null` column.
1 parent af9b565 commit 659c1c8

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

crates/crates_io_database/src/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ diesel::table! {
761761
/// (Automatically generated by Diesel.)
762762
avatar -> Nullable<Varchar>,
763763
/// 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.
764-
org_id -> Nullable<Int4>,
764+
org_id -> Int4,
765765
}
766766
}
767767

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table teams alter column org_id drop not null;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Delete crate owners that are associated with teams that have no `org_id`
2+
with broken_crate_owners as (
3+
select crate_id, owner_id, owner_kind
4+
from crate_owners
5+
left join teams on teams.id = crate_owners.owner_id
6+
where owner_kind = 1
7+
and teams.org_id is null
8+
)
9+
delete from crate_owners
10+
using broken_crate_owners
11+
where crate_owners.crate_id = broken_crate_owners.crate_id
12+
and crate_owners.owner_id = broken_crate_owners.owner_id
13+
and crate_owners.owner_kind = broken_crate_owners.owner_kind;
14+
15+
-- Delete teams that have no `org_id`
16+
delete from teams where org_id is null;
17+
18+
-- Make `org_id` not null
19+
alter table teams alter column org_id set not null;

src/models/team.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Team {
3030
pub name: Option<String>,
3131
pub avatar: Option<String>,
3232
/// The GitHub Organization ID this team sits under
33-
pub org_id: Option<i32>,
33+
pub org_id: i32,
3434
}
3535

3636
#[derive(Insertable, AsChangeset, Debug, Builder)]
@@ -169,17 +169,7 @@ impl Team {
169169
gh_login: &str,
170170
token: &AccessToken,
171171
) -> Result<bool, GitHubError> {
172-
match self.org_id {
173-
Some(org_id) => {
174-
team_with_gh_id_contains_user(gh_client, org_id, self.github_id, gh_login, token)
175-
.await
176-
}
177-
// This means we don't have an org_id on file for the `self` team. It much
178-
// probably was deleted from github by the time we backfilled the database.
179-
// Short-circuiting to false since a non-existent team cannot contain any
180-
// user
181-
None => Ok(false),
182-
}
172+
team_with_gh_id_contains_user(gh_client, self.org_id, self.github_id, gh_login, token).await
183173
}
184174

185175
pub async fn owning(krate: &Crate, conn: &mut AsyncPgConnection) -> QueryResult<Vec<Owner>> {

0 commit comments

Comments
 (0)