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:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user