-
Notifications
You must be signed in to change notification settings - Fork 96
Extend user loading from the DB #1908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| use crate::github::User; | ||
| use anyhow::Context; | ||
| use tokio_postgres::Client as DbClient; | ||
|
|
||
| /// Add a new user. | ||
| /// If an user already exists, updates their username. | ||
| pub async fn record_username(db: &DbClient, user_id: u64, username: &str) -> anyhow::Result<()> { | ||
| db.execute( | ||
| r" | ||
| INSERT INTO users (user_id, username) VALUES ($1, $2) | ||
| ON CONFLICT (user_id) | ||
| DO UPDATE SET username = $2", | ||
| &[&(user_id as i64), &username], | ||
| ) | ||
| .await | ||
| .context("inserting user id / username")?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Return a user from the DB. | ||
| pub async fn get_user(db: &DbClient, user_id: u64) -> anyhow::Result<Option<User>> { | ||
| let row = db | ||
| .query_opt( | ||
| r" | ||
| SELECT username | ||
| FROM users | ||
| WHERE user_id = $1;", | ||
| &[&(user_id as i64)], | ||
| ) | ||
| .await | ||
| .context("cannot load user from DB")?; | ||
| Ok(row.map(|row| { | ||
| let username: &str = row.get(0); | ||
| User { | ||
| id: user_id, | ||
| login: username.to_string(), | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::db::users::{get_user, record_username}; | ||
| use crate::tests::run_test; | ||
|
|
||
| #[tokio::test] | ||
| async fn update_username_on_conflict() { | ||
| run_test(|ctx| async { | ||
| let db = ctx.db_client().await; | ||
|
|
||
| record_username(&db, 1, "Foo").await?; | ||
| record_username(&db, 1, "Bar").await?; | ||
|
|
||
| assert_eq!(get_user(&db, 1).await?.unwrap().login, "Bar"); | ||
|
|
||
| Ok(ctx) | ||
| }) | ||
| .await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you
SELECT *then you can justOk(row.into())but it's a matter of preference ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't currently implement
From<tokio_postgres::Row> for Useranywhere :( Which goes back to my comment that we kind of misuse the GitHub user to also mean the database user.Introducing a separate type would mean doing some conversions, but I guess that if we want to keep the
userstable, it's the Right Thing™ to do, in the long term. What do you think? :)