Skip to content

Commit

Permalink
refactor v2 migrations to make v2 columns not null after introducing …
Browse files Browse the repository at this point in the history
…it as nullable field and vice versa
  • Loading branch information
hrithikesh026 committed Feb 20, 2025
1 parent 5ff01f1 commit ddef69b
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 144 deletions.
7 changes: 4 additions & 3 deletions v2_migrations/2024-08-28-081721_add_v2_columns/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ ALTER TABLE business_profile DROP COLUMN routing_algorithm_id,
DROP COLUMN frm_routing_algorithm_id,
DROP COLUMN payout_routing_algorithm_id,
DROP COLUMN default_fallback_routing,
DROP COLUMN should_collect_cvv_during_payment;
DROP COLUMN should_collect_cvv_during_payment,
DROP COLUMN three_ds_decision_manager_config;

DROP TYPE "OrderFulfillmentTimeOrigin";

Expand Down Expand Up @@ -46,8 +47,8 @@ ALTER TABLE payment_attempt DROP COLUMN payment_method_type_v2,
DROP COLUMN connector_token_details;

ALTER TABLE merchant_connector_account
ALTER COLUMN payment_methods_enabled TYPE JSON [ ];

ALTER COLUMN payment_methods_enabled TYPE JSON [ ],
DROP COLUMN IF EXISTS feature_metadata;

ALTER TABLE payment_methods
DROP COLUMN IF EXISTS locker_fingerprint_id,
Expand Down
8 changes: 5 additions & 3 deletions v2_migrations/2024-08-28-081721_add_v2_columns/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ALTER TABLE customers
ADD COLUMN IF NOT EXISTS merchant_reference_id VARCHAR(64),
ADD COLUMN IF NOT EXISTS default_billing_address BYTEA DEFAULT NULL,
ADD COLUMN IF NOT EXISTS default_shipping_address BYTEA DEFAULT NULL,
ADD COLUMN IF NOT EXISTS status "DeleteStatus" NOT NULL DEFAULT 'active';
ADD COLUMN IF NOT EXISTS status "DeleteStatus";

CREATE TYPE "OrderFulfillmentTimeOrigin" AS ENUM ('create', 'confirm');

Expand All @@ -15,13 +15,14 @@ ADD COLUMN routing_algorithm_id VARCHAR(64) DEFAULT NULL,
ADD COLUMN frm_routing_algorithm_id VARCHAR(64) DEFAULT NULL,
ADD COLUMN payout_routing_algorithm_id VARCHAR(64) DEFAULT NULL,
ADD COLUMN default_fallback_routing JSONB DEFAULT NULL,
ADD COLUMN three_ds_decision_manager_config jsonb,
-- Intentionally not adding a default value here since we would have to
-- check if any merchants have enabled this from configs table,
-- before filling data for this column.
-- If no merchants have enabled this, then we can use `false` as the default value
-- when adding the column, later we can drop the default added for the column
-- so that we ensure new records inserted always have a value for the column.
ADD COLUMN should_collect_cvv_during_payment BOOLEAN NOT NULL;
ADD COLUMN should_collect_cvv_during_payment BOOLEAN;

ALTER TABLE payment_intent
ADD COLUMN merchant_reference_id VARCHAR(64),
Expand Down Expand Up @@ -56,7 +57,8 @@ ADD COLUMN payment_method_type_v2 VARCHAR,

-- Change the type of the column from JSON to JSONB
ALTER TABLE merchant_connector_account
ALTER COLUMN payment_methods_enabled TYPE JSONB [ ];
ALTER COLUMN payment_methods_enabled TYPE JSONB [ ],
ADD COLUMN IF NOT EXISTS feature_metadata JSONB;

ALTER TABLE payment_methods
ADD COLUMN IF NOT EXISTS locker_fingerprint_id VARCHAR(64),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
-- This file should undo anything in `up.sql`
------------------------ Organization -----------------------
-- Re-add primary key constraint on `organization`
ALTER TABLE organization ADD CONSTRAINT organization_pkey PRIMARY KEY (org_id);
-- Restore `org_id` to NOT NULL
ALTER TABLE organization ALTER COLUMN org_id SET NOT NULL;
ALTER TABLE organization ADD PRIMARY KEY (org_id);

