CLI tool to analyze SQL migrations for potential production database risks
Project description
DDLCheck
DDLCheck is a tool that scans PostgreSQL SQL migration files for potentially risky operations that could cause downtime, data loss, or other issues in production environments.
Overview
Database migrations can be risky, especially in production environments with large tables and high traffic. DDLCheck analyzes your SQL migrations to identify operations that:
- Cause table rewrites (ALTER COLUMN TYPE, DROP COLUMN)
- Acquire excessive locks (non-CONCURRENT indexes, SET NOT NULL)
- May lead to data loss (DROP TABLE, TRUNCATE)
- Affect all rows without filtering (UPDATE without WHERE)
Installation
# Install with pip
pip install ddlcheck
# Or with Poetry
poetry add ddlcheck
Usage
# Check a single SQL file
ddlcheck check migration.sql
# Check a directory of SQL files
ddlcheck check migrations/
# Exclude specific checks
ddlcheck check migrations/ --exclude add_column_not_null_default,drop_table
# List all available checks
ddlcheck list-checks
# Show version
ddlcheck version
Example Output
File: migration.sql
┏━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Line ┃ Severity ┃ Check ┃ Message ┃
┡━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ 1 │ HIGH │ add_column_not_null_default │ Column 'email_verified' added to table 'users' with NOT NULL and DEFAULT │
└──────┴──────────┴────────────┴──────────────────────────────────────────────────────────────────────────┘
Suggestion for add_column_not_null_default (line 1):
Consider using two separate migrations:
1. First add the column with a DEFAULT but as nullable
2. After data has been populated, add the NOT NULL constraint
Available Checks
DDLCheck includes multiple checks for common risky operations:
-
High Severity:
add_column_not_null_default: Detects when columns are added with NOT NULL constraints and DEFAULT valuesalter_column_type: Detects ALTER COLUMN TYPE operations that require table rewritesdrop_table: Detects DROP TABLE operations that could result in data losstruncate: Detects TRUNCATE operations which can cause data loss and locksupdate_without_filter: Detects UPDATE statements without WHERE clauses
-
Medium Severity:
create_index: Detects index creation without the CONCURRENTLY optiondrop_column: Detects DROP COLUMN operations that require table rewritesrename_column: Detects column renames that can break dependent objectsset_not_null: Detects when NOT NULL constraints are added to existing columns
Configuration
You can configure DDLCheck using a .ddlcheck file in TOML format:
# List of check IDs to disable
excluded_checks = ["drop_table", "truncate"]
# Override severity levels
[severity]
create_index = "LOW"
add_column_not_null_default = "HIGH"
# Individual check configurations
[create_index]
ignore_non_concurrent = false
min_size_warning = 1000
Documentation
For more detailed documentation, please visit our documentation site.
Contributing
Contributions are welcome! See CONTRIBUTING.md for more information on how to get started.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ddlcheck-0.1.0.tar.gz.
File metadata
- Download URL: ddlcheck-0.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.9.19 Darwin/24.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81cc976bee1795329f5d0fb0f5e6eba408fa14b4ddc9528abdc3b9ee55a3e8ea
|
|
| MD5 |
db734c7310f16bd0e17601e6337106c5
|
|
| BLAKE2b-256 |
172896d9639af9cc9731ba92848659943553c4657c53338f5abc818b78c99188
|
File details
Details for the file ddlcheck-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ddlcheck-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.2 CPython/3.9.19 Darwin/24.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd2414ff99f8d44a93ac89c38a2e1d71dd3d84b02596e6273af17019122fd437
|
|
| MD5 |
dc93f270a7d3ca2f72363ef81b82b0f4
|
|
| BLAKE2b-256 |
3be924f128e967d79aed1c77803010e93cd243a56ff7c94f998097ca8cfa12bc
|