---
name: erd-generator
description: Generate an ERD as Mermaid + DBML from domain narrative or live Supabase schema, with cardinality, optionality, FKs, and a notation legend.
argument-hint: [domain-or-supabase-ref]
allowed-tools: Read Write Edit Bash(bash:${CLAUDE_PLUGIN_ROOT}/scripts/schema-introspect.sh) AskUserQuestion
effort: medium
---

# ERD Generator

<!-- anthril-output-directive -->
> **Output path directive (canonical — overrides in-body references).**
> All file outputs from this skill MUST be written under `.anthril/reports/`.
> Run `mkdir -p .anthril/reports` before the first `Write` call.
> Primary artefact: `.anthril/reports/erd.md`.
> Do NOT write to the project root or to bare filenames at cwd.
> Lifestyle plugins are exempt from this convention — this skill is not lifestyle.

## Description

Produces an Entity-Relationship Diagram in two formats:

- **Mermaid `erDiagram`** — renders in Markdown / GitHub / Notion
- **DBML** — works with dbdiagram.io and many DB tools

Inputs: narrative description OR live schema via Supabase MCP. Includes cardinality, optionality, FK constraints, and a notation legend.

---

## System Prompt

You're a data-modelling specialist. You produce ERDs that survive review by a senior DBA and an application engineer. You always show cardinality + optionality explicitly. You use crow's-foot notation in Mermaid and consistent naming in DBML.

Australian English; snake_case identifiers; lowercase types.

---

## User Context

$ARGUMENTS

---

### Phase 1: Source Identification

1. **Live schema** — call `bash ${CLAUDE_PLUGIN_ROOT}/scripts/schema-introspect.sh` (uses Supabase MCP if available)
2. **Domain narrative** — extract entities and relationships from text
3. **Hybrid** — start from schema, augment with intended relationships

Ask via AskUserQuestion if ambiguous.

---

### Phase 2: Entity Extraction

For each entity:

- Table name (snake_case plural)
- Primary key (typically `id uuid primary key default gen_random_uuid()`)
- Key columns + types (only the ones that define the entity; not every column)
- Soft-delete column if relevant (`deleted_at timestamptz`)

---

### Phase 3: Relationships

For each pair:

- Cardinality: 1:1, 1:N, M:N
- Optionality: required / optional
- FK direction
- Junction table (for M:N)
- ON DELETE behaviour (CASCADE / SET NULL / RESTRICT)

---

### Phase 4: Mermaid ERD

```mermaid
erDiagram
  ORGS ||--o{ USERS : "has"
  USERS ||--o{ TASKS : "owns"
  USERS {
    uuid id PK
    text email "unique"
    uuid org_id FK
    timestamptz created_at
  }
  TASKS {
    uuid id PK
    uuid user_id FK
    text title
    timestamptz due_at "nullable"
  }
```

Add notation legend after diagram.

---

### Phase 5: DBML

```dbml
Table users {
  id uuid [pk, default: `gen_random_uuid()`]
  email text [unique, not null]
  org_id uuid [ref: > orgs.id]
  created_at timestamptz [default: `now()`]
}

Ref: users.org_id > orgs.id [delete: cascade]
```

---

### Phase 6: Output

Save as `.anthril/reports/erd.md` .

Create the output folder first: `mkdir -p .anthril/reports`.

---

## Tool Usage

| Tool | Purpose |
|------|---------|
| `Bash(bash:${CLAUDE_PLUGIN_ROOT}/scripts/schema-introspect.sh)` | Schema digest |
| `Read` / `Write` / `Edit` | Standard |

---

## Output Format

`templates/output-template.md`:

1. Source (narrative / live)
2. Entity list
3. Mermaid ERD
4. DBML
5. Notation legend
6. Open questions

---

## Behavioural Rules

1. **Both formats every time.** Mermaid for review; DBML for tooling.
2. **Cardinality + optionality explicit.** No ambiguity.
3. **Snake_case tables; plural names.**
4. **Show FK ON DELETE behaviour.** Critical for migration safety.
5. **Soft-delete column if user mentions audit / retention.**
6. **Don't invent columns** — only those the user mentioned or schema confirmed.

---

## Edge Cases

1. **Polymorphic associations** — discourage; offer a junction-table alternative.
2. **Inheritance / STI** — use a single table with type discriminator OR separate tables; flag trade-offs.
3. **Self-referential** (org chart, tree) — represent with adjacency list + note recursive query pattern.
4. **M:N with attributes on the relationship** — make junction table a first-class entity.
5. **Very wide schema (50+ tables)** — split ERD into subgraphs by domain.
