-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(users): store and retrieve lineage_context from DB instead of Redis #7940
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
Open
tsdk02
wants to merge
15
commits into
main
Choose a base branch
from
lineage-context-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd1428c
feat(users): store and retrieve lineage_context from DB instead of Redis
tsdk02 cb18d6b
check v2 user role first before v1 user role
tsdk02 b0bd6c1
add lineage_context column in v2 schema
tsdk02 078716d
add comment
tsdk02 6884b69
resolved comments and refactored code
tsdk02 87ef460
chore: run formatter
hyperswitch-bot[bot] e84b153
fixed clippy errors
tsdk02 07a5d92
chore: run formatter
hyperswitch-bot[bot] a716d29
fixed clippy errors
tsdk02 187002b
removed comment
tsdk02 deb41a5
Merge branch 'main' into lineage-context-db
tsdk02 c3ed3a8
update last_modified_at for MockDb
tsdk02 e80d127
move lineage_context DB update to background using tokio::spawn
tsdk02 670c2e9
added logging message for successfully updating lineage context
tsdk02 4ca1ce6
deleted file
tsdk02 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,9 @@ | ||
/// User related types | ||
pub mod core; | ||
|
||
/// Theme related types | ||
pub mod theme; | ||
|
||
pub use core::*; | ||
|
||
pub use theme::*; |
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,28 @@ | ||
use diesel::{deserialize::FromSqlRow, expression::AsExpression}; | ||
|
||
use crate::id_type; | ||
|
||
/// Struct for lineageContext | ||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, AsExpression, FromSqlRow)] | ||
#[diesel(sql_type = diesel::sql_types::Jsonb)] | ||
pub struct LineageContext { | ||
/// user_id: String | ||
pub user_id: String, | ||
|
||
/// merchant_id: MerchantId | ||
pub merchant_id: id_type::MerchantId, | ||
|
||
/// role_id: String | ||
pub role_id: String, | ||
|
||
/// org_id: OrganizationId | ||
pub org_id: id_type::OrganizationId, | ||
|
||
/// profile_id: ProfileId | ||
pub profile_id: id_type::ProfileId, | ||
|
||
/// tenant_id: TenantId | ||
pub tenant_id: id_type::TenantId, | ||
} | ||
|
||
crate::impl_to_sql_from_sql_json!(LineageContext); |
File renamed without changes.
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
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
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 |
---|---|---|
|
@@ -8,7 +8,10 @@ use api_models::{ | |
user::{self as user_api, InviteMultipleUserResponse, NameIdUnit}, | ||
}; | ||
use common_enums::{EntityType, UserAuthType}; | ||
use common_utils::{type_name, types::keymanager::Identifier}; | ||
use common_utils::{ | ||
type_name, | ||
types::{keymanager::Identifier, user::LineageContext}, | ||
}; | ||
#[cfg(feature = "email")] | ||
use diesel_models::user_role::UserRoleUpdate; | ||
use diesel_models::{ | ||
|
@@ -3169,7 +3172,7 @@ pub async fn switch_org_for_user( | |
} | ||
}; | ||
|
||
let lineage_context = domain::LineageContext { | ||
let lineage_context = LineageContext { | ||
user_id: user_from_token.user_id.clone(), | ||
merchant_id: merchant_id.clone(), | ||
role_id: role_id.clone(), | ||
|
@@ -3182,9 +3185,29 @@ pub async fn switch_org_for_user( | |
.clone(), | ||
}; | ||
|
||
lineage_context | ||
.try_set_lineage_context_in_cache(&state, user_from_token.user_id.as_str()) | ||
.await; | ||
tokio::spawn({ | ||
let state = state.clone(); | ||
let lineage_context = lineage_context.clone(); | ||
let user_id = user_from_token.user_id.clone(); | ||
async move { | ||
if let Err(e) = state | ||
.global_store | ||
.update_user_by_user_id( | ||
&user_id, | ||
diesel_models::user::UserUpdate::LineageContextUpdate { lineage_context }, | ||
) | ||
.await | ||
{ | ||
logger::error!( | ||
"Failed to update lineage context for user {}: {:?}", | ||
user_id, | ||
e | ||
); | ||
} else { | ||
logger::debug!("Successfully updated lineage context for user {}", user_id); | ||
} | ||
} | ||
}); | ||
|
||
let token = utils::user::generate_jwt_auth_token_with_attributes( | ||
&state, | ||
|
@@ -3381,7 +3404,7 @@ pub async fn switch_merchant_for_user_in_org( | |
} | ||
}; | ||
|
||
let lineage_context = domain::LineageContext { | ||
let lineage_context = LineageContext { | ||
user_id: user_from_token.user_id.clone(), | ||
merchant_id: merchant_id.clone(), | ||
role_id: role_id.clone(), | ||
|
@@ -3394,9 +3417,27 @@ pub async fn switch_merchant_for_user_in_org( | |
.clone(), | ||
}; | ||
|
||
lineage_context | ||
.try_set_lineage_context_in_cache(&state, user_from_token.user_id.as_str()) | ||
.await; | ||
tokio::spawn({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evaluate whether a consistent retry or fallback strategy is needed across all lineage_context update tasks (e.g. in switch_merchant and switch_profile) to ensure reliable DB updates. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
let state = state.clone(); | ||
let lineage_context = lineage_context.clone(); | ||
let user_id = user_from_token.user_id.clone(); | ||
async move { | ||
let _ = state | ||
.global_store | ||
.update_user_by_user_id( | ||
&user_id, | ||
diesel_models::user::UserUpdate::LineageContextUpdate { lineage_context }, | ||
) | ||
.await | ||
.map_err(|e| { | ||
logger::error!( | ||
"Failed to update lineage context for user {}: {:?}", | ||
user_id, | ||
e | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
let token = utils::user::generate_jwt_auth_token_with_attributes( | ||
&state, | ||
|
@@ -3514,7 +3555,7 @@ pub async fn switch_profile_for_user_in_org_and_merchant( | |
} | ||
}; | ||
|
||
let lineage_context = domain::LineageContext { | ||
let lineage_context = LineageContext { | ||
user_id: user_from_token.user_id.clone(), | ||
merchant_id: user_from_token.merchant_id.clone(), | ||
role_id: role_id.clone(), | ||
|
@@ -3527,9 +3568,27 @@ pub async fn switch_profile_for_user_in_org_and_merchant( | |
.clone(), | ||
}; | ||
|
||
lineage_context | ||
.try_set_lineage_context_in_cache(&state, user_from_token.user_id.as_str()) | ||
.await; | ||
tokio::spawn({ | ||
let state = state.clone(); | ||
let lineage_context = lineage_context.clone(); | ||
let user_id = user_from_token.user_id.clone(); | ||
async move { | ||
let _ = state | ||
.global_store | ||
.update_user_by_user_id( | ||
&user_id, | ||
diesel_models::user::UserUpdate::LineageContextUpdate { lineage_context }, | ||
) | ||
.await | ||
.map_err(|e| { | ||
logger::error!( | ||
"Failed to update lineage context for user {}: {:?}", | ||
user_id, | ||
e | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
let token = utils::user::generate_jwt_auth_token_with_attributes( | ||
&state, | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
Consider adding a retry mechanism for the asynchronous update of lineage_context in the background task to improve resilience against transient DB failures.
Copilot uses AI. Check for mistakes.