Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(merchant_account): change primary key for merchant account (#5327)

Narayan Bhata0c367ee8c7f
diff --git a/crates/diesel_models/src/query/merchant_account.rs b/crates/diesel_models/src/query/merchant_account.rs
index dd2f284305..66526e11cb 100644
--- a/crates/diesel_models/src/query/merchant_account.rs
+++ b/crates/diesel_models/src/query/merchant_account.rs
@@ -22,7 +22,7 @@ impl MerchantAccount {
) -> StorageResult<Self> {
match generics::generic_update_by_id::<<Self as HasTable>::Table, _, _, _>(
conn,
- self.id,
+ self.merchant_id.clone(),
merchant_account,
)
.await
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 68689e8644..b27d1e9611 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -633,7 +633,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- merchant_account (id) {
+ merchant_account (merchant_id) {
id -> Int4,
#[max_length = 64]
merchant_id -> Varchar,
diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rs
index e353cbd566..3fdbf45025 100644
--- a/crates/diesel_models/src/schema_v2.rs
+++ b/crates/diesel_models/src/schema_v2.rs
@@ -633,7 +633,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- merchant_account (id) {
+ merchant_account (merchant_id) {
id -> Int4,
#[max_length = 64]
merchant_id -> Varchar,
diff --git a/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/down.sql b/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/down.sql
new file mode 100644
index 0000000000..2f169d8019
--- /dev/null
+++ b/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/down.sql
@@ -0,0 +1,5 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE merchant_account DROP CONSTRAINT merchant_account_pkey;
+
+ALTER TABLE merchant_account
+ADD PRIMARY KEY (id);
diff --git a/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/up.sql b/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/up.sql
new file mode 100644
index 0000000000..5db5076c07
--- /dev/null
+++ b/migrations/2024-07-15-111327_change_primary_key_for_merchant_account/up.sql
@@ -0,0 +1,13 @@
+-- Your SQL goes here
+-- The below query will lock the merchant account table
+-- Running this query is not necessary on higher environments
+-- as the application will work fine without these queries being run
+-- This query is necessary for the application to not use id in update of merchant_account
+-- This query should be run after the new version of application is deployed
+ALTER TABLE merchant_account DROP CONSTRAINT merchant_account_pkey;
+
+-- Use the `merchant_id` column as primary key
+-- This is already a unique, not null column
+-- So this query should not fail for not null or duplicate values reasons
+ALTER TABLE merchant_account
+ADD PRIMARY KEY (merchant_id);