A secure SQL execution engine with risk analysis and reversible snapshot support.
Project description
🛡️ DB Safe Layer
AI-generated SQL must pass through a Safe Execution Layer before execution.
Database safety is no longer a human review task.
LLM agents, automation scripts, and internal tools can generate SQL—but execution remains the real risk.
DB Safe Layer is a lightweight SQL firewall that intercepts every statement before it reaches your database and performs:
• SQL structural analysis
• Dry-run impact estimation
• Risk classification
• Optional snapshot creation
• Gated execution
• Full audit + deterministic replay
Just drop it in front of your database—no infra changes.
“Like having a tireless DBA reviewing every command.”
🚧 Why this is needed
Teams across analytics, data engineering, healthcare, finance, and consulting report the same problem:
• LLMs sometimes generate hallucinated or destructive SQL
• Developers rely on manual review (slow + error-prone)
• Operations lack audit logs and replayability
• Even staging databases get damaged accidentally
DB Safe Layer provides a deterministic safety boundary before SQL touches any real data.
🔁 Execution Pipeline (Deterministic)
User SQL Input
│
▼
precheck(sql)
│
▼
dry_run(sql)
│
▼
estimated_rows
│
▼
analyze_risk(sql, rows)
│
├── LOW → execute(sql)
│
└── MEDIUM/HIGH → ask user yes/no
│
├── no → abort
│
└── yes → snapshot() → execute(sql)
│
▼
write audit.json
│
▼
return result
Every step is recorded.
Every run can be replayed deterministically.
✨ Features
🧠 1. Structural Risk Analysis
Using SQLGlot AST parsing—not regex.
Detects:
• DROP / TRUNCATE / ALTER
• DELETE / UPDATE without WHERE
• Cross-table mutations
• Write operations on protected tables
Produces standardized risk levels: LOW / MEDIUM / HIGH / CRITICAL.
🔮 2. Dry-Run (Non-Destructive Impact Estimation)
Before running a write query:
DELETE → SELECT COUNT(*)
UPDATE → SELECT COUNT(*)
INSERT → SELECT COUNT(*) FROM VALUES(...)
Allows users to see:
“This will update 3,214 rows. Proceed?”
📸 3. Automatic Snapshot
For high-risk operations, DB Safe Layer creates a snapshot:
• SQLite → file copy
• PostgreSQL → CREATE TABLE AS snapshot / txid
Snapshots are references, and backups—fast and reversible.
📝 4. Full Audit + Replay
Every run logs:
• SQL
• Parsed AST
• Risk level
• Dry-run result
• Snapshot reference
• Execution decision
• Final result
Replay re-executes only read-only steps, without touching the database.
📂 Structure
db-safe-layer/
│
├── app.py # Example runner
├── db/
│ ├── config.py # DB configuration
│ ├── database.py # Engine/session management
│ └── snapshot.py # Snapshot creation
│
├── execution/
│ └── executor.py # Precheck → Dry-run → Risk → Execution
│
├── utils/
│ ├── risk_policy.py # Risk classifier
│ └── sqlglot_helper.py # SQL AST parsing + rewriting
│
├── audit/
│ ├── log_manager.py # Write audit logs
│ └── replay.py # Deterministic replay
│
└── requirements.txt
SQL → Precheck → Dry-Run → Risk analyse → Approval → Snapshot → Execution DAG → Audit + Replay
Stack
- SQLGlot:SQL AST、dry-run(SELECT COUNT(*) FROM (...)
- SQLAlchemy
🚀Quick Start
Installation
Clone project
Bash
git clone https://github.com/interact-space/database-safe-layer.git
Install
Bash
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
Configure Copy .env.example -> .env and configure the database connection:
Quick Start
✅ Python API
python
from db_safe_ayer import safe_exec, rollback_to
#module SQL check
safe_exec(SQL)
#module rollback to specified snapshot id
rollback_to()
✅ CLI
bash
#SQL check
safe-layer <SQL>
#rollback to specified snapshot id
safe-db-rollback
💬 Join the Discussion
If you have any ideas, suggestions, or questions while using this project, feel free to open an Issue and share your thoughts! Whether it’s a feature request, bug report, improvement proposal, or general discussion, we truly welcome your participation.
👉 Start the conversation here: Issues Your feedback helps make this project better — thank you for your support! 🙌
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 db_safe_layer-0.1.1.tar.gz.
File metadata
- Download URL: db_safe_layer-0.1.1.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7501136232be006240a5b7c5783abbd1366f32f4d1223fdaa98e9dcf941a0476
|
|
| MD5 |
d18b418eec5c6dff6e02d8d56840568b
|
|
| BLAKE2b-256 |
da8b28ce1779387138415462ce26d9cdef9c3545ab7dae733dd3779c2e4424b1
|
File details
Details for the file db_safe_layer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: db_safe_layer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2aad21c13109c7bb4d498eb6079b3a7c2ee485f5cae5611a7cc6808a50c738f0
|
|
| MD5 |
a9a0d12dd581c677b6f4bf5863576922
|
|
| BLAKE2b-256 |
fe40e67d0fabf7182300b428e8854a38b787fb0c1a3a7695f1f7d1eced671750
|