Schema validation, migration generation, and query safety for Python SQL projects
Project description
🛡️ 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
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
sqlwarden-0.1.0.tar.gz
(73.5 kB
view details)
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
sqlwarden-0.1.0-py3-none-any.whl
(60.4 kB
view details)
File details
Details for the file sqlwarden-0.1.0.tar.gz.
File metadata
- Download URL: sqlwarden-0.1.0.tar.gz
- Upload date:
- Size: 73.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5c2d1523578df6d9a82e006fdf9ca1534e48df80c32ced0d6105436ebe5077a
|
|
| MD5 |
3c50d09950a63551e0089fa50432e1ed
|
|
| BLAKE2b-256 |
1051bf9722cc054cc088e6dcfae00c4bd2a5a2844d87f6e8bff646a6340c8950
|
File details
Details for the file sqlwarden-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlwarden-0.1.0-py3-none-any.whl
- Upload date:
- Size: 60.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f35bf67fc68dc308d430acf7853c893dc03692559c63244638becbcb3bbd93f0
|
|
| MD5 |
a2e242898e1823c64cdc95ef01f8a064
|
|
| BLAKE2b-256 |
656e031ab69e1f1d73947968b88e56d64ebe6dbd4c0b767cc625383ebe878976
|