-- Merchant Account
ALTER TABLE merchant_account ADD PRIMARY KEY (merchant_id);
ALTER TABLE merchant_account
ALTER COLUMN primary_business_details SET NOT NULL,
ALTER COLUMN is_recon_enabled SET NOT NULL,
ALTER COLUMN is_recon_enabled SET DEFAULT FALSE;

-- Business Profile
ALTER TABLE business_profile ADD PRIMARY KEY (profile_id);

-- Merchant Connector Account
ALTER TABLE merchant_connector_account ADD PRIMARY KEY (merchant_connector_id);

-- Customers
ALTER TABLE customers ADD PRIMARY KEY (merchant_id, customer_id);

-- Payment Intent
ALTER TABLE payment_intent ADD PRIMARY KEY (payment_id, merchant_id);
ALTER TABLE payment_intent ALTER COLUMN active_attempt_id SET NOT NULL;
ALTER TABLE payment_intent ALTER COLUMN active_attempt_id SET DEFAULT 'xxx';

-- Payment Attempt
ALTER TABLE payment_attempt ADD PRIMARY KEY (attempt_id, merchant_id);
ALTER TABLE payment_attempt ALTER COLUMN amount SET NOT NULL;

-- Payment Methods
ALTER TABLE payment_methods ADD PRIMARY KEY (payment_method_id);
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
-- Drop not null constraint on org_id in orgnaization table
-- Drop not null constraint on org_id in organization table
ALTER TABLE organization DROP CONSTRAINT organization_pkey;
ALTER TABLE organization ALTER COLUMN org_id DROP NOT NULL;
ALTER TABLE organization ALTER COLUMN org_id DROP NOT NULL;

-- Drop not null in merchant_account table for v1 columns that are dropped in v2
ALTER TABLE merchant_account
DROP CONSTRAINT merchant_account_pkey,
ALTER COLUMN merchant_id DROP NOT NULL,
ALTER COLUMN primary_business_details DROP NOT NULL,
ALTER COLUMN is_recon_enabled DROP NOT NULL;

ALTER TABLE business_profile
DROP CONSTRAINT business_profile_pkey,
ALTER COLUMN profile_id DROP NOT NULL;

ALTER TABLE merchant_connector_account
DROP CONSTRAINT merchant_connector_account_pkey,
ALTER COLUMN merchant_connector_id DROP NOT NULL;

ALTER TABLE customers
DROP CONSTRAINT customers_pkey,
ALTER COLUMN customer_id DROP NOT NULL;

ALTER TABLE payment_intent
DROP CONSTRAINT payment_intent_pkey,
ALTER COLUMN payment_id DROP NOT NULL,
ALTER COLUMN active_attempt_id DROP NOT NULL;

ALTER TABLE payment_intent ALTER COLUMN active_attempt_id DROP DEFAULT;

ALTER TABLE payment_attempt
DROP CONSTRAINT payment_attempt_pkey,
ALTER COLUMN attempt_id DROP NOT NULL,
ALTER COLUMN amount DROP NOT NULL;

ALTER TABLE payment_methods
DROP CONSTRAINT payment_methods_pkey,
ALTER COLUMN payment_method_id DROP NOT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ALTER TABLE merchant_connector_account DROP COLUMN IF EXISTS id;


ALTER TABLE business_profile DROP COLUMN IF EXISTS id;

ALTER TABLE customers DROP COLUMN IF EXISTS id;

Expand Down
5 changes: 5 additions & 0 deletions v2_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
ALTER TABLE merchant_connector_account
ADD COLUMN IF NOT EXISTS id VARCHAR(64);

------------------------ Business Profile -----------------------
-- This migration is to modify the id column in business_profile table to be a VARCHAR(64) and to set the id column as primary key
ALTER TABLE business_profile
ADD COLUMN id VARCHAR(64);

------------------------ Customers -----------------------


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,99 +17,53 @@ WHERE org_name IS NULL
------------------------ Merchant Account -----------------------
-- The new primary key for v2 merchant account will be `id`
ALTER TABLE merchant_account DROP CONSTRAINT merchant_account_pkey;

