{"name":"@crewd/sql-style-guide","version":"1.0.0","type":"skill","description":"Conventions for readable SQL and safe, zero-downtime schema migrations.","integrity":"sha256-ab65f8db5f0bb212bec69b126fc550d0dd9682e4719a50fb45125ab3a0d2b0dd","tarball_url":"https://localhost:3000/api/v1/tarballs/crewd/sql-style-guide/1.0.0","tarballUrl":"https://localhost:3000/api/v1/tarballs/crewd/sql-style-guide/1.0.0","size_bytes":316,"hosts":["claude-code","cursor","gemini-cli"],"manifest":{"name":"@crewd/sql-style-guide","type":"skill","entry":"SKILL.md","hosts":["claude-code","cursor","gemini-cli"],"schema":"https://crewd.dev/schema/skill/v1","content":"---\nname: sql-style-guide\ndescription: \"Conventions for readable SQL and safe, zero-downtime schema migrations.\"\n---\n\n# Sql Style Guide\n\nUse this skill when writing queries, models or migrations.\n\n## Query style\n\n- Keywords UPPERCASE, identifiers `snake_case`, one clause per line.\n- Use CTEs (`WITH ...`) for multi-step logic; name them after what they contain.\n- Always qualify columns with table aliases in joins; aliases are meaningful (`orders o`, not `t1`).\n- Never `SELECT *` in application code or models.\n- State the grain of every result in a comment: `-- one row per user per day`.\n- Before joining, check cardinality to avoid fan-out double counting.\n\n```sql\nWITH active_users AS (\n    SELECT u.id, u.created_at\n    FROM users u\n    WHERE u.deleted_at IS NULL\n)\nSELECT\n    DATE_TRUNC('day', o.created_at) AS day,\n    COUNT(DISTINCT o.user_id)       AS buyers\nFROM orders o\nJOIN active_users au ON au.id = o.user_id\nGROUP BY 1\nORDER BY 1;\n```\n\n## Schema conventions\n\n- Tables plural (`orders`), PK `id`, FKs `<singular>_id`, timestamps `created_at` / `updated_at` as `timestamptz`.\n- Enforce `NOT NULL`, `UNIQUE`, `CHECK` and FK constraints in the database.\n- Index every FK and every column used in frequent `WHERE`/`ORDER BY`.\n\n## Zero-downtime migrations (expand → migrate → contract)\n\n1. **Expand**: add nullable column / new table; deploy code that writes both.\n2. **Backfill** in batches (e.g. 1–10k rows) to avoid long locks.\n3. **Switch reads** to the new column.\n4. **Contract**: add `NOT NULL`, drop old column in a later release.\n\nPostgres specifics: `CREATE INDEX CONCURRENTLY`; add FKs/checks `NOT VALID` then `VALIDATE CONSTRAINT`;\navoid `ALTER TYPE` rewrites on large tables; set `lock_timeout` in migrations.\n","version":"1.0.0","description":"Conventions for readable SQL and safe, zero-downtime schema migrations."},"scripts":[],"dependencies":[],"deprecated":null,"created_at":"2026-10-18T17:02:36.839215+00:00"}