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(core): change primary keys in user, user_roles and roles tables (#5374)

Prajjwal Kumarb51c8e1d12c2
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 4b5f4a8ad4..b8c5344cbb 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -1168,7 +1168,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- roles (id) {
+ roles (role_id) {
id -> Int4,
#[max_length = 64]
role_name -> Varchar,
@@ -1251,7 +1251,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- user_roles (id) {
+ user_roles (user_id, merchant_id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
@@ -1275,7 +1275,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- users (id) {
+ users (user_id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rs
index a2e35a6d6a..c831bbfa70 100644
--- a/crates/diesel_models/src/schema_v2.rs
+++ b/crates/diesel_models/src/schema_v2.rs
@@ -1166,7 +1166,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- roles (id) {
+ roles (role_id) {
id -> Int4,
#[max_length = 64]
role_name -> Varchar,
@@ -1249,7 +1249,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- user_roles (id) {
+ user_roles (user_id, merchant_id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
@@ -1273,7 +1273,7 @@ diesel::table! {
use diesel::sql_types::*;
use crate::enums::diesel_exports::*;
- users (id) {
+ users (user_id) {
id -> Int4,
#[max_length = 64]
user_id -> Varchar,
diff --git a/migrations/2024-07-19-095541_change_primary_key_for_users/down.sql b/migrations/2024-07-19-095541_change_primary_key_for_users/down.sql
new file mode 100644
index 0000000000..6ba57be2af
--- /dev/null
+++ b/migrations/2024-07-19-095541_change_primary_key_for_users/down.sql
@@ -0,0 +1,5 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE users DROP CONSTRAINT users_pkey;
+
+ALTER TABLE users
+ADD PRIMARY KEY (id);
diff --git a/migrations/2024-07-19-095541_change_primary_key_for_users/up.sql b/migrations/2024-07-19-095541_change_primary_key_for_users/up.sql
new file mode 100644
index 0000000000..3d53115c17
--- /dev/null
+++ b/migrations/2024-07-19-095541_change_primary_key_for_users/up.sql
@@ -0,0 +1,12 @@
+-- Your SQL goes here
+-- The below query will lock the users table
+-- Running this query is not necessary on higher environments
+-- as the application will work fine without these queries being run
+-- This query should be run after the new version of application is deployed
+ALTER TABLE users DROP CONSTRAINT users_pkey;
+
+-- Use the `user_id` columns as primary key
+-- These are already unique, not null column
+-- So this query should not fail for not null or duplicate value reasons
+ALTER TABLE users
+ADD PRIMARY KEY (user_id);
diff --git a/migrations/2024-07-19-100016_change_primary_key_for_user_roles/down.sql b/migrations/2024-07-19-100016_change_primary_key_for_user_roles/down.sql
new file mode 100644
index 0000000000..47b9f679c4
--- /dev/null
+++ b/migrations/2024-07-19-100016_change_primary_key_for_user_roles/down.sql
@@ -0,0 +1,5 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE user_roles DROP CONSTRAINT user_roles_pkey;
+
+ALTER TABLE user_roles
+ADD PRIMARY KEY (id);
diff --git a/migrations/2024-07-19-100016_change_primary_key_for_user_roles/up.sql b/migrations/2024-07-19-100016_change_primary_key_for_user_roles/up.sql
new file mode 100644
index 0000000000..65d9c61fcb
--- /dev/null
+++ b/migrations/2024-07-19-100016_change_primary_key_for_user_roles/up.sql
@@ -0,0 +1,12 @@
+-- Your SQL goes here
+-- The below query will lock the user_roles table
+-- Running this query is not necessary on higher environments
+-- as the application will work fine without these queries being run
+-- This query should be run after the new version of application is deployed
+ALTER TABLE user_roles DROP CONSTRAINT user_roles_pkey;
+
+-- Use the `user_id, merchant_id` columns as primary key
+-- These are already unique, not null columns
+-- So this query should not fail for not null or duplicate value reasons
+ALTER TABLE user_roles
+ADD PRIMARY KEY (user_id, merchant_id);
diff --git a/migrations/2024-07-19-100936_change_primary_key_for_roles/down.sql b/migrations/2024-07-19-100936_change_primary_key_for_roles/down.sql
new file mode 100644
index 0000000000..70e4d1674a
--- /dev/null
+++ b/migrations/2024-07-19-100936_change_primary_key_for_roles/down.sql
@@ -0,0 +1,5 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE roles DROP CONSTRAINT roles_pkey;
+
+ALTER TABLE roles
+ADD PRIMARY KEY (id);
diff --git a/migrations/2024-07-19-100936_change_primary_key_for_roles/up.sql b/migrations/2024-07-19-100936_change_primary_key_for_roles/up.sql
new file mode 100644
index 0000000000..8bde2a3b42
--- /dev/null
+++ b/migrations/2024-07-19-100936_change_primary_key_for_roles/up.sql
@@ -0,0 +1,12 @@
+-- Your SQL goes here
+-- The below query will lock the user_roles table
+-- Running this query is not necessary on higher environments
+-- as the application will work fine without these queries being run
+-- This query should be run after the new version of application is deployed
+ALTER TABLE roles DROP CONSTRAINT roles_pkey;
+
+-- Use the `role_id` column as primary key
+-- These are already unique, not null column
+-- So this query should not fail for not null or duplicate value reasons
+ALTER TABLE roles
+ADD PRIMARY KEY (role_id);