Skip to content

Commit fed17ec

Browse files
authored
feat: add Claude Code integration for end-users (#63)
### **User description** ## Summary Adds Claude Code integration for SRTD end-users: - **Skill** (`.claude/skills/srtd-cli/SKILL.md`): Focused on iterative SQL template development workflow with watch mode central. Written for LLM consumption, not human documentation. - **Rule** (`.claude/rules/srtd.md`): Path-scoped guardrails injected when editing template files (`**/migrations-templates/**/*.sql`) - **README section**: Simple copy commands for users to add integration to their projects ### Key Design Decisions The skill is intentionally minimal and workflow-focused: 1. Start with `srtd watch --json` in background 2. Monitor via `TaskOutput` for `templateApplied`/`templateError` events 3. Iterate on templates until working 4. Build only at the end Not a comprehensive CLI reference - just what an LLM needs to work effectively with SRTD. ## Test plan - [x] Verified skill loads via `/srtd-cli` - [x] Tested watch mode in background with `--json` flag - [x] Created templates, saw them apply, introduced errors, fixed them - [x] Confirmed rule format matches Claude Code spec (quoted glob patterns in YAML array) ___ ### **PR Type** Enhancement ___ ### **Description** - Add Claude Code skill and rule for SRTD template development - Rewrite skill focused on iterative watch-mode workflow - Provide copy commands in README for easy integration - Remove test template and build log from sample directory ___ ### Diagram Walkthrough ```mermaid flowchart LR A["Claude Code User"] -->|"activates skill"| B["SRTD Skill<br/>srtd-cli/SKILL.md"] B -->|"guides workflow"| C["Start watch --json<br/>in background"] C -->|"monitor events"| D["templateApplied/<br/>templateError"] D -->|"iterate templates"| E["Edit SQL files"] E -->|"when ready"| F["srtd build"] G["Path-scoped Rule<br/>.claude/rules/srtd.md"] -->|"provides inline<br/>guardrails"| E H["README section"] -->|"copy commands"| I["Install to project"] ``` <details><summary><h3>File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>SKILL.md</strong><dd><code>Rewrite skill for LLM-focused iterative workflow</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> .claude/skills/srtd-cli/SKILL.md <ul><li>Rewritten from comprehensive CLI reference to focused iterative <br>workflow guide<br> <li> Emphasizes starting <code>srtd watch --json</code> in background as first step<br> <li> Documents monitoring <code>TaskOutput</code> for <code>templateApplied</code>/<code>templateError</code> <br>events<br> <li> Simplified template patterns with idempotent examples for functions, <br>policies, triggers<br> <li> Removed command tables, keyboard shortcuts, and configuration details</ul> </details> </td> <td><a href="https://github.com/t1mmen/srtd/pull/63/files#diff-49eac8f54faaaaa96bf927aaa971dde73830bdbe11c706a1e9232974e98a879b">+41/-180</a></td> </tr> <tr> <td> <details> <summary><strong>srtd.md</strong><dd><code>Add path-scoped rules for template file editing</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> .claude/rules/srtd.md <ul><li>New file providing path-scoped guardrails for template files<br> <li> Covers required idempotent patterns for functions, views, policies, <br>triggers<br> <li> Documents template dependencies via <code>@depends-on</code> comments<br> <li> Explains WIP template behavior and testing workflow<br> <li> Lists common mistakes to avoid when editing templates</ul> </details> </td> <td><a href="https://github.com/t1mmen/srtd/pull/63/files#diff-d1bcafa2b5a3d62e5513e94324f50f7d29da4569365062075c6fbf4ab6259098">+48/-0</a>&nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Documentation</strong></td><td><table> <tr> <td> <details> <summary><strong>README.md</strong><dd><code>Add Claude Code integration setup instructions</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> README.md <ul><li>Add new "Claude Code Integration" section with copy commands<br> <li> Provide instructions to copy skill and rule from node_modules to <br>project<br> <li> Brief explanation of skill activation and rule guidance</ul> </details> </td> <td><a href="https://github.com/t1mmen/srtd/pull/63/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5">+12/-0</a>&nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Miscellaneous</strong></td><td><table> <tr> <td> <details> <summary><strong>.srtd.buildlog.json</strong><dd><code>Remove test build log file</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> supabase/migrations-templates/.srtd.buildlog.json - Removed test build log file from sample directory </details> </td> <td><a href="https://github.com/t1mmen/srtd/pull/63/files#diff-bda5df27adb782036a76d413c8b895dce469785af067dbd2f9d9cc9a56675cd7">+0/-11</a>&nbsp; &nbsp; </td> </tr> <tr> <td> <details> <summary><strong>test.sql</strong><dd><code>Remove test template file</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> supabase/migrations-templates/test.sql - Removed test template file from sample directory </details> </td> <td><a href="https://github.com/t1mmen/srtd/pull/63/files#diff-60e63cc33c071554e9f9c757d797badf3d0b8b2e6ff1771a7b4f711986efa964">+0/-6</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></td></tr></tbody></table> </details> ___
1 parent 085985b commit fed17ec

File tree

5 files changed

+51
-222
lines changed

5 files changed

+51
-222
lines changed

.claude/rules/srtd.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
paths:
3+
- "**/supabase/migrations-templates/**/*.sql"
4+
---
5+
6+
# SRTD Template
7+
8+
Load `Skill('srtd-cli')` for full context.
9+
10+
**Idempotency rules:**
11+
- Functions/Views: `CREATE OR REPLACE` (use `DROP` only when changing signature)
12+
- Policies: `DROP IF EXISTS` then `CREATE`
13+
- Triggers: Drop both trigger AND function first
14+
15+
**Quick reference:**
16+
- Dependencies: `-- @depends-on: other.sql` at top
17+
- WIP files: `.wip.sql` suffix (local only, won't build)
18+
- Watch mode: `srtd watch --json` in background

.claude/skills/srtd-cli/SKILL.md

Lines changed: 21 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,224 +1,40 @@
11
---
22
name: srtd-cli
3-
description: Guide users through SRTD CLI for live-reloading SQL templates into Supabase. Use when users ask about srtd commands, template writing, migrations, watch mode, or Supabase database object management.
3+
description: This skill should be used when the user mentions "srtd", "sql templates", "migrations-templates", "live reload sql", "supabase functions", when working with files in supabase/migrations-templates/, when .buildlog.json or srtd.config.json is detected, or when writing Postgres functions/views/triggers for Supabase.
44
---
55

6-
# SRTD CLI Usage Guide
6+
# SRTD - Iterative SQL Template Development
77

8-
SRTD makes database objects (functions, views, RLS policies, triggers) reviewable like regular code. Templates are the source of truth; they build to timestamped migrations.
9-
10-
## Quick Start
11-
12-
```bash
13-
# Install
14-
npm install -g @t1mmen/srtd # or: npx @t1mmen/srtd
15-
16-
# Initialize in Supabase project
17-
npx @t1mmen/srtd init
18-
19-
# Create first template
20-
cat > supabase/migrations-templates/hello.sql << 'EOF'
21-
DROP FUNCTION IF EXISTS public.hello;
22-
CREATE FUNCTION public.hello() RETURNS text AS $$
23-
BEGIN
24-
RETURN 'Hello from SRTD!';
25-
END;
26-
$$ LANGUAGE plpgsql;
27-
EOF
28-
29-
# Start live reload
30-
npx @t1mmen/srtd watch
31-
```
32-
33-
## Commands
34-
35-
| Command | Purpose |
36-
|---------|---------|
37-
| `srtd` | Interactive menu |
38-
| `srtd watch` | Live reload - edits apply instantly to local DB |
39-
| `srtd build` | Generate migration files from templates |
40-
| `srtd apply` | Apply templates to DB without generating migrations |
41-
| `srtd register` | Mark existing templates as already deployed |
42-
| `srtd promote` | Convert `.wip` template to buildable |
43-
| `srtd clear` | Reset build logs |
44-
45-
### Command Options
46-
47-
```bash
48-
srtd build --force # Rebuild all templates
49-
srtd build --bundle # All templates → single migration
50-
srtd apply --force # Reapply all templates
51-
srtd clear --local # Clear local state only
52-
srtd clear --shared # Clear shared state only
53-
srtd clear --reset # Full reset
54-
```
55-
56-
## Core Workflow
57-
58-
```
59-
1. CREATE template → supabase/migrations-templates/my_func.sql
60-
2. WATCH mode → srtd watch (auto-applies on save)
61-
3. TEST locally → Query your function in Supabase Studio
62-
4. BUILD migration → srtd build
63-
5. DEPLOY → supabase migration up
64-
```
65-
66-
## Template Patterns
67-
68-
### Functions (Most Common)
69-
70-
```sql
71-
-- Always DROP first to allow parameter changes
72-
DROP FUNCTION IF EXISTS public.calculate_total;
73-
CREATE FUNCTION public.calculate_total(order_id uuid)
74-
RETURNS numeric AS $$
75-
BEGIN
76-
RETURN (SELECT SUM(price * quantity) FROM order_items WHERE order_id = $1);
77-
END;
78-
$$ LANGUAGE plpgsql;
79-
```
80-
81-
### RLS Policies
82-
83-
```sql
84-
DROP POLICY IF EXISTS "users_own_data" ON profiles;
85-
CREATE POLICY "users_own_data" ON profiles
86-
USING (auth.uid() = user_id);
87-
```
88-
89-
### Views
90-
91-
```sql
92-
CREATE OR REPLACE VIEW active_users AS
93-
SELECT id, email, last_seen
94-
FROM users
95-
WHERE last_seen > NOW() - INTERVAL '30 days';
96-
```
97-
98-
### Triggers
99-
100-
```sql
101-
DROP TRIGGER IF EXISTS on_user_created ON auth.users;
102-
DROP FUNCTION IF EXISTS handle_new_user;
103-
104-
CREATE FUNCTION handle_new_user() RETURNS trigger AS $$
105-
BEGIN
106-
INSERT INTO public.profiles (id, email)
107-
VALUES (NEW.id, NEW.email);
108-
RETURN NEW;
109-
END;
110-
$$ LANGUAGE plpgsql SECURITY DEFINER;
111-
112-
CREATE TRIGGER on_user_created
113-
AFTER INSERT ON auth.users
114-
FOR EACH ROW EXECUTE FUNCTION handle_new_user();
115-
```
116-
117-
### Enum Extensions
118-
119-
```sql
120-
DO $$
121-
BEGIN
122-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'status') THEN
123-
CREATE TYPE status AS ENUM ('pending', 'active');
124-
END IF;
125-
ALTER TYPE status ADD VALUE IF NOT EXISTS 'archived';
126-
END $$;
127-
```
128-
129-
## WIP Templates
130-
131-
Use `.wip.sql` suffix for work-in-progress:
132-
133-
```bash
134-
# Create WIP template (applies locally, never builds to migration)
135-
touch supabase/migrations-templates/experimental.wip.sql
136-
137-
# When ready to deploy
138-
srtd promote experimental.wip.sql
139-
# Renames to experimental.sql, next build creates migration
140-
```
141-
142-
## Registering Existing Objects
143-
144-
When adopting SRTD on existing project:
8+
## Workflow
1459

10+
Start watch in background immediately:
14611
```bash
147-
# Create templates for existing functions
148-
# Then register them (prevents immediate migration generation)
149-
srtd register my_existing_function.sql another_function.sql
150-
151-
# Or interactively select which to register
152-
srtd register
153-
```
154-
155-
## Configuration
156-
157-
`srtd.config.json` (optional - sensible defaults):
158-
159-
```jsonc
160-
{
161-
"templateDir": "supabase/migrations-templates",
162-
"migrationDir": "supabase/migrations",
163-
"pgConnection": "postgresql://postgres:postgres@localhost:54322/postgres",
164-
"wipIndicator": ".wip",
165-
"migrationPrefix": "srtd",
166-
"wrapInTransaction": true
167-
}
12+
srtd watch --json
16813
```
14+
Use `run_in_background: true`. Monitor with `TaskOutput` for events: `templateApplied` (success), `templateError` (check `errorMessage`/`errorHint`).
16915

170-
## State Files
16+
## Templates
17117

172-
| File | Purpose | Git |
173-
|------|---------|-----|
174-
| `.buildlog.json` | Tracks built migrations | Commit |
175-
| `.buildlog.local.json` | Tracks local DB state | Gitignore |
18+
Location: `supabase/migrations-templates/*.sql`. Must be idempotent.
17619

177-
## What SRTD Is For
20+
**Idempotency patterns:**
21+
- Functions/Views: `CREATE OR REPLACE` (use `DROP` only when changing signature)
22+
- Policies: `DROP POLICY IF EXISTS` then `CREATE POLICY` (not replaceable)
23+
- Triggers: Drop BOTH trigger AND function first
17824

179-
**Use SRTD for** (idempotent, replaceable):
180-
- Functions
181-
- Views
182-
- RLS policies
183-
- Triggers
184-
- Roles/permissions
185-
- Enum extensions
25+
**Dependencies:** `-- @depends-on: other.sql` at top
18626

187-
**Use regular migrations for** (stateful):
188-
- Table structures
189-
- Indexes
190-
- Data modifications
191-
- Column alterations
27+
**WIP:** `.wip.sql` suffix → applies locally, never builds
19228

193-
## Troubleshooting
194-
195-
### "Could not find project root"
196-
Run from directory containing `supabase/` folder.
197-
198-
### Database connection failed
199-
Check `pgConnection` in config. Default Supabase local:
200-
```
201-
postgresql://postgres:postgres@localhost:54322/postgres
202-
```
203-
Ensure Supabase is running: `supabase start`
204-
205-
### Template not applying
206-
1. Check for SQL syntax errors (shown in watch output)
207-
2. Ensure template uses idempotent pattern (DROP IF EXISTS)
208-
3. Try `srtd apply --force`
29+
## Commands
20930

210-
### Build logs out of sync
21131
```bash
212-
srtd clear --reset # Full reset
213-
srtd watch # Reapply all templates
32+
srtd apply [--force] [--json] # Apply to local DB (use instead of watch for one-off)
33+
srtd build [--bundle] # Generate migration FILES only (does NOT apply to DB)
34+
srtd clear --reset # Reset state if confused
21435
```
21536

216-
### "Interactive mode requires TTY"
217-
Use flags instead: `srtd clear --local` not `srtd clear`
218-
219-
## Watch Mode Tips
37+
## State
22038

221-
- Press `u` to toggle history display
222-
- Press `q` to quit
223-
- Errors show immediately with file path and message
224-
- Multiple terminals: one for watch, one for testing
39+
- `.buildlog.json` → commit (tracks built migrations)
40+
- `.buildlog.local.json` → gitignored (local DB state)

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ Nested directories are created automatically.
233233
| `.buildlog.json` | What's been built to migrations | Commit |
234234
| `.buildlog.local.json` | What's applied to your local DB | Gitignore |
235235

236+
## Claude Code Integration
237+
238+
SRTD includes a skill and rule for [Claude Code](https://claude.ai/code) users:
239+
240+
```bash
241+
# Copy to your project's .claude directory
242+
cp -r node_modules/@t1mmen/srtd/.claude/skills/srtd-cli .claude/skills/
243+
cp -r node_modules/@t1mmen/srtd/.claude/rules/srtd.md .claude/rules/
244+
```
245+
246+
The skill activates when working with SQL templates. The rule provides inline guidance for template files.
247+
236248
## Contributing
237249

238250
Bug fixes, docs, and test coverage welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md).

supabase/migrations-templates/.srtd.buildlog.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

supabase/migrations-templates/test.sql

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)