<!--
title: Use defensive SQL clauses
domain: app-frameworks
topic: Migrations
language: Sql
source: juspay/hyperswitch
updated: 2025-09-04
url: https://awesomereviewers.com/reviewers/hyperswitch-use-defensive-sql-clauses/
-->

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:
```sql
-- 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.
