1---
2name: sql-dao
3description: SQL data access best practices for AIRBot reviewers
4license: MIT
5metadata:
6 source: https://skillmd.com/skills/majiayu000/sql-dao
7---
8
9## Mission
10- Guard database performance and correctness by enforcing disciplined DAO/DAL patterns.
11- Catch regressions that risk outages: unbounded queries, missing indexes, unsafe scripts, or misuse of replicas.
12
13## Query Execution Standards
14- Require explicit column selection (`SELECT col1, col2`) rather than `SELECT *`.
15- Prefer synchronous flows within `jdbi.inTransaction {}`; avoid mixing suspend calls inside transactions.
16- Enforce batching (`@SqlBatch`, `@BatchChunkSize(2000)`) for bulk inserts/updates and chunk large `WHERE IN` arguments (SQL Server limit ~2200 params).
17- Validate pagination on read-heavy endpoints; flag unbounded fetches or N+1 loops.
18- Ensure blocking annotations (`@BlockingClass`, `@BlockingCall`) exist for DAL/Repo classes and consumers.
19- Confirm master vs. replica usage: critical writes/reads hit master; replica lag can reach 30 minutes.
20
21## Indexing & Performance
22- Request evidence of supporting indexes for new predicates, sort columns, and pagination keys.
23- Encourage use of table aliases/prefixes in JOINs to maintain clarity.
24- For new queries, verify index coverage and that `updated_at` timestamps update alongside data mutations.
25- Demand UTC handling for timestamps and rely on the database (`CURRENT_TIMESTAMP`) to set them.
26
27## Schema & DDL Expectations
28- Ensure PRs document DDL changes and keep migrations incremental/backward compatible.
29- Require `created_at`/`updated_at` columns, primary keys, and consider unique constraints where appropriate.
30- Prefer `NVARCHAR` over `VARCHAR`; align column nullability with Kotlin model nullability.
31- Advocate for foreign keys to avoid orphaned rows and use online/resumable index operations.
32
33## Scripts & Data Ops
34- Scripts should live in dedicated packages, run transactional logic in managers/DAL, and treat CLI entrypoints as thin wrappers.
35- Verify bulk update scripts log progress, support `mockRun`, wrap per-row mutations in try/catch, and notify stakeholders before production runs.
36
37## Related Stores
38- Cosmos DB batches should rely on `BulkExecutor`; discourage ad-hoc parallel loops.
39- RedisCache2 usage must reuse clients, keep TTLs under 6 hours, and avoid local caches that cannot be invalidated.
40
41## Tooling Tips
42- `Grep` for `SELECT *`, `@SqlBatch`, `inTransaction`, or `AsyncResponse` inside DAO code to ensure patterns align.
43- `Read` migration files and DAL implementations to confirm pagination, batching, and index handling.
44- `Glob` `*Dao.kt`, `*Repository.kt`, `*Script.kt` to review related data access or scripting changes together.