Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions crates/crates_io_database/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct User {
pub account_lock_until: Option<DateTime<Utc>>,
pub is_admin: bool,
pub publish_notifications: bool,
pub username: Option<String>,
}

impl User {
Expand Down Expand Up @@ -85,6 +86,7 @@ pub struct NewUser<'a> {
pub gh_id: i32,
pub gh_login: &'a str,
pub name: Option<&'a str>,
pub username: Option<&'a str>,
pub gh_avatar: Option<&'a str>,
pub gh_access_token: &'a str,
}
Expand Down Expand Up @@ -114,6 +116,7 @@ impl NewUser<'_> {
.do_update()
.set((
users::gh_login.eq(excluded(users::gh_login)),
users::username.eq(excluded(users::username)),
users::name.eq(excluded(users::name)),
users::gh_avatar.eq(excluded(users::gh_avatar)),
users::gh_access_token.eq(excluded(users::gh_access_token)),
Expand Down
6 changes: 6 additions & 0 deletions crates/crates_io_database/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,12 @@ diesel::table! {
is_admin -> Bool,
/// Whether or not the user wants to receive notifications when a package they own is published
publish_notifications -> Bool,
/// The `username` column of the `users` table.
///
/// Its SQL type is `Nullable<Varchar>`.
///
/// (Automatically generated by Diesel.)
username -> Nullable<Varchar>,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/crates_io_database_dump/src/dump-db.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ account_lock_reason = "private"
account_lock_until = "private"
is_admin = "private"
publish_notifications = "private"
username = "public"
[users.column_defaults]
gh_access_token = "''"

Expand Down
2 changes: 1 addition & 1 deletion ...o_database_dump/src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY;
\copy "metadata" ("total_downloads") TO 'data/metadata.csv' WITH CSV HEADER
\copy "reserved_crate_names" ("name") TO 'data/reserved_crate_names.csv' WITH CSV HEADER
\copy "teams" ("avatar", "github_id", "id", "login", "name", "org_id") TO 'data/teams.csv' WITH CSV HEADER
\copy (SELECT "gh_avatar", "gh_id", "gh_login", "id", "name" FROM "users" WHERE id in ( SELECT owner_id AS user_id FROM crate_owners WHERE NOT deleted AND owner_kind = 0 UNION SELECT published_by as user_id FROM versions )) TO 'data/users.csv' WITH CSV HEADER
\copy (SELECT "gh_avatar", "gh_id", "gh_login", "id", "name", "username" FROM "users" WHERE id in ( SELECT owner_id AS user_id FROM crate_owners WHERE NOT deleted AND owner_kind = 0 UNION SELECT published_by as user_id FROM versions )) TO 'data/users.csv' WITH CSV HEADER

\copy "crates_categories" ("category_id", "crate_id") TO 'data/crates_categories.csv' WITH CSV HEADER
\copy "crates_keywords" ("crate_id", "keyword_id") TO 'data/crates_keywords.csv' WITH CSV HEADER
Expand Down
2 changes: 1 addition & 1 deletion ...o_database_dump/src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ BEGIN;
\copy "metadata" ("total_downloads") FROM 'data/metadata.csv' WITH CSV HEADER
\copy "reserved_crate_names" ("name") FROM 'data/reserved_crate_names.csv' WITH CSV HEADER
\copy "teams" ("avatar", "github_id", "id", "login", "name", "org_id") FROM 'data/teams.csv' WITH CSV HEADER
\copy "users" ("gh_avatar", "gh_id", "gh_login", "id", "name") FROM 'data/users.csv' WITH CSV HEADER
\copy "users" ("gh_avatar", "gh_id", "gh_login", "id", "name", "username") FROM 'data/users.csv' WITH CSV HEADER
\copy "crates_categories" ("category_id", "crate_id") FROM 'data/crates_categories.csv' WITH CSV HEADER
\copy "crates_keywords" ("crate_id", "keyword_id") FROM 'data/crates_keywords.csv' WITH CSV HEADER
\copy "crate_owners" ("crate_id", "created_at", "created_by", "owner_id", "owner_kind") FROM 'data/crate_owners.csv' WITH CSV HEADER
Expand Down
4 changes: 4 additions & 0 deletions migrations/2025-02-19-013433_add_username_to_user/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP INDEX IF EXISTS lower_username;

ALTER TABLE users
DROP COLUMN username;
6 changes: 6 additions & 0 deletions migrations/2025-02-19-013433_add_username_to_user/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE users
-- The column needs to be nullable for this migration to be fast; can be changed to non-nullable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we probably could get away with making this non-null with a default of '', but Postgres can handle changes in nullability without a table rewrite, so I don't know that it would really matter in practice.

-- after backfill of all records.
ADD COLUMN username VARCHAR;

CREATE INDEX lower_username ON users (lower(username));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a benchmark, but I suspect we'd save some work by creating this after the column has been populated.

2 changes: 1 addition & 1 deletion src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn list_crate_owner_invitations_for_user(
.iter()
.find(|u| u.id == private.inviter_id)
.ok_or_else(|| internal(format!("missing user {}", private.inviter_id)))?
.login
.username
.clone(),
invitee_id: private.invitee_id,
inviter_id: private.inviter_id,
Expand Down
1 change: 1 addition & 0 deletions src/controllers/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub async fn save_user_to_database(
let new_user = NewUser::builder()
.gh_id(user.id)
.gh_login(&user.login)
.username(&user.login)
.maybe_name(user.name.as_deref())
.maybe_gh_avatar(user.avatar_url.as_deref())
.gh_access_token(access_token)
Expand Down
1 change: 1 addition & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ mod tests {
let user_id = diesel::insert_into(users::table)
.values((
users::name.eq("user1"),
users::username.eq("user1"),
users::gh_login.eq("user1"),
users::gh_id.eq(42),
users::gh_access_token.eq("some random token"),
Expand Down
1 change: 1 addition & 0 deletions src/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ mod tests {
NewUser::builder()
.gh_id(0)
.gh_login(gh_login)
.username(gh_login)
.gh_access_token("some random token")
.build()
.insert(conn)
Expand Down
25 changes: 22 additions & 3 deletions src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/openapi.rs
expression: response.json()
snapshot_kind: text
---
{
"components": {
Expand Down Expand Up @@ -117,7 +118,7 @@ expression: response.json()
"type": "boolean"
},
"login": {
"description": "The user's login name.",
"description": "The user's GitHub login.",
"example": "ghost",
"type": "string"
},
Expand All @@ -141,11 +142,17 @@ expression: response.json()
"string",
"null"
]
},
"username": {
"description": "The user's crates.io username.",
"example": "ghost",
"type": "string"
}
},
"required": [
"id",
"login",
"username",
"email_verified",
"email_verification_sent",
"is_admin",
Expand Down Expand Up @@ -707,7 +714,7 @@ expression: response.json()
"type": "string"
},
"login": {
"description": "The login name of the team or user.",
"description": "The GitHub login of the team or user.",
"example": "ghost",
"type": "string"
},
Expand All @@ -726,11 +733,17 @@ expression: response.json()
"string",
"null"
]
},
"username": {
"description": "The crates.io username of the team or user.",
"example": "ghost",
"type": "string"
}
},
"required": [
"id",
"login",
"username",
"kind"
],
"type": "object"
Expand Down Expand Up @@ -853,7 +866,7 @@ expression: response.json()
"type": "integer"
},
"login": {
"description": "The user's login name.",
"description": "The user's GitHub login name.",
"example": "ghost",
"type": "string"
},
Expand All @@ -869,11 +882,17 @@ expression: response.json()
"description": "The user's GitHub profile URL.",
"example": "https://github.com/ghost",
"type": "string"
},
"username": {
"description": "The user's crates.io username.",
"example": "ghost",
"type": "string"
}
},
"required": [
"id",
"login",
"username",
"url"
],
"type": "object"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/publish/edition.rs
expression: response.json()
snapshot_kind: text
---
{
"version": {
Expand All @@ -13,7 +14,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
}
}
],
Expand Down Expand Up @@ -44,7 +46,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
},
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"repository": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/publish/links.rs
expression: response.json()
snapshot_kind: text
---
{
"version": {
Expand All @@ -13,7 +14,8 @@ expression: response.json()
"id": 1,
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
}
}
],
Expand Down Expand Up @@ -44,7 +46,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
},
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"repository": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/publish/manifest.rs
expression: response.json()
snapshot_kind: text
---
{
"version": {
Expand All @@ -13,7 +14,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
}
}
],
Expand Down Expand Up @@ -44,7 +46,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
},
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"repository": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/publish/manifest.rs
expression: response.json()
snapshot_kind: text
---
{
"version": {
Expand All @@ -13,7 +14,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
}
}
],
Expand Down Expand Up @@ -47,7 +49,8 @@ expression: response.json()
"id": "[id]",
"login": "foo",
"name": null,
"url": "https://github.com/foo"
"url": "https://github.com/foo",
"username": "foo"
},
"readme_path": "/api/v1/crates/foo/1.0.0/readme",
"repository": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/yanking.rs
expression: json
snapshot_kind: text
---
{
"version": {
Expand All @@ -26,6 +27,7 @@ expression: json
"published_by": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand All @@ -36,6 +38,7 @@ expression: json
"user": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand All @@ -47,6 +50,7 @@ expression: json
"user": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/tests/krate/yanking.rs
expression: json
snapshot_kind: text
---
{
"version": {
Expand All @@ -26,6 +27,7 @@ expression: json
"published_by": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand All @@ -36,6 +38,7 @@ expression: json
"user": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand All @@ -47,6 +50,7 @@ expression: json
"user": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand All @@ -58,6 +62,7 @@ expression: json
"user": {
"id": 1,
"login": "foo",
"username": "foo",
"name": null,
"avatar": null,
"url": "https://github.com/foo"
Expand Down
Loading