apply-ecto-conventions
Apply Ecto Conventions
Use this skill when writing or reviewing Ecto database code to ensure consistent, idiomatic patterns.
Precondition: Invoke ecto-essentials before this skill for the full Ecto reference.
Canonical FP bar: docs/fcis-engineering-rules.md — Functional Core, Imperative Shell: pure domain modules; side effects at edges. Build changesets/Multi in pure-ish functions; run Repo once at the context edge.
RULES — Follow these with no exceptions
1. Never call Repo from LiveViews or controllers — all database operations belong in context modules
2. Prefer non-bang functions in application logic (Repo.get/1, Repo.insert/1) — use bang only in tests
3. Parameterize all user input in queries — use ^ for interpolation, never string concatenation in fragment
4. Always preload associations outside loops to prevent N+1 queries
5. Add foreign_key_constraint and unique_constraint in changesets to match database constraints
6. Use Ecto.Multi for 2+ related operations — never chain multiple Repo calls in sequence without a transaction
7. Add indexes on foreign keys and frequently queried columns
8. Never combine schema changes and data backfill in the same migration