ALTER TABLE merchant_account ALTER COLUMN id DROP NOT NULL;
-- In order to run this query, the merchant_id column should be unique and not null
-- We need to backfill the id, a simple strategy will be to copy the values of id to merchant_id
-- Query to update the merchant_id column with values of id
UPDATE merchant_account
SET merchant_id = id
WHERE merchant_id IS NULL;

-- Note: This command might not run successfully for the existing table
-- This is because there will be some rows ( which are created via v2 application ) which will have id as empty
-- A backfill might be required to run this query
-- However if this is being run on a fresh database, this should succeed
ALTER TABLE merchant_account
ADD PRIMARY KEY (merchant_id);

------------------------ Business Profile -----------------------
ALTER TABLE business_profile DROP CONSTRAINT business_profile_pkey;
ALTER TABLE business_profile ALTER COLUMN id DROP NOT NULL;
UPDATE business_profile
SET profile_id = id
WHERE profile_id IS NULL;

ALTER TABLE business_profile DROP COLUMN id;

ALTER TABLE business_profile
ADD PRIMARY KEY (profile_id);

------------------------ Merchant Connector Account -----------------------
ALTER TABLE merchant_connector_account DROP CONSTRAINT merchant_connector_account_pkey;

ALTER TABLE merchant_connector_account ALTER COLUMN id DROP NOT NULL;
UPDATE merchant_connector_account
SET merchant_connector_id = id
WHERE merchant_connector_id IS NULL;

ALTER TABLE merchant_connector_account
ADD PRIMARY KEY (merchant_connector_id);

ALTER TABLE merchant_connector_account
ALTER COLUMN profile_id DROP NOT NULL;

DROP INDEX IF EXISTS merchant_connector_account_profile_id_index;

------------------------ Customers -----------------------
-- Run this query only when V1 is deprecated
ALTER TABLE customers DROP CONSTRAINT customers_pkey;

ALTER TABLE customers ALTER COLUMN id DROP NOT NULL;
-- Back filling before making it primary key
UPDATE customers
SET customer_id = id
WHERE customer_id IS NULL;

ALTER TABLE customers
ADD PRIMARY KEY (merchant_id, customer_id);

------------------------ Payment Intent -----------------------
ALTER TABLE payment_intent DROP CONSTRAINT payment_intent_pkey;

ALTER TABLE payment_intent ALTER COLUMN id DROP NOT NULL;
UPDATE payment_intent
SET payment_id = id
WHERE payment_id IS NULL;

ALTER TABLE payment_intent
ADD PRIMARY KEY (payment_id, merchant_id);

ALTER TABLE payment_intent
ALTER COLUMN currency DROP NOT NULL,
ALTER COLUMN client_secret DROP NOT NULL,
ALTER COLUMN profile_id DROP NOT NULL;

ALTER TABLE payment_intent
ALTER COLUMN active_attempt_id
SET NOT NULL;

ALTER TABLE payment_intent
ALTER COLUMN session_expiry DROP NOT NULL;

ALTER TABLE payment_intent
ALTER COLUMN active_attempt_id
SET DEFAULT 'xxx';

------------------------ Payment Attempt -----------------------
ALTER TABLE payment_attempt DROP CONSTRAINT payment_attempt_pkey;
ALTER TABLE payment_attempt ALTER COLUMN id DROP NOT NULL;

UPDATE payment_attempt
SET attempt_id = id
WHERE attempt_id IS NULL;

ALTER TABLE payment_attempt
ALTER COLUMN net_amount DROP NOT NULL;

ALTER TABLE payment_attempt
ADD PRIMARY KEY (attempt_id, merchant_id);

------------------------ Payment Methods -----------------------
ALTER TABLE payment_methods DROP CONSTRAINT payment_methods_pkey;

ALTER TABLE payment_methods
ADD PRIMARY KEY (payment_method_id);
ALTER TABLE payment_methods ALTER COLUMN id DROP NOT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ ADD CONSTRAINT organization_organization_name_key UNIQUE (organization_name);

