Back to all reviewers

Use defensive SQL clauses

juspay/hyperswitch
Based on 2 comments
Sql

Always use defensive SQL clauses like `IF NOT EXISTS` and `IF EXISTS` in migration scripts to make them idempotent and prevent failures when run multiple times or in different environments. This ensures migrations can be safely re-executed without causing errors due to existing schema elements.

Migrations Sql

Reviewer Prompt

Always use defensive SQL clauses like IF NOT EXISTS and IF EXISTS in migration scripts to make them idempotent and prevent failures when run multiple times or in different environments. This ensures migrations can be safely re-executed without causing errors due to existing schema elements.

Example:

-- Good: Uses IF NOT EXISTS to prevent errors
ALTER TABLE payment_intent ADD COLUMN IF NOT EXISTS enable_overcapture BOOLEAN;

-- Good: Uses IF EXISTS for safe removal
ALTER TABLE subscription DROP COLUMN IF EXISTS profile_id;

When performing complex operations like column renaming or constraint modifications, understand that some operations require separate statements and cannot be nested within a single ALTER TABLE command.

2
Comments Analyzed
Sql
Primary Language
Migrations
Category

Source Discussions