Drop Table Check
Check ID: drop_table | Severity: HIGH
What It Checks For
This check detects DROP TABLE operations which could potentially cause data loss and application errors.
Example risky SQL:
Why Its Risky
Dropping a table is a high-risk operation because:
- It permanently deletes all data stored in the table
- It cannot be easily reversed without a proper backup
- It can cause application errors if code still references the table
- It may impact dependent objects like views, functions, and triggers
Safer Alternative
Instead of immediately dropping tables, consider:
- Rename instead of drop: Temporarily rename the table (e.g., add '_bak' suffix) to ensure no issues before permanently dropping it
- Two-step migration: First remove all application references to the table, then drop it in a separate migration
- Use IF EXISTS: Always use
DROP TABLE IF EXISTSto prevent errors if the table doesn't exist - Consider dependencies: Check for and handle dependent objects before dropping tables
Example safer approach:
-- 1. First rename the table to mark it for deletion
ALTER TABLE customers RENAME TO customers_to_delete;
-- 2. In a later migration (after confirming no issues):
DROP TABLE IF EXISTS customers_to_delete;
Configuration Options
You can configure or disable this check in your .ddlcheck configuration file: