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

refactor(v1v2): refactor database queries for v1 and v2 #7244

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

hrithikesh026
Copy link
Contributor

@hrithikesh026 hrithikesh026 commented Feb 11, 2025

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

Other changes:

  1. Introduced product type column in merchant_account
  2. restructure payment_methods related v2 DB queries
  3. drop auto increment id column from all possible v1 tables.
  4. introduce varchar id column in merchant_account in v1.
  5. justfile changes to run all v1 migrations first and then v2 migrations. (prefixed v2 migrations with 9 before copying to a common directory so that all v2 migration will run only after all v1 migrations are run).

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

How did you test it?

Tested connector sanity.
No test cases.
image

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@hrithikesh026 hrithikesh026 added S-waiting-on-review Status: This PR has been implemented and needs to be reviewed M-database-changes Metadata: This PR involves database schema changes labels Feb 11, 2025
@hrithikesh026 hrithikesh026 self-assigned this Feb 11, 2025
@hrithikesh026 hrithikesh026 requested review from a team as code owners February 11, 2025 11:53
Copy link

semanticdiff-com bot commented Feb 11, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  crates/diesel_models/src/payment_attempt.rs  78% smaller
  crates/diesel_models/src/payment_method.rs  75% smaller
  crates/diesel_models/src/business_profile.rs  67% smaller
  crates/diesel_models/src/payment_intent.rs  60% smaller
  crates/diesel_models/src/schema.rs  33% smaller
  crates/router/src/db/configs.rs  24% smaller
  crates/router/src/db/locker_mock_up.rs  15% smaller
  crates/diesel_models/src/schema_v2.rs  4% smaller
  crates/diesel_models/src/merchant_account.rs  3% smaller
  crates/diesel_models/src/locker_mock_up.rs  1% smaller
  crates/diesel_models/src/blocklist_lookup.rs  1% smaller
  crates/diesel_models/src/configs.rs  1% smaller
  crates/diesel_models/src/blocklist_fingerprint.rs  1% smaller
  crates/common_enums/src/enums.rs  0% smaller
  crates/common_enums/src/enums/accounts.rs  0% smaller
  crates/diesel_models/drop_id.patch Unsupported file format
  crates/diesel_models/remove_id.patch Unsupported file format
  crates/diesel_models/src/merchant_connector_account.rs  0% smaller
  crates/hyperswitch_domain_models/src/merchant_account.rs  0% smaller
  crates/router/src/core/admin.rs  0% smaller
  diesel.toml Unsupported file format
  justfile Unsupported file format
  migrations/2025-02-06-111828_drop_int_id_column_accross_database/down.sql Unsupported file format
  migrations/2025-02-06-111828_drop_int_id_column_accross_database/up.sql Unsupported file format
  migrations/2025-02-10-101701_recreate_varchar_id_column_for_merchant_account/down.sql Unsupported file format
  migrations/2025-02-10-101701_recreate_varchar_id_column_for_merchant_account/up.sql Unsupported file format
  migrations/2025-02-11-062329_add-product-type-column-merchant-account/down.sql Unsupported file format
  migrations/2025-02-11-062329_add-product-type-column-merchant-account/up.sql Unsupported file format
  v2_migrations/2024-08-23-112510_payment_methods_v2_db_changes/down.sql Unsupported file format
  v2_migrations/2024-08-23-112510_payment_methods_v2_db_changes/up.sql Unsupported file format
  v2_migrations/2024-08-28-081721_add_v2_columns/down.sql Unsupported file format
  v2_migrations/2024-08-28-081721_add_v2_columns/up.sql Unsupported file format
  v2_migrations/2024-08-28-081722_drop_not_null_constraints_on_v1_columns/down.sql Unsupported file format
  v2_migrations/2024-08-28-081722_drop_not_null_constraints_on_v1_columns/up.sql Unsupported file format
  v2_migrations/2024-08-28-081747_recreate_ids_for_v2/down.sql Unsupported file format
  v2_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sql Unsupported file format
  v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/down.sql Unsupported file format
  v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/up.sql Unsupported file format
  v2_migrations/2024-08-28-081839_add_not_null_constraints_to_v2_columns/down.sql Unsupported file format
  v2_migrations/2024-08-28-081839_add_not_null_constraints_to_v2_columns/up.sql Unsupported file format
  v2_migrations/2025-01-13-081847_drop_v1_columns/down.sql Unsupported file format
  v2_migrations/2025-01-13-081847_drop_v1_columns/up.sql Unsupported file format
  v2_migrations/2025-01-13-081848_alter_column_types_for_v2/down.sql Unsupported file format
  v2_migrations/2025-01-13-081848_alter_column_types_for_v2/up.sql Unsupported file format
  v2_migrations/2025-01-13-181842_add_three_ds_decision_manager_config_in_profile/down.sql Unsupported file format
  v2_migrations/2025-01-13-181842_add_three_ds_decision_manager_config_in_profile/up.sql Unsupported file format
  v2_migrations/2025-01-29-103844_add_feature_metadata_in_merchant_connector_account/down.sql Unsupported file format
  v2_migrations/2025-01-29-103844_add_feature_metadata_in_merchant_connector_account/up.sql Unsupported file format

