🛡️ SQLGuard
Schema validation, migration generation, and query safety for Python SQL projects.
Features
- Schema Definition: Define database schemas in Python with full type support
- Schema Diff: Compare two schemas and detect breaking vs safe changes
- Migration Generation: Auto-generate ALTER TABLE migration scripts from schema diffs
- SQL Linting: Catch unsafe patterns (SELECT *, missing WHERE on DELETE/UPDATE, SQL injection risks)
- Query Validation: Validate queries against your schema (column existence, type compatibility)
- CLI: Full command-line interface for all operations
- Multiple Dialects: Support for PostgreSQL, MySQL, and SQLite column types
Installation
pip install sqlguard
Quick Start
Define a Schema
from sqlguard import Schema, Table, Column
schema = Schema("my_app", [
Table("users", [
Column("id", "integer", primary_key=True),
Column("email", "varchar", nullable=False, unique=True),
Column("name", "varchar", nullable=False),
Column("created_at", "timestamp", default="now()"),
]),
Table("posts", [
Column("id", "integer", primary_key=True),
Column("user_id", "integer", nullable=False, references="users.id"),
Column("title", "varchar", nullable=False),
Column("body", "text"),
Column("published", "boolean", default="false"),
]),
])
Diff Two Schemas
from sqlguard import SchemaDiffer
differ = SchemaDiffer()
diff = differ.diff(old_schema, new_schema)
for change in diff.breaking_changes:
print(f"⚠️ BREAKING: {change}")
for change in diff.safe_changes:
print(f"✅ SAFE: {change}")
Generate Migrations
from sqlguard import MigrationGenerator
generator = MigrationGenerator(dialect="postgresql")
migrations = generator.generate(diff)
for migration in migrations:
print(migration.to_sql())
Lint SQL Queries
from sqlguard import SQLLinter
linter = SQLLinter()
issues = linter.lint("SELECT * FROM users WHERE id = " + str(user_id))
for issue in issues:
print(f"[{issue.severity}] {issue.rule}: {issue.message}")
Validate Queries Against Schema
from sqlguard import QueryValidator
validator = QueryValidator(schema)
errors = validator.validate("SELECT email, nam FROM users")
for error in errors:
print(f"Invalid column: {error.column} in table: {error.table}")
CLI
# Lint a SQL file
sqlguard lint queries.sql
# Diff two schema files
sqlguard diff old_schema.py new_schema.py
# Generate migration from diff
sqlguard migrate old_schema.py new_schema.py --dialect postgresql
# Validate queries against schema
sqlguard validate queries.sql --schema schema.py
License
MIT
Metadata
Release files for sqlwarden 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlwarden-0.1.0.tar.gz | 73.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlwarden-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 133.9 kB
Release files / sqlwarden-0.1.0.tar.gz
| Download URL | sqlwarden-0.1.0.tar.gz |
|---|---|
| Size | 73.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5c2d1523578df6d9a82e006fdf9ca1534e48df80c32ced0d6105436ebe5077a
|
|
BLAKE2b-256 checksum How to use checksums |
1051bf9722cc054cc088e6dcfae00c4bd2a5a2844d87f6e8bff646a6340c8950
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / sqlwarden-0.1.0-py3-none-any.whl
| Download URL | sqlwarden-0.1.0-py3-none-any.whl |
|---|---|
| Size | 60.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f35bf67fc68dc308d430acf7853c893dc03692559c63244638becbcb3bbd93f0
|
|
BLAKE2b-256 checksum How to use checksums |
656e031ab69e1f1d73947968b88e56d64ebe6dbd4c0b767cc625383ebe878976
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|