perf: migrate all user/session/auth IDs from VARCHAR(36) to native UUID

- Schema: all ~15 VARCHAR(36) columns → UUID with DEFAULT gen_random_uuid()
- Domain entities: User, Session, DeviceCode, AppPassword, Share → id: Uuid
- DTOs: CurrentUser.id → Uuid (API boundary DTOs keep String for JSON)
- Auth middleware: parse JWT claims.sub (String) → Uuid at boundary
- All repository traits, port traits, service impls updated end-to-end
- Handlers: pass Uuid by value (Copy, 16 bytes) instead of String refs
- Settings chain: updated_by column → Uuid (was text, caused setup crash)
- Removed ~650 lines of String↔Uuid conversion boilerplate
- Eliminates per-request heap allocations for ID cloning
- 16-byte binary comparison vs 36-byte string comparison in all queries
- Native UUID indexing in PostgreSQL (btree on 16 bytes vs 36-char text)

85 files changed, 1090 insertions(+), 1739 deletions(-)
This commit is contained in:
Diocrafts
2026-03-07 14:59:32 +01:00
parent 9f08460027
commit 06ed0455ce
85 changed files with 1090 additions and 1739 deletions
+4 -4
View File
@@ -11,9 +11,9 @@ use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct AppPassword {
/// Unique identifier.
pub id: String,
pub id: Uuid,
/// Owner user ID.
pub user_id: String,
pub user_id: Uuid,
/// Human-readable label chosen by the user (e.g. "DAVx5 on Pixel 8").
pub label: String,
/// Argon2 hash of the generated password token.
@@ -41,7 +41,7 @@ impl AppPassword {
/// The caller is responsible for hashing the raw token and passing
/// the hash and prefix.
pub fn new(
user_id: String,
user_id: Uuid,
label: String,
password_hash: String,
prefix: String,
@@ -49,7 +49,7 @@ impl AppPassword {
expires_at: Option<DateTime<Utc>>,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
user_id,
label,
password_hash,
+10 -10
View File
@@ -48,13 +48,13 @@ impl std::fmt::Display for DeviceCodeStatus {
/// Domain entity for a Device Authorization flow.
#[derive(Debug, Clone)]
pub struct DeviceCode {
id: String,
id: Uuid,
device_code: String,
user_code: String,
client_name: String,
scopes: String,
status: DeviceCodeStatus,
user_id: Option<String>,
user_id: Option<Uuid>,
access_token: Option<String>,
refresh_token: Option<String>,
verification_uri: String,
@@ -89,7 +89,7 @@ impl DeviceCode {
) -> Self {
let now = Utc::now();
Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
device_code,
user_code,
client_name,
@@ -111,13 +111,13 @@ impl DeviceCode {
/// Reconstruct from database row.
#[allow(clippy::too_many_arguments)]
pub fn from_raw(
id: String,
id: Uuid,
device_code: String,
user_code: String,
client_name: String,
scopes: String,
status: DeviceCodeStatus,
user_id: Option<String>,
user_id: Option<Uuid>,
access_token: Option<String>,
refresh_token: Option<String>,
verification_uri: String,
@@ -150,8 +150,8 @@ impl DeviceCode {
// ── Getters ──────────────────────────────────────────────────
pub fn id(&self) -> &str {
&self.id
pub fn id(&self) -> Uuid {
self.id
}
pub fn device_code(&self) -> &str {
@@ -174,8 +174,8 @@ impl DeviceCode {
self.status
}
pub fn user_id(&self) -> Option<&str> {
self.user_id.as_deref()
pub fn user_id(&self) -> Option<Uuid> {
self.user_id
}
pub fn access_token(&self) -> Option<&str> {
@@ -243,7 +243,7 @@ impl DeviceCode {
}
/// Authorize this device code for a specific user, storing the tokens.
pub fn authorize(&mut self, user_id: String, access_token: String, refresh_token: String) {
pub fn authorize(&mut self, user_id: Uuid, access_token: String, refresh_token: String) {
self.status = DeviceCodeStatus::Authorized;
self.user_id = Some(user_id);
self.access_token = Some(access_token);
+10 -13
View File
@@ -3,8 +3,8 @@ use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct Session {
id: String,
user_id: String,
id: Uuid,
user_id: Uuid,
refresh_token: String,
expires_at: DateTime<Utc>,
ip_address: Option<String>,
@@ -15,22 +15,19 @@ pub struct Session {
impl Session {
pub fn new(
user_id: String,
user_id: Uuid,
refresh_token: String,
ip_address: Option<String>,
user_agent: Option<String>,
expires_in_days: i64,
) -> Self {
if user_id.is_empty() {
panic!("Session user_id cannot be empty");
}
if refresh_token.is_empty() {
panic!("Session refresh_token cannot be empty");
}
let now = Utc::now();
Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
user_id,
refresh_token,
expires_at: now + Duration::days(expires_in_days),
@@ -43,8 +40,8 @@ impl Session {
#[allow(clippy::too_many_arguments)]
pub fn from_raw(
id: String,
user_id: String,
id: Uuid,
user_id: Uuid,
refresh_token: String,
expires_at: DateTime<Utc>,
ip_address: Option<String>,
@@ -65,12 +62,12 @@ impl Session {
}
// Getters
pub fn id(&self) -> &str {
&self.id
pub fn id(&self) -> Uuid {
self.id
}
pub fn user_id(&self) -> &str {
&self.user_id
pub fn user_id(&self) -> Uuid {
self.user_id
}
pub fn refresh_token(&self) -> &str {
+21 -16
View File
@@ -6,7 +6,7 @@ pub use super::entity_errors::ShareError;
#[derive(Debug, Clone, PartialEq)]
pub struct Share {
id: String,
id: Uuid,
item_id: String,
item_name: Option<String>,
item_type: ShareItemType,
@@ -15,7 +15,7 @@ pub struct Share {
expires_at: Option<u64>,
permissions: SharePermissions,
created_at: u64,
created_by: String,
created_by: Uuid,
access_count: u64,
}
@@ -37,7 +37,7 @@ impl Share {
item_id: String,
item_name: Option<String>,
item_type: ShareItemType,
created_by: String,
created_by: Uuid,
permissions: Option<SharePermissions>,
password_hash: Option<String>,
expires_at: Option<u64>,
@@ -69,7 +69,7 @@ impl Share {
.as_secs();
Ok(Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
item_id,
item_name,
item_type,
@@ -89,7 +89,7 @@ impl Share {
#[allow(clippy::too_many_arguments)]
pub fn from_raw(
id: String,
id: Uuid,
item_id: String,
item_name: Option<String>,
item_type: ShareItemType,
@@ -98,7 +98,7 @@ impl Share {
expires_at: Option<u64>,
permissions: SharePermissions,
created_at: u64,
created_by: String,
created_by: Uuid,
access_count: u64,
) -> Self {
Self {
@@ -118,8 +118,8 @@ impl Share {
// ── Getters ──
pub fn id(&self) -> &str {
&self.id
pub fn id(&self) -> Uuid {
self.id
}
pub fn item_id(&self) -> &str {
@@ -150,8 +150,8 @@ impl Share {
self.created_at
}
pub fn created_by(&self) -> &str {
&self.created_by
pub fn created_by(&self) -> Uuid {
self.created_by
}
pub fn access_count(&self) -> u64 {
@@ -262,13 +262,18 @@ impl TryFrom<&str> for ShareItemType {
mod tests {
use super::*;
fn test_user_id() -> Uuid {
Uuid::new_v4()
}
#[test]
fn test_create_share() {
let uid = test_user_id();
let share = Share::new(
"test_file_id".to_string(),
None,
ShareItemType::File,
"user123".to_string(),
uid,
None,
None,
None,
@@ -277,7 +282,7 @@ mod tests {
assert_eq!(share.item_id(), "test_file_id");
assert_eq!(*share.item_type(), ShareItemType::File);
assert_eq!(share.created_by(), "user123");
assert_eq!(share.created_by(), uid);
assert!(share.permissions().read());
assert!(!share.permissions().write());
assert!(!share.permissions().reshare());
@@ -299,7 +304,7 @@ mod tests {
"test_file_id".to_string(),
None,
ShareItemType::File,
"user123".to_string(),
test_user_id(),
None,
None,
Some(future),
@@ -314,7 +319,7 @@ mod tests {
"test_file_id".to_string(),
None,
ShareItemType::File,
"user123".to_string(),
test_user_id(),
None,
None,
Some(past),
@@ -349,7 +354,7 @@ mod tests {
"test_file_id".to_string(),
None,
ShareItemType::File,
"user123".to_string(),
test_user_id(),
None,
Some("some_hash_value".to_string()),
None,
@@ -366,7 +371,7 @@ mod tests {
"test_file_id".to_string(),
None,
ShareItemType::File,
"user123".to_string(),
test_user_id(),
None,
None, // No password
None,
+7 -7
View File
@@ -22,7 +22,7 @@ impl std::fmt::Display for UserRole {
#[derive(Debug, Clone)]
pub struct User {
id: String,
id: Uuid,
username: String,
email: String,
password_hash: String,
@@ -70,7 +70,7 @@ impl User {
let now = Utc::now();
Ok(Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
username,
email,
password_hash,
@@ -99,7 +99,7 @@ impl User {
Self::validate_email(&email)?;
let now = Utc::now();
Ok(Self {
id: Uuid::new_v4().to_string(),
id: Uuid::new_v4(),
username,
email,
password_hash: "__OIDC_NO_PASSWORD__".to_string(),
@@ -117,7 +117,7 @@ impl User {
#[allow(clippy::too_many_arguments)]
pub fn from_data(
id: String,
id: Uuid,
username: String,
email: String,
password_hash: String,
@@ -148,7 +148,7 @@ impl User {
#[allow(clippy::too_many_arguments)]
pub fn from_data_full(
id: String,
id: Uuid,
username: String,
email: String,
password_hash: String,
@@ -180,8 +180,8 @@ impl User {
}
// Getters
pub fn id(&self) -> &str {
&self.id
pub fn id(&self) -> Uuid {
self.id
}
pub fn username(&self) -> &str {
@@ -22,23 +22,23 @@ pub trait AddressBookRepository: Send + Sync + 'static {
) -> AddressBookRepositoryResult<Option<AddressBook>>;
async fn get_address_books_by_owner(
&self,
owner_id: &str,
owner_id: Uuid,
) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn get_shared_address_books(
&self,
user_id: &str,
user_id: Uuid,
) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn get_public_address_books(&self) -> AddressBookRepositoryResult<Vec<AddressBook>>;
async fn share_address_book(
&self,
address_book_id: &Uuid,
user_id: &str,
user_id: Uuid,
can_write: bool,
) -> AddressBookRepositoryResult<()>;
async fn unshare_address_book(
&self,
address_book_id: &Uuid,
user_id: &str,
user_id: Uuid,
) -> AddressBookRepositoryResult<()>;
async fn get_address_book_shares(
&self,
@@ -21,20 +21,20 @@ pub trait CalendarRepository: Send + Sync + 'static {
/// Lists all calendars for a specific user
async fn list_calendars_by_owner(
&self,
owner_id: &str,
owner_id: Uuid,
) -> CalendarRepositoryResult<Vec<Calendar>>;
/// Finds a calendar by name and owner
async fn find_calendar_by_name_and_owner(
&self,
name: &str,
owner_id: &str,
owner_id: Uuid,
) -> CalendarRepositoryResult<Calendar>;
/// Lists calendars shared with a specific user
async fn list_calendars_shared_with_user(
&self,
user_id: &str,
user_id: Uuid,
) -> CalendarRepositoryResult<Vec<Calendar>>;
/// List public calendars
@@ -48,7 +48,7 @@ pub trait CalendarRepository: Send + Sync + 'static {
async fn user_has_calendar_access(
&self,
calendar_id: &Uuid,
user_id: &str,
user_id: Uuid,
) -> CalendarRepositoryResult<bool>;
/// Gets a custom property for a calendar
@@ -83,7 +83,7 @@ pub trait CalendarRepository: Send + Sync + 'static {
async fn share_calendar(
&self,
calendar_id: &Uuid,
user_id: &str,
user_id: Uuid,
access_level: &str,
) -> CalendarRepositoryResult<()>;
@@ -91,7 +91,7 @@ pub trait CalendarRepository: Send + Sync + 'static {
async fn remove_calendar_sharing(
&self,
calendar_id: &Uuid,
user_id: &str,
user_id: Uuid,
) -> CalendarRepositoryResult<()>;
/// Get calendar sharing information (who has access to this calendar)
@@ -1,5 +1,6 @@
use crate::common::errors::DomainError;
use crate::domain::entities::session::Session;
use uuid::Uuid;
#[derive(Debug, thiserror::Error)]
pub enum SessionRepositoryError {
@@ -33,7 +34,7 @@ pub trait SessionRepository: Send + Sync + 'static {
async fn create_session(&self, session: Session) -> SessionRepositoryResult<Session>;
/// Gets a session by ID
async fn get_session_by_id(&self, id: &str) -> SessionRepositoryResult<Session>;
async fn get_session_by_id(&self, id: Uuid) -> SessionRepositoryResult<Session>;
/// Gets a session by refresh token
async fn get_session_by_refresh_token(
@@ -42,14 +43,14 @@ pub trait SessionRepository: Send + Sync + 'static {
) -> SessionRepositoryResult<Session>;
/// Gets all sessions for a user
async fn get_sessions_by_user_id(&self, user_id: &str)
async fn get_sessions_by_user_id(&self, user_id: Uuid)
-> SessionRepositoryResult<Vec<Session>>;
/// Revokes a specific session
async fn revoke_session(&self, session_id: &str) -> SessionRepositoryResult<()>;
async fn revoke_session(&self, session_id: Uuid) -> SessionRepositoryResult<()>;
/// Revokes all sessions for a user
async fn revoke_all_user_sessions(&self, user_id: &str) -> SessionRepositoryResult<u64>;
async fn revoke_all_user_sessions(&self, user_id: Uuid) -> SessionRepositoryResult<u64>;
/// Deletes expired sessions
async fn delete_expired_sessions(&self) -> SessionRepositoryResult<u64>;
@@ -1,5 +1,6 @@
use crate::common::errors::DomainError;
use std::collections::HashMap;
use uuid::Uuid;
/// Repository for platform settings stored in the database.
/// Settings are key-value pairs organized by category (e.g., "oidc", "general").
@@ -18,7 +19,7 @@ pub trait SettingsRepository: Send + Sync + 'static {
value: &str,
category: &str,
is_secret: bool,
updated_by: Option<&str>,
updated_by: Option<Uuid>,
) -> Result<(), DomainError>;
/// Delete a setting by key
@@ -34,7 +35,7 @@ pub trait SettingsRepository: Send + Sync + 'static {
/// The default implementation falls back to the non-atomic
/// get-then-set pattern for repositories that don't support a native
/// atomic upsert.
async fn try_claim_initialization(&self, admin_user_id: &str) -> Result<bool, DomainError> {
async fn try_claim_initialization(&self, admin_user_id: Uuid) -> Result<bool, DomainError> {
// Default: non-atomic fallback (overridden by PG implementation)
match self.get("system_initialized").await? {
Some(v) if v == "true" => Ok(false),
+4 -3
View File
@@ -1,4 +1,5 @@
use thiserror::Error;
use uuid::Uuid;
use crate::domain::{
entities::share::{Share, ShareItemType},
@@ -24,7 +25,7 @@ pub trait ShareRepository: Send + Sync + 'static {
async fn save(&self, share: &Share) -> Result<Share, ShareRepositoryError>;
/// Find a share by its ID
async fn find_by_id(&self, id: &str) -> Result<Share, ShareRepositoryError>;
async fn find_by_id(&self, id: Uuid) -> Result<Share, ShareRepositoryError>;
/// Find a share by its token
async fn find_by_token(&self, token: &str) -> Result<Share, ShareRepositoryError>;
@@ -37,10 +38,10 @@ pub trait ShareRepository: Send + Sync + 'static {
) -> Result<Vec<Share>, ShareRepositoryError>;
/// Delete a share by its ID
async fn delete(&self, id: &str) -> Result<(), ShareRepositoryError>;
async fn delete(&self, id: Uuid) -> Result<(), ShareRepositoryError>;
/// Find all shares created by a specific user
async fn find_by_user(&self, user_id: &str) -> Result<Vec<Share>, ShareRepositoryError>;
async fn find_by_user(&self, user_id: Uuid) -> Result<Vec<Share>, ShareRepositoryError>;
/// Find all shares (admin operation)
async fn find_all(&self) -> Result<Vec<Share>, ShareRepositoryError>;
+9 -8
View File
@@ -1,5 +1,6 @@
use crate::common::errors::DomainError;
use crate::domain::entities::user::{User, UserRole};
use uuid::Uuid;
#[derive(Debug, thiserror::Error)]
pub enum UserRepositoryError {
@@ -45,7 +46,7 @@ pub trait UserRepository: Send + Sync + 'static {
async fn create_user(&self, user: User) -> UserRepositoryResult<User>;
/// Gets a user by ID
async fn get_user_by_id(&self, id: &str) -> UserRepositoryResult<User>;
async fn get_user_by_id(&self, id: Uuid) -> UserRepositoryResult<User>;
/// Gets a user by username
async fn get_user_by_username(&self, username: &str) -> UserRepositoryResult<User>;
@@ -59,12 +60,12 @@ pub trait UserRepository: Send + Sync + 'static {
/// Updates only a user's storage usage
async fn update_storage_usage(
&self,
user_id: &str,
user_id: Uuid,
usage_bytes: i64,
) -> UserRepositoryResult<()>;
/// Updates the last login date
async fn update_last_login(&self, user_id: &str) -> UserRepositoryResult<()>;
async fn update_last_login(&self, user_id: Uuid) -> UserRepositoryResult<()>;
/// Lists users with pagination
async fn list_users(&self, limit: i64, offset: i64) -> UserRepositoryResult<Vec<User>>;
@@ -73,21 +74,21 @@ pub trait UserRepository: Send + Sync + 'static {
async fn search_users(&self, query: &str, limit: i64) -> UserRepositoryResult<Vec<User>>;
/// Activates or deactivates a user
async fn set_user_active_status(&self, user_id: &str, active: bool)
async fn set_user_active_status(&self, user_id: Uuid, active: bool)
-> UserRepositoryResult<()>;
/// Changes a user's password
async fn change_password(&self, user_id: &str, password_hash: &str)
async fn change_password(&self, user_id: Uuid, password_hash: &str)
-> UserRepositoryResult<()>;
/// Changes a user's role
async fn change_role(&self, user_id: &str, role: UserRole) -> UserRepositoryResult<()>;
async fn change_role(&self, user_id: Uuid, role: UserRole) -> UserRepositoryResult<()>;
/// Lists users by role (admin or user)
async fn list_users_by_role(&self, role: &str) -> UserRepositoryResult<Vec<User>>;
/// Deletes a user
async fn delete_user(&self, user_id: &str) -> UserRepositoryResult<()>;
async fn delete_user(&self, user_id: Uuid) -> UserRepositoryResult<()>;
/// Finds a user by OIDC provider + subject pair
async fn get_user_by_oidc_subject(
@@ -99,7 +100,7 @@ pub trait UserRepository: Send + Sync + 'static {
/// Updates a user's storage quota
async fn update_storage_quota(
&self,
user_id: &str,
user_id: Uuid,
quota_bytes: i64,
) -> UserRepositoryResult<()>;