Skip to content

Commit 4b7f55a

Browse files
refactor(accounts): move accounts related tables to accounts schema (#7626)
1 parent d892ee7 commit 4b7f55a

File tree

18 files changed

+130
-68
lines changed

18 files changed

+130
-68
lines changed

crates/common_types/src/payment_methods.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
//! Common types to be used in payment methods
22
33
use diesel::{
4-
backend::Backend, deserialize, deserialize::FromSql, sql_types::Jsonb, AsExpression, Queryable,
4+
backend::Backend,
5+
deserialize,
6+
deserialize::FromSql,
7+
sql_types::{Json, Jsonb},
8+
AsExpression, Queryable,
59
};
610
use serde::{Deserialize, Serialize};
711
use utoipa::ToSchema;
812

913
/// Details of all the payment methods enabled for the connector for the given merchant account
14+
15+
// sql_type for this can be json instead of jsonb. This is because validation at database is not required since it will always be written by the application.
16+
// This is a performance optimization to avoid json validation at database level.
17+
// jsonb enables faster querying on json columns, but it doesn't justify here since we are not querying on this column.
18+
// https://docs.rs/diesel/latest/diesel/sql_types/struct.Jsonb.html
1019
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, AsExpression)]
1120
#[serde(deny_unknown_fields)]
12-
#[diesel(sql_type = Jsonb)]
21+
#[diesel(sql_type = Json)]
1322
pub struct PaymentMethodsEnabled {
1423
/// Type of payment method.
1524
#[schema(value_type = PaymentMethod,example = "card")]

crates/diesel_models/src/business_profile.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct Profile {
6868
pub force_3ds_challenge: Option<bool>,
6969
pub is_debit_routing_enabled: bool,
7070
pub merchant_business_country: Option<common_enums::CountryAlpha2>,
71+
pub id: Option<common_utils::id_type::ProfileId>,
7172
}
7273

7374
#[cfg(feature = "v1")]
@@ -120,6 +121,7 @@ pub struct ProfileNew {
120121
pub force_3ds_challenge: Option<bool>,
121122
pub is_debit_routing_enabled: bool,
122123
pub merchant_business_country: Option<common_enums::CountryAlpha2>,
124+
pub id: Option<common_utils::id_type::ProfileId>,
123125
}
124126

125127
#[cfg(feature = "v1")]
@@ -289,6 +291,7 @@ impl ProfileUpdateInternal {
289291
is_clear_pan_retries_enabled: is_clear_pan_retries_enabled
290292
.unwrap_or(source.is_clear_pan_retries_enabled),
291293
force_3ds_challenge,
294+
id: source.id,
292295
is_debit_routing_enabled,
293296
merchant_business_country: merchant_business_country
294297
.or(source.merchant_business_country),
@@ -348,6 +351,7 @@ pub struct Profile {
348351
pub force_3ds_challenge: Option<bool>,
349352
pub is_debit_routing_enabled: bool,
350353
pub merchant_business_country: Option<common_enums::CountryAlpha2>,
354+
pub id: common_utils::id_type::ProfileId,
351355
pub routing_algorithm_id: Option<common_utils::id_type::RoutingId>,
352356
pub order_fulfillment_time: Option<i64>,
353357
pub order_fulfillment_time_origin: Option<common_enums::OrderFulfillmentTimeOrigin>,
@@ -356,7 +360,6 @@ pub struct Profile {
356360
pub default_fallback_routing: Option<pii::SecretSerdeValue>,
357361
pub three_ds_decision_manager_config: Option<common_types::payments::DecisionManagerRecord>,
358362
pub should_collect_cvv_during_payment: bool,
359-
pub id: common_utils::id_type::ProfileId,
360363
}
361364

362365
impl Profile {

crates/diesel_models/src/merchant_connector_account.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct MerchantConnectorAccount {
5454
pub additional_merchant_data: Option<Encryption>,
5555
pub connector_wallets_details: Option<Encryption>,
5656
pub version: common_enums::ApiVersion,
57+
pub id: Option<id_type::MerchantConnectorAccountId>,
5758
}
5859

5960
#[cfg(feature = "v1")]
@@ -98,8 +99,8 @@ pub struct MerchantConnectorAccount {
9899
pub additional_merchant_data: Option<Encryption>,
99100
pub connector_wallets_details: Option<Encryption>,
100101
pub version: common_enums::ApiVersion,
101-
pub feature_metadata: Option<MerchantConnectorAccountFeatureMetadata>,
102102
pub id: id_type::MerchantConnectorAccountId,
103+
pub feature_metadata: Option<MerchantConnectorAccountFeatureMetadata>,
103104
}
104105

105106
#[cfg(feature = "v2")]
@@ -140,6 +141,7 @@ pub struct MerchantConnectorAccountNew {
140141
pub additional_merchant_data: Option<Encryption>,
141142
pub connector_wallets_details: Option<Encryption>,
142143
pub version: common_enums::ApiVersion,
144+
pub id: Option<id_type::MerchantConnectorAccountId>,
143145
}
144146

145147
#[cfg(feature = "v2")]
@@ -167,8 +169,8 @@ pub struct MerchantConnectorAccountNew {
167169
pub status: storage_enums::ConnectorStatus,
168170
pub additional_merchant_data: Option<Encryption>,
169171
pub connector_wallets_details: Option<Encryption>,
170-
pub id: id_type::MerchantConnectorAccountId,
171172
pub version: common_enums::ApiVersion,
173+
pub id: id_type::MerchantConnectorAccountId,
172174
pub feature_metadata: Option<MerchantConnectorAccountFeatureMetadata>,
173175
}
174176

crates/diesel_models/src/schema.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ diesel::table! {
223223
force_3ds_challenge -> Nullable<Bool>,
224224
is_debit_routing_enabled -> Bool,
225225
merchant_business_country -> Nullable<CountryAlpha2>,
226+
#[max_length = 64]
227+
id -> Nullable<Varchar>,
226228
}
227229
}
228230

@@ -788,6 +790,8 @@ diesel::table! {
788790
additional_merchant_data -> Nullable<Bytea>,
789791
connector_wallets_details -> Nullable<Bytea>,
790792
version -> ApiVersion,
793+
#[max_length = 64]
794+
id -> Nullable<Varchar>,
791795
}
792796
}
793797

crates/diesel_models/src/schema_v2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ diesel::table! {
220220
is_debit_routing_enabled -> Bool,
221221
merchant_business_country -> Nullable<CountryAlpha2>,
222222
#[max_length = 64]
223+
id -> Varchar,
224+
#[max_length = 64]
223225
routing_algorithm_id -> Nullable<Varchar>,
224226
order_fulfillment_time -> Nullable<Int8>,
225227
order_fulfillment_time_origin -> Nullable<OrderFulfillmentTimeOrigin>,
@@ -230,8 +232,6 @@ diesel::table! {
230232
default_fallback_routing -> Nullable<Jsonb>,
231233
three_ds_decision_manager_config -> Nullable<Jsonb>,
232234
should_collect_cvv_during_payment -> Bool,
233-
#[max_length = 64]
234-
id -> Varchar,
235235
}
236236
}
237237

@@ -750,7 +750,7 @@ diesel::table! {
750750
connector_name -> Varchar,
751751
connector_account_details -> Bytea,
752752
disabled -> Nullable<Bool>,
753-
payment_methods_enabled -> Nullable<Array<Nullable<Jsonb>>>,
753+
payment_methods_enabled -> Nullable<Array<Nullable<Json>>>,
754754
connector_type -> ConnectorType,
755755
metadata -> Nullable<Jsonb>,
756756
#[max_length = 255]
@@ -767,9 +767,9 @@ diesel::table! {
767767
additional_merchant_data -> Nullable<Bytea>,
768768
connector_wallets_details -> Nullable<Bytea>,
769769
version -> ApiVersion,
770-
feature_metadata -> Nullable<Jsonb>,
771770
#[max_length = 64]
772771
id -> Varchar,
772+
feature_metadata -> Nullable<Jsonb>,
773773
}
774774
}
775775

crates/hyperswitch_domain_models/src/business_profile.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ impl super::behaviour::Conversion for Profile {
641641

642642
async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {
643643
Ok(diesel_models::business_profile::Profile {
644-
profile_id: self.profile_id,
644+
profile_id: self.profile_id.clone(),
645+
id: Some(self.profile_id),
645646
merchant_id: self.merchant_id,
646647
profile_name: self.profile_name,
647648
created_at: self.created_at,
@@ -793,7 +794,8 @@ impl super::behaviour::Conversion for Profile {
793794

794795
async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {
795796
Ok(diesel_models::business_profile::ProfileNew {
796-
profile_id: self.profile_id,
797+
profile_id: self.profile_id.clone(),
798+
id: Some(self.profile_id),
797799
merchant_id: self.merchant_id,
798800
profile_name: self.profile_name,
799801
created_at: self.created_at,

crates/hyperswitch_domain_models/src/merchant_connector_account.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ impl behaviour::Conversion for MerchantConnectorAccount {
356356
connector_account_details: self.connector_account_details.into(),
357357
test_mode: self.test_mode,
358358
disabled: self.disabled,
359-
merchant_connector_id: self.merchant_connector_id,
359+
merchant_connector_id: self.merchant_connector_id.clone(),
360+
id: Some(self.merchant_connector_id),
360361
payment_methods_enabled: self.payment_methods_enabled,
361362
connector_type: self.connector_type,
362363
metadata: self.metadata,
@@ -452,7 +453,8 @@ impl behaviour::Conversion for MerchantConnectorAccount {
452453
connector_account_details: Some(self.connector_account_details.into()),
453454
test_mode: self.test_mode,
454455
disabled: self.disabled,
455-
merchant_connector_id: self.merchant_connector_id,
456+
merchant_connector_id: self.merchant_connector_id.clone(),
457+
id: Some(self.merchant_connector_id),
456458
payment_methods_enabled: self.payment_methods_enabled,
457459
connector_type: Some(self.connector_type),
458460
metadata: self.metadata,

crates/router/src/db.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,14 @@ pub trait GlobalStorageInterface:
161161

162162
#[async_trait::async_trait]
163163
pub trait AccountsStorageInterface:
164-
Send + Sync + dyn_clone::DynClone + OrganizationInterface + 'static
164+
Send
165+
+ Sync
166+
+ dyn_clone::DynClone
167+
+ OrganizationInterface
168+
+ merchant_account::MerchantAccountInterface
169+
+ business_profile::ProfileInterface
170+
+ merchant_connector_account::MerchantConnectorAccountInterface
171+
+ 'static
165172
{
166173
}
167174

crates/router/src/db/business_profile.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl ProfileInterface for Store {
8282
merchant_key_store: &domain::MerchantKeyStore,
8383
business_profile: domain::Profile,
8484
) -> CustomResult<domain::Profile, errors::StorageError> {
85-
let conn = connection::pg_connection_write(self).await?;
85+
let conn = connection::pg_accounts_connection_write(self).await?;
8686
business_profile
8787
.construct_new()
8888
.await
@@ -106,7 +106,7 @@ impl ProfileInterface for Store {
106106
merchant_key_store: &domain::MerchantKeyStore,
107107
profile_id: &common_utils::id_type::ProfileId,
108108
) -> CustomResult<domain::Profile, errors::StorageError> {
109-
let conn = connection::pg_connection_read(self).await?;
109+
let conn = connection::pg_accounts_connection_read(self).await?;
110110
storage::Profile::find_by_profile_id(&conn, profile_id)
111111
.await
112112
.map_err(|error| report!(errors::StorageError::from(error)))?
@@ -126,7 +126,7 @@ impl ProfileInterface for Store {
126126
merchant_id: &common_utils::id_type::MerchantId,
127127
profile_id: &common_utils::id_type::ProfileId,
128128
) -> CustomResult<domain::Profile, errors::StorageError> {
129-
let conn = connection::pg_connection_read(self).await?;
129+
let conn = connection::pg_accounts_connection_read(self).await?;
130130
storage::Profile::find_by_merchant_id_profile_id(&conn, merchant_id, profile_id)
131131
.await
132132
.map_err(|error| report!(errors::StorageError::from(error)))?
@@ -147,7 +147,7 @@ impl ProfileInterface for Store {
147147
profile_name: &str,
148148
merchant_id: &common_utils::id_type::MerchantId,
149149
) -> CustomResult<domain::Profile, errors::StorageError> {
150-
let conn = connection::pg_connection_read(self).await?;
150+
let conn = connection::pg_accounts_connection_read(self).await?;
151151
storage::Profile::find_by_profile_name_merchant_id(&conn, profile_name, merchant_id)
152152
.await
153153
.map_err(|error| report!(errors::StorageError::from(error)))?
@@ -168,7 +168,7 @@ impl ProfileInterface for Store {
168168
current_state: domain::Profile,
169169
profile_update: domain::ProfileUpdate,
170170
) -> CustomResult<domain::Profile, errors::StorageError> {
171-
let conn = connection::pg_connection_write(self).await?;
171+
let conn = connection::pg_accounts_connection_write(self).await?;
172172
Conversion::convert(current_state)
173173
.await
174174
.change_context(errors::StorageError::EncryptionError)?
@@ -190,7 +190,7 @@ impl ProfileInterface for Store {
190190
profile_id: &common_utils::id_type::ProfileId,
191191
merchant_id: &common_utils::id_type::MerchantId,
192192
) -> CustomResult<bool, errors::StorageError> {
193-
let conn = connection::pg_connection_write(self).await?;
193+
let conn = connection::pg_accounts_connection_write(self).await?;
194194
storage::Profile::delete_by_profile_id_merchant_id(&conn, profile_id, merchant_id)
195195
.await
196196
.map_err(|error| report!(errors::StorageError::from(error)))
@@ -203,7 +203,7 @@ impl ProfileInterface for Store {
203203
merchant_key_store: &domain::MerchantKeyStore,
204204
merchant_id: &common_utils::id_type::MerchantId,
205205
) -> CustomResult<Vec<domain::Profile>, errors::StorageError> {
206-
let conn = connection::pg_connection_read(self).await?;
206+
let conn = connection::pg_accounts_connection_read(self).await?;
207207
storage::Profile::list_profile_by_merchant_id(&conn, merchant_id)
208208
.await
209209
.map_err(|error| report!(errors::StorageError::from(error)))

crates/router/src/db/merchant_account.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl MerchantAccountInterface for Store {
112112
merchant_account: domain::MerchantAccount,
113113
merchant_key_store: &domain::MerchantKeyStore,
114114
) -> CustomResult<domain::MerchantAccount, errors::StorageError> {
115-
let conn = connection::pg_connection_write(self).await?;
115+
let conn = connection::pg_accounts_connection_write(self).await?;
116116
merchant_account
117117
.construct_new()
118118
.await
@@ -137,7 +137,7 @@ impl MerchantAccountInterface for Store {
137137
merchant_key_store: &domain::MerchantKeyStore,
138138
) -> CustomResult<domain::MerchantAccount, errors::StorageError> {
139139
let fetch_func = || async {
140-
let conn = connection::pg_connection_read(self).await?;
140+
let conn = connection::pg_accounts_connection_read(self).await?;
141141
storage::MerchantAccount::find_by_merchant_id(&conn, merchant_id)
142142
.await
143143
.map_err(|error| report!(errors::StorageError::from(error)))
@@ -183,7 +183,7 @@ impl MerchantAccountInterface for Store {
183183
merchant_account: storage::MerchantAccountUpdate,
184184
merchant_key_store: &domain::MerchantKeyStore,
185185
) -> CustomResult<domain::MerchantAccount, errors::StorageError> {
186-
let conn = connection::pg_connection_write(self).await?;
186+
let conn = connection::pg_accounts_connection_write(self).await?;
187187

188188
let updated_merchant_account = Conversion::convert(this)
189189
.await
@@ -214,7 +214,7 @@ impl MerchantAccountInterface for Store {
214214
merchant_account: storage::MerchantAccountUpdate,
215215
merchant_key_store: &domain::MerchantKeyStore,
216216
) -> CustomResult<domain::MerchantAccount, errors::StorageError> {
217-
let conn = connection::pg_connection_write(self).await?;
217+
let conn = connection::pg_accounts_connection_write(self).await?;
218218
let updated_merchant_account = storage::MerchantAccount::update_with_specific_fields(
219219
&conn,
220220
merchant_id,
@@ -245,7 +245,7 @@ impl MerchantAccountInterface for Store {
245245
) -> CustomResult<(domain::MerchantAccount, domain::MerchantKeyStore), errors::StorageError>
246246
{
247247
let fetch_by_pub_key_func = || async {
248-
let conn = connection::pg_connection_read(self).await?;
248+
let conn = connection::pg_accounts_connection_read(self).await?;
249249

250250
storage::MerchantAccount::find_by_publishable_key(&conn, publishable_key)
251251
.await
@@ -294,7 +294,7 @@ impl MerchantAccountInterface for Store {
294294
organization_id: &common_utils::id_type::OrganizationId,
295295
) -> CustomResult<Vec<domain::MerchantAccount>, errors::StorageError> {
296296
use futures::future::try_join_all;
297-
let conn = connection::pg_connection_read(self).await?;
297+
let conn = connection::pg_accounts_connection_read(self).await?;
298298

299299
let encrypted_merchant_accounts =
300300
storage::MerchantAccount::list_by_organization_id(&conn, organization_id)
@@ -338,7 +338,7 @@ impl MerchantAccountInterface for Store {
338338
&self,
339339
merchant_id: &common_utils::id_type::MerchantId,
340340
) -> CustomResult<bool, errors::StorageError> {
341-
let conn = connection::pg_connection_write(self).await?;
341+
let conn = connection::pg_accounts_connection_write(self).await?;
342342

343343
let is_deleted_func = || async {
344344
storage::MerchantAccount::delete_by_merchant_id(&conn, merchant_id)
@@ -375,7 +375,7 @@ impl MerchantAccountInterface for Store {
375375
state: &KeyManagerState,
376376
merchant_ids: Vec<common_utils::id_type::MerchantId>,
377377
) -> CustomResult<Vec<domain::MerchantAccount>, errors::StorageError> {
378-
let conn = connection::pg_connection_read(self).await?;
378+
let conn = connection::pg_accounts_connection_read(self).await?;
379379

380380
let encrypted_merchant_accounts =
381381
storage::MerchantAccount::list_multiple_merchant_accounts(&conn, merchant_ids)
@@ -439,7 +439,7 @@ impl MerchantAccountInterface for Store {
439439
)>,
440440
errors::StorageError,
441441
> {
442-
let conn = connection::pg_connection_read(self).await?;
442+
let conn = connection::pg_accounts_connection_read(self).await?;
443443
let encrypted_merchant_accounts =
444444
storage::MerchantAccount::list_all_merchant_accounts(&conn, limit, offset)
445445
.await
@@ -460,7 +460,7 @@ impl MerchantAccountInterface for Store {
460460
&self,
461461
merchant_account: storage::MerchantAccountUpdate,
462462
) -> CustomResult<usize, errors::StorageError> {
463-
let conn = connection::pg_connection_read(self).await?;
463+
let conn = connection::pg_accounts_connection_read(self).await?;
464464

465465
let db_func = || async {
466466
storage::MerchantAccount::update_all_merchant_accounts(

0 commit comments

Comments
 (0)