------------------------ Merchant Account -----------------------
-- The new primary key for v2 merchant account will be `id`
ALTER TABLE merchant_account DROP CONSTRAINT merchant_account_pkey;

-- In order to make id as primary key, it should be unique and not null
-- We need to backfill the id, a simple strategy will be to copy the values of merchant_id to id
-- Query to update the id column with values of merchant_id
Expand All @@ -37,17 +35,12 @@ ALTER TABLE merchant_account
ADD PRIMARY KEY (id);

------------------------ Business Profile -----------------------
-- This migration is to modify the id column in business_profile table to be a VARCHAR(64) and to set the id column as primary key
ALTER TABLE business_profile
ADD COLUMN id VARCHAR(64);

-- Backfill the id column with the profile_id to prevent null values
UPDATE business_profile
SET id = profile_id
WHERE id IS NULL;

ALTER TABLE business_profile DROP CONSTRAINT business_profile_pkey;

ALTER TABLE business_profile
ADD PRIMARY KEY (id);

Expand All @@ -57,22 +50,14 @@ UPDATE merchant_connector_account
SET id = merchant_connector_id
WHERE id IS NULL;

ALTER TABLE merchant_connector_account DROP CONSTRAINT merchant_connector_account_pkey;

ALTER TABLE merchant_connector_account
ADD PRIMARY KEY (id);

-- This migration is to make profile_id mandatory in mca table
ALTER TABLE merchant_connector_account
ALTER COLUMN profile_id
SET NOT NULL;

CREATE INDEX IF NOT EXISTS merchant_connector_account_profile_id_index ON merchant_connector_account (profile_id);

------------------------ Customers -----------------------
-- Run this query only when V1 is deprecated
ALTER TABLE customers DROP CONSTRAINT IF EXISTS customers_pkey;

-- Back filling before making it primary key
-- This will fail when making `id` as primary key, if the `customer_id` column has duplicate values.
-- Another option is to use a randomly generated ID instead.
Expand All @@ -84,59 +69,14 @@ ALTER TABLE customers
ADD PRIMARY KEY (id);

------------------------ Payment Intent -----------------------
ALTER TABLE payment_intent DROP CONSTRAINT payment_intent_pkey;

ALTER TABLE payment_intent
ADD PRIMARY KEY (id);

------------------------ Payment Attempt -----------------------
ALTER TABLE payment_attempt DROP CONSTRAINT payment_attempt_pkey;

ALTER TABLE payment_attempt
ADD PRIMARY KEY (id);

-- This migration is to make fields mandatory in payment_intent table
ALTER TABLE payment_intent
ALTER COLUMN profile_id
SET NOT NULL,
ALTER COLUMN currency
SET NOT NULL,
ALTER COLUMN client_secret
SET NOT NULL,
ALTER COLUMN session_expiry
SET NOT NULL,
ALTER COLUMN active_attempt_id DROP NOT NULL;
ALTER TABLE payment_attempt ADD PRIMARY KEY (id);

------------------------ Payment Attempt -----------------------
ALTER TABLE payment_attempt DROP CONSTRAINT payment_attempt_pkey;

ALTER TABLE payment_attempt
ADD PRIMARY KEY (id);

-- This migration is to make fields mandatory in payment_attempt table
ALTER TABLE payment_attempt
ALTER COLUMN net_amount
SET NOT NULL,
ALTER COLUMN authentication_type
SET NOT NULL,
ALTER COLUMN payment_method_type_v2
SET NOT NULL,
ALTER COLUMN payment_method_subtype
SET NOT NULL;

ALTER TABLE payment_intent
ALTER COLUMN session_expiry
SET NOT NULL;

-- This migration is to make fields optional in payment_intent table
ALTER TABLE payment_intent
ALTER COLUMN active_attempt_id DROP NOT NULL;

ALTER TABLE payment_intent
ALTER COLUMN active_attempt_id DROP DEFAULT;

------------------------ Payment Methods -----------------------
ALTER TABLE payment_methods DROP CONSTRAINT IF EXISTS payment_methods_pkey;

ALTER TABLE payment_methods
ADD PRIMARY KEY (id);
Loading

0 comments on commit ddef69b

Please sign in to comment.