Skip to main content

Postgres migration safety analyzer — catch dangerous operations before they run

Project description

LockSafe AI

Catch unsafe Postgres migrations before they reach production.

LockSafe AI is a CLI tool that analyzes PostgreSQL migration files and detects operations that can cause production downtime or data loss — before they are applied.


Installation

pip install locksafe

Requires Python 3.10+


Usage

Analyze a single file

locksafe analyze migration.sql

With options:

locksafe analyze migration.sql --severity HIGH
locksafe analyze migration.sql --fail-on CRITICAL
locksafe analyze migration.sql --format json
cat migration.sql | locksafe analyze --stdin

Check multiple files (CI/CD)

Analyze an entire directory:

locksafe check migrations/

Only check files changed since a git reference:

locksafe check . --since origin/main

Fail the build on unsafe migrations:

locksafe check migrations/ --fail-on HIGH

Example

Input

CREATE INDEX idx_users_email ON users(email);
 
ALTER TABLE users ADD COLUMN status TEXT NOT NULL;
 
TRUNCATE payments;

Output

🔐 LockSafe Analysis
File: migration.sql
Statements: 3 │ Critical: 2 │ High: 0 │ Medium: 0 │ Low: 0
 
⚠ Migration risks detected
 
🔴 [CRITICAL] Statement 1
↳ CREATE INDEX without CONCURRENTLY on table 'users'
 
🔴 [CRITICAL] Statement 2
↳ ADD COLUMN NOT NULL without DEFAULT causes table rewrite
 
🔴 [CRITICAL] Statement 3
↳ TRUNCATE destroys all rows in payments

CLI Reference

locksafe analyze

Option Description
--format json Output results in JSON format
--severity LEVEL Show findings at or above severity
--fail-on LEVEL Exit with code 1 if threshold is met
--ignore RULE_ID Ignore specific rule(s)
--quiet No output if no issues found
--stdin Read SQL from stdin

locksafe check

Option Description
--since REF Only check files changed since git ref
--format json Output results in JSON
--severity LEVEL Filter findings by severity
--fail-on LEVEL Exit threshold
--ignore RULE_ID Ignore specific rules

Severity Levels

Level Meaning
🔴 CRITICAL High risk of downtime or data loss
🟠 HIGH Likely to cause production issues
🟡 MEDIUM May cause performance or locking issues
🔵 LOW Informational or minor risk

Exit Codes

Code Meaning
0 No issues found
1 Issues detected (based on --fail-on)
2 SQL parsing error

Debug Mode

locksafe --debug analyze migration.sql

CI/CD Integration

Block unsafe migrations in your pipeline:

locksafe check migrations/

Fail only on critical issues:

locksafe check migrations/ --fail-on CRITICAL

Check only changed files on pull requests:

locksafe check . --since origin/main

Exits with code 1 if risks are detected at or above the configured threshold.


Tips

  • Always review CRITICAL findings before applying migrations in production
  • Use CONCURRENTLY for index operations on large tables
  • Avoid schema changes that trigger a full table rewrite during peak traffic
  • Run locksafe explain <rule-id> to understand any flagged rule in depth

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

locksafe-0.3.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

locksafe-0.3.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file locksafe-0.3.1.tar.gz.

File metadata

  • Download URL: locksafe-0.3.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for locksafe-0.3.1.tar.gz
Algorithm Hash digest
SHA256 0a5026db64f6cb0aca927bdace8141df6c3a952103d604e304b34612ca078769
MD5 4ef280cc83d2363a35b8e38e739d685b
BLAKE2b-256 8712a2c950110d2703394f0f0c3f27f21612b153eaabaf42cf434d6df99fcd4a

See more details on using hashes here.

File details

Details for the file locksafe-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: locksafe-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for locksafe-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9647dfc72db7c2e0e2c25037cbf155324858d726a216c81328e06402cb5621e9
MD5 44a87cc04e5e65442d1cbac8d24cac15
BLAKE2b-256 5de2b6d0283b417b4c6e551c777ce466e3662be5dc210a3dbee0565d1b96be9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page