agentleFS
bybren-llcbybren-llc/safe-agentic-workflow/.cursor/rules/12-database-sql.mdc

safe-agentic-workflow / rules

Cursor rule419 starsChanged 4 months agoMITView on GitHub

Database rules for PostgreSQL 16, Alembic migrations, SQLAlchemy models, and Row Level Security.

  • Deletes or force-pushes

The file

We split this 12-database-sql.mdc into the rules it gives an agent

11 rules. About 0 are common, 9 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every database operation MUST use the appropriate context helper:

    line 24
  2. Distinctive:

    No direct session/ORM calls without RLS context wrappers

    line 34
  3. Distinctive:

    No bypassing RLS policies for convenience

    line 35
  4. Distinctive:

    No using superuser credentials in application code

    line 36
  5. Distinctive:

    After any schema change, validate RLS:

    line 40
  6. Very specific:

    Never use DROP TABLE without a backup strategy

    line 63
  7. Distinctive:

    Never skip Alembic (no manual DDL in production)

    line 64
  8. Distinctive:

    Always add proper indexes for query patterns

    line 65
  9. Distinctive:

    Add RLS policies for every new table with user data

    line 66
  10. Very specific:

    Use SQLAlchemy 2.x Mapped type annotations

    line 71
  11. Distinctive:

    Use UUIDs for primary keys

    line 74

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
12-database-sql.mdc
---
description: "Database rules for PostgreSQL 16, Alembic migrations, SQLAlchemy models, and Row Level Security."
globs: "alembic/**,**/models.py,**/schemas.py,**/models/**"
alwaysApply: false
---

# 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

```bash
# 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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.