Skip to content
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

feat: migration for user_id #1663

Merged
merged 6 commits into from Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions auth/Cargo.toml
Expand Up @@ -31,6 +31,7 @@ tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-opentelemetry = { workspace = true }
tracing-subscriber = { workspace = true }
ulid = { workspace = true }

[dev-dependencies]
axum-extra = { version = "0.7.1" }
Expand Down
4 changes: 4 additions & 0 deletions auth/migrations/0003_user_ids.sql
@@ -0,0 +1,4 @@
-- Add new user_id column for users
ALTER TABLE users
ADD COLUMN user_id TEXT UNIQUE;
-- All NULL values are filled in at runtime
17 changes: 17 additions & 0 deletions auth/src/lib.rs
Expand Up @@ -10,6 +10,7 @@ use args::StartArgs;
use shuttle_common::{claims::AccountTier, ApiKey};
use sqlx::{migrate::Migrator, query, PgPool};
use tracing::info;
use user::User;

use crate::api::serve;
pub use api::ApiBuilder;
Expand Down Expand Up @@ -67,5 +68,21 @@ pub async fn pgpool_init(db_uri: &str) -> io::Result<PgPool> {
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

// Post-migration logic for 0003.
// This is done here to skip the need for postgres extensions.
let names: Vec<(String,)> =
sqlx::query_as("SELECT account_name FROM users WHERE user_id IS NULL")
.fetch_all(&pool)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
for (name,) in names {
sqlx::query("UPDATE users SET user_id = $1 WHERE account_name = $2")
.bind(User::new_user_id())
.bind(name)
.execute(&pool)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

Ok(pool)
}
7 changes: 7 additions & 0 deletions auth/src/user.rs
Expand Up @@ -246,6 +246,7 @@ impl UserManagement for UserManager {
#[derive(Clone, Debug)]
pub struct User {
pub name: AccountName,
pub id: String,
pub key: Secret<ApiKey>,
pub account_tier: AccountTier,
pub subscriptions: Vec<Subscription>,
Expand All @@ -272,6 +273,10 @@ impl User {
.map(|sub| &sub.id)
}

pub fn new_user_id() -> String {
format!("user_{}", ulid::Ulid::new())
}

pub fn new(
name: AccountName,
key: ApiKey,
Expand All @@ -280,6 +285,7 @@ impl User {
) -> Self {
Self {
name,
id: Self::new_user_id(),
key: Secret::new(key),
account_tier,
subscriptions,
Expand Down Expand Up @@ -344,6 +350,7 @@ impl FromRow<'_, PgRow> for User {
fn from_row(row: &PgRow) -> Result<Self, sqlx::Error> {
Ok(User {
name: row.try_get("account_name").unwrap(),
id: row.try_get("user_id").unwrap(),
key: Secret::new(row.try_get("key").unwrap()),
account_tier: AccountTier::from_str(row.try_get("account_tier").unwrap()).map_err(
|err| sqlx::Error::ColumnDecode {
Expand Down
3 changes: 3 additions & 0 deletions gateway/migrations/0007_user_ids.sql
@@ -0,0 +1,3 @@
-- Add new user_id column for projects
ALTER TABLE projects
ADD COLUMN user_id TEXT;