Skip to content

Commit

Permalink
Remove field from db table
Browse files Browse the repository at this point in the history
  • Loading branch information
apiraino committed Jan 23, 2024
1 parent 9d7bfcd commit e375877
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ CREATE UNIQUE INDEX jobs_name_scheduled_at_unique_index
CREATE table review_prefs (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id BIGINT REFERENCES users(user_id),
assigned_prs INT[] NOT NULL DEFAULT array[]::INT[],
num_assigned_prs INTEGER
assigned_prs INT[] NOT NULL DEFAULT array[]::INT[]
);
CREATE UNIQUE INDEX review_prefs_user_id ON review_prefs(user_id);
",
Expand Down
27 changes: 9 additions & 18 deletions src/handlers/review_work_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn get_pr_assignees(
issue_num: i32,
) -> anyhow::Result<Vec<TeamMemberWorkQueue>> {
let q = "
SELECT u.username, r.*
SELECT u.username, r.*, array_length(assigned_prs, 1) as num_assigned_prs
FROM review_prefs r
JOIN users u on u.user_id=r.user_id
WHERE $1 = ANY (assigned_prs)";
Expand All @@ -36,27 +36,19 @@ async fn update_team_member_workqueue(
) -> anyhow::Result<TeamMemberWorkQueue> {
let q = "
UPDATE review_prefs r
SET assigned_prs = $2, num_assigned_prs = $3
SET assigned_prs = $2
FROM users u
WHERE r.user_id=$1 AND u.user_id=r.user_id
RETURNING u.username, r.*";
let num_assigned_prs = assignee.assigned_prs.len() as i32;
RETURNING u.username, r.*, array_length(assigned_prs, 1) as num_assigned_prs";
let rec = db
.query_one(
q,
&[&assignee.user_id, &assignee.assigned_prs, &num_assigned_prs],
)
.query_one(q, &[&assignee.user_id, &assignee.assigned_prs])
.await
.context("Update DB error")?;
Ok(rec.into())
}

/// Add a new user (if not existing)
async fn maybe_create_team_member(
db: &DbClient,
user_id: i64,
username: &str,
) -> anyhow::Result<u64> {
async fn ensure_team_member(db: &DbClient, user_id: i64, username: &str) -> anyhow::Result<u64> {
let q = "
INSERT INTO users (user_id, username) VALUES ($1, $2)
ON CONFLICT DO NOTHING";
Expand All @@ -73,12 +65,11 @@ async fn upsert_team_member_workqueue(
user_id: i64,
pr: i32,
) -> anyhow::Result<u64, anyhow::Error> {
let q =
"
let q = "
INSERT INTO review_prefs
(user_id, assigned_prs, num_assigned_prs) VALUES ($1, $2, 1)
(user_id, assigned_prs) VALUES ($1, $2)
ON CONFLICT (user_id)
DO UPDATE SET assigned_prs = array_append(review_prefs.assigned_prs, $3), num_assigned_prs = review_prefs.num_assigned_prs + 1
DO UPDATE SET assigned_prs = array_append(review_prefs.assigned_prs, $3)
WHERE review_prefs.user_id=$1";
let pr_v = vec![pr];
db.execute(q, &[&user_id, &pr_v, &pr])
Expand Down Expand Up @@ -142,7 +133,7 @@ pub(super) async fn handle_input<'a>(
for u in event.issue.assignees.iter() {
let user_id = u.id.expect("Github user was expected! Please investigate.");

if let Err(err) = maybe_create_team_member(&db_client, user_id, &u.login).await {
if let Err(err) = ensure_team_member(&db_client, user_id, &u.login).await {
log::error!("Failed to create user in DB: this PR assignment won't be tracked.");
return Err(err);
}
Expand Down
2 changes: 1 addition & 1 deletion triagebot.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ allow-unauthenticated = ["bug", "invalid", "question", "enhancement"]
[review-work-queue]
# A list of teams that will use the new PR review assignment workflow.
# Team names match https://github.com/rust-lang/team/tree/master/teams
enabled-for-teams=["compiler", "compiler-contributors"]
enabled-for-teams = ["compiler", "compiler-contributors"]

[note]

Expand Down

0 comments on commit e375877

Please sign in to comment.