12-database-sql

12-database-sql is a cursor rule for Cursor from bybren-llc/safe-agentic-workflow. It costs 0 tokens per session (613 once invoked), scanned A, original, MIT.

A set of database rules for PostgreSQL, a relational database, using SQLAlchemy models and Alembic migrations. It requires Row Level Security, which limits database rows according to the user or system requesting them.

In plain words
What is it for?
Use it when changing database tables, models, migrations, or access policies, and when checking user, administrator, and background-task data isolation.
Why use it?
It helps prevent users from seeing one another’s data and ensures schema changes include the required access checks.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use it when changing database tables, models, migrations, or access policies, and when checking user, administrator, and background-task data isolation.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/bybren-llc/safe-agentic-workflow/12-database-sql
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/bybren-llc/safe-agentic-workflow

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for 12-database-sql

README.md
[![agentmods](https://agentmods.dev/badge/rules/bybren-llc/safe-agentic-workflow/12-database-sql/github.svg)](https://agentmods.dev/rules/bybren-llc/safe-agentic-workflow/12-database-sql)
Your own site
<a href="https://agentmods.dev/rules/bybren-llc/safe-agentic-workflow/12-database-sql"><img src="https://agentmods.dev/badge/rules/bybren-llc/safe-agentic-workflow/12-database-sql/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for 12-database-sql

Your own site · 80×15
<a href="https://agentmods.dev/rules/bybren-llc/safe-agentic-workflow/12-database-sql"><img src="https://agentmods.dev/badge/rules/bybren-llc/safe-agentic-workflow/12-database-sql.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 613 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00000 $0.00613
Opus 5.5 $0.00000 $0.00245
Sonnet 5.5 $0.00000 $0.00123
Haiku 4.5 $0.00000 $0.00061

Measured 1mo ago against content hash 71880a0c52bb, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

12-database-sql scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 1mo ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/12-database-sql.mdc · 83 lines

How it starts

The opening of the file, as written. The whole thing — 83 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Database and SQL Rules

These rules apply when working on database models, schemas, and migrations.

Technology

  • Database: PostgreSQL 16
  • ORM: SQLAlchemy 2.x
  • Migrations: Alembic
  • RLS: Row Level Security (MANDATORY)

Row Level Security (RLS)

CRITICAL: RLS is mandatory for ALL tables containing user data.

RLS Context Helpers

Every database operation MUST use the appropriate context helper:

Helper Use Case
with_user_context User-scoped operations
with_admin_context Admin-scoped operations
with_system_context Background/webhook operations

Zero Tolerance

  • No direct session/ORM calls without RLS context wrappers
  • No bypassing RLS policies for convenience
  • No using superuser credentials in application code

RLS Validation

After any schema change, validate RLS:

  • User isolation: User A cannot see User B's data
  • Admin access: Admin can see data within policy scope
  • System context: Background tasks use system context

Migration Workflow

# 1. Create migration
alembic revision --autogenerate -m "description"

# 2. REVIEW the generated migration (do not blindly apply)
# 3. Add RLS policies if new table created
# 4. Test locally
alembic upgrade head

# 5. Commit
git add alembic/versions/
git commit -m "feat(db): description [{{TICKET_PREFIX}}-XXX]"

Migration Rules

  • Never use DROP TABLE without a backup strategy
  • Never skip Alembic (no manual DDL in production)
  • Always add proper indexes for query patterns
  • Add RLS policies for every new table with user data
  • Schema changes REQUIRE System Architect approval

SQLAlchemy Model Standards

  • Use SQLAlchemy 2.x Mapped type annotations
  • Define relationships explicitly
  • Include created_at and updated_at timestamps on all tables
  • Use UUIDs for primary keys

Key References

  • docs/database/DATA_DICTIONARY.md -- Schema reference (SINGLE SOURCE OF TRUTH)
  • docs/database/RLS_DATABASE_MIGRATION_SOP.md -- Migration SOP (MANDATORY reading)
  • docs/database/RLS_IMPLEMENTATION_GUIDE.md -- RLS implementation details
  • patterns_library/database/rls-migration.md -- RLS migration pattern
  • patterns_library/database/prisma-transaction.md -- Transaction pattern

Read the full file on GitHub · 83 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 1mo ago First seen · 83 lines · 0 tokens per session scan A 71880a0c52bb

Subscribe to this mod's changes

12-database-sql is a cursor rule published in the GitHub repository bybren-llc/safe-agentic-workflow (423 stars, last pushed 2mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 613 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.