-- It must be ensured that the deployed version of the application includes the `id` column in any of its queries.
-- Re-create the id column as this was used as the primary key with a different type
------------------------ Merchant Account -----------------------
ALTER TABLE merchant_account ADD COLUMN id SERIAL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make all these SERIAL to VARCHAR itself?

@hrithikesh026 hrithikesh026 linked an issue Feb 13, 2025 that may be closed by this pull request
2 tasks
jarnura
jarnura previously approved these changes Feb 14, 2025
pub struct BlocklistFingerprint {
#[serde(skip_serializing)]
pub id: i32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot remove id from here right? This will not be backwards compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this will not be backwards compatible. Hence we need to go for a deployment with downtime. Instead of staggering.

@@ -49,7 +47,6 @@ impl From<ConfigUpdate> for ConfigUpdateInternal {
impl From<ConfigNew> for Config {
fn from(config_new: ConfigNew) -> Self {
Self {
id: 0i32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot remove this as well right? this will cause downtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. We need to go with a downtime for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add this in the patch for now, anyways we will need to have some downtime when deploying for v2, let's please add this in patch for now

@@ -52,6 +52,8 @@ pub struct MerchantAccount {
pub pm_collect_link_config: Option<serde_json::Value>,
pub version: common_enums::ApiVersion,
pub is_platform_account: bool,
pub id: Option<common_utils::id_type::MerchantId>,
pub product_type: Option<common_enums::MerchantProductType>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can a merchant account be associated with only one product type? what if the merchant wants to use more than one product for the same merchant account?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not allowed. Once a merchant is assigned a product_type, it cannot be changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is, can a merchant be associated with more than one product type at moment, both orchestrator and recovery service. Should this be a vector / hashset instead of just one element?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. One merchant can only belong to one product_type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the merchant wants to explore another product, they would need to create a new merchant_account.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird restriction to place though. I feel it's a valid use case to be able to use multiple products with the same merchant account.

Another question I have is about the API keys: is the merchant now expected to use different API keys in the same deployment of theirs, that uses our orchestration and vaulting products (or any two or more products for the matter)? Won't this become too much of a maintenance hassle for them?

@@ -5,7 +5,6 @@ diesel::table! {
use crate::enums::diesel_exports::*;

address (address_id) {
id -> Nullable<Int4>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address table will not be present in v2, we can drop this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take this up in a separate pr since this is outside the scope of this pr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sure

@@ -764,6 +787,7 @@ impl super::behaviour::Conversion for MerchantAccount {
async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {
let now = date_time::now();
Ok(diesel_models::merchant_account::MerchantAccountNew {
id: None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this left None on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, we will only allow v1 application to retrive v2 merchants, Not the other way around

Narayanbhat166
Narayanbhat166 previously approved these changes Feb 21, 2025
Narayanbhat166
Narayanbhat166 previously approved these changes Feb 21, 2025
Comment on lines 112 to 127
ALTER TABLE payment_methods
ADD COLUMN IF NOT EXISTS payment_method_id VARCHAR(64),
ADD COLUMN IF NOT EXISTS accepted_currency "Currency" [ ],
ADD COLUMN IF NOT EXISTS scheme VARCHAR(32),
ADD COLUMN IF NOT EXISTS token VARCHAR(128),
ADD COLUMN IF NOT EXISTS cardholder_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS issuer_name VARCHAR(64),
ADD COLUMN IF NOT EXISTS issuer_country VARCHAR(64),
ADD COLUMN IF NOT EXISTS is_stored BOOLEAN,
ADD COLUMN IF NOT EXISTS direct_debit_token VARCHAR(128),
ADD COLUMN IF NOT EXISTS swift_code VARCHAR(32),
ADD COLUMN IF NOT EXISTS payment_method_issuer VARCHAR(128),
ADD COLUMN IF NOT EXISTS metadata JSON,
ADD COLUMN IF NOT EXISTS payment_method VARCHAR,
ADD COLUMN IF NOT EXISTS payment_method_type VARCHAR(64),
ADD COLUMN IF NOT EXISTS payment_method_issuer_code "PaymentMethodIssuerCode";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payer_country is not added back here while its being removed in up.sql?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

Narayanbhat166
Narayanbhat166 previously approved these changes Feb 24, 2025
Chethan-rao
Chethan-rao previously approved these changes Feb 24, 2025
@hrithikesh026 hrithikesh026 requested review from a team and removed request for a team February 24, 2025 10:51
jarnura
jarnura previously approved these changes Feb 24, 2025
Copy link
Member

@SanchithHegde SanchithHegde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few questions / nitpicks from my side. The nitpicks can be addressed separately, not a problem.

@@ -0,0 +1,28 @@
use serde;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Unnecessary import, can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am deriving serde::Deserialize and Serialize for enum ProductType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm aware, you can directly use it in the derive() line, you don't need the explicit use serde; import.

Comment on lines +3 to +6
#[derive(
Default,
Clone,
Debug,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This enum can also derive Copy.

@@ -52,6 +52,8 @@ pub struct MerchantAccount {
pub pm_collect_link_config: Option<serde_json::Value>,
pub version: common_enums::ApiVersion,
pub is_platform_account: bool,
pub id: Option<common_utils::id_type::MerchantId>,
pub product_type: Option<common_enums::MerchantProductType>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird restriction to place though. I feel it's a valid use case to be able to use multiple products with the same merchant account.

Another question I have is about the API keys: is the merchant now expected to use different API keys in the same deployment of theirs, that uses our orchestration and vaulting products (or any two or more products for the matter)? Won't this become too much of a maintenance hassle for them?

@@ -317,6 +324,7 @@ impl From<MerchantAccountUpdate> for MerchantAccountUpdateInternal {
is_recon_enabled: None,
recon_status: None,
is_platform_account: None,
product_type: None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do we want to allow updating the product type once the merchant account has been created?
  2. If the answer for the above question is yes, then I don't see the MerchantAccountUpdate enum modified? So as it stands, the field would never be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes product_type will be populated only once after merchant account is created. Once it is populated, it cannot be modified.
  2. That will be taken up later.

mkdir -p {{resultant_dir}}
cp -r {{v1_migration_dir}}/* {{resultant_dir}}/

# Prefix v2 migrations with 9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being done so that v2 migrations always show up last in the combined list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

jarnura
jarnura previously approved these changes Feb 25, 2025
Chethan-rao
Chethan-rao previously approved these changes Feb 25, 2025
@hrithikesh026 hrithikesh026 dismissed stale reviews from Chethan-rao and jarnura via 87db6fe February 25, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
M-database-changes Metadata: This PR involves database schema changes S-waiting-on-review Status: This PR has been implemented and needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] create accounts schema for accounts tables
5 participants