SQL Change
This learning is based on a real production incident that affected multiple services.
Let’s examine how to properly review SQL changes, using a real-world example where a missing WHERE clause caused widespread data corruption.
The Scenario
A developer made changes to an SQL query that updates authorized representative flags. While the changes were reviewed, they caused data corruption because:
- The
WHEREclause was accidentally removed. - The review process didn’t catch the missing clause.
- Integration tests didn’t verify unintended data changes.
- No automated checks for large-scale data updates.
- Manual testing focused only on the happy path.
Before
user_records.sql
-- Original query with proper WHERE clause
UPDATE user_records
SET is_authorized_representative = false
WHERE entity_id = :entity_id
AND email != :target_email;
UPDATE user_records
SET is_authorized_representative = true
WHERE entity_id = :entity_id
AND email = :target_email;PR Comment
Choose the comment that you think is the most constructive and helpful.
Click here to learn more
Key Lessons
1. SQL Review Fundamentals
- Check for
WHEREclauses - Verify affected records
- Consider data impact
- Use SQL builder tools
2. Testing Strategy
- Verify unintended changes
- Test with realistic data
- Add integration tests
- Monitor affected rows
3. Review Best Practices
- Use automated tools
- Require multiple reviewers
- Check for data safety
- Consider rollback plans
Tips for Reviewers
1. Ask SQL-Focused Questions
- Are all clauses present?
- How many rows will be affected?
- Is data properly scoped?
- Example: “Should this UPDATE have a WHERE clause?“
2. Verify Testing Approach
- Are changes verified?
- Is data impact tested?
- Are there safety checks?
- Example: “How do we verify only intended records change?“
3. Document Requirements
- List SQL safety checks
- Note testing requirements
- Document verification steps
- Example: “SQL changes must include row count checks”
Common Pitfalls to Avoid
1. Focusing Only on Syntax
- ❌ “The SQL syntax looks good.”
- ✅ “The SQL syntax looks good, but let’s verify the
WHEREclause.”
2. Insufficient Testing
- ❌ “The tests pass, so it’s good.”
- ✅ “The tests pass, but let’s verify unintended data changes.”
3. Missing Safety Checks
- ❌ “It’s just a simple UPDATE.”
- ✅ “Let’s add logging and verification for this UPDATE.”
Remember: A good SQL review considers data safety and unintended consequences. Understanding the full impact of SQL changes and implementing proper safeguards helps prevent data corruption!
Last updated on