Skip to content

Commit 6c46ecb

Browse files
committed
docs: Add comprehensive guide for auto-compilation flow development and deployment strategies
1 parent ebfdee0 commit 6c46ecb

File tree

5 files changed

+2895
-0
lines changed

5 files changed

+2895
-0
lines changed

COMPILE_WORKER.md

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
# Auto-Compilation: Simplified Flow Development
2+
3+
> **Implementation**: This feature is being built in two phases:
4+
>
5+
> - **Phase 1 (MVP)**: Core auto-compilation with conservative behavior
6+
> - **Phase 2 (Enhancement)**: Smart updates that preserve data when possible
7+
8+
---
9+
10+
## 🚀 Local Development - No Manual Steps
11+
12+
### 1. Start Edge Runtime
13+
14+
```bash
15+
supabase functions serve
16+
```
17+
18+
### 2. Start Worker (Triggers Auto-Compilation)
19+
20+
```bash
21+
curl http://localhost:54321/functions/v1/my-worker
22+
```
23+
24+
- Worker detects local environment ([see how](#environment-detection))
25+
- Auto-creates flow in database
26+
- ✅ Ready to process tasks immediately
27+
28+
### 3. Edit Flow Code
29+
30+
Make changes to your flow definition file.
31+
32+
### 4. Restart Worker (After Code Changes)
33+
34+
```bash
35+
# Kill `functions serve` (Ctrl+C), then restart
36+
supabase functions serve
37+
```
38+
39+
```bash
40+
# Start worker with fresh code
41+
curl http://localhost:54321/functions/v1/my-worker
42+
```
43+
44+
- Worker auto-updates flow definition
45+
- ✅ Ready to test immediately
46+
47+
**What happens automatically:**
48+
49+
- Worker detects local environment
50+
- Compares flow code with database definition
51+
- Updates database to match your code
52+
- **Phase 1**: Always drops and recreates (fresh state guaranteed)
53+
- **Phase 2**: Preserves test data when only runtime options change
54+
55+
**No `pgflow compile` commands needed in development! 🎉**
56+
57+
---
58+
59+
## 🔍 Environment Detection
60+
61+
Workers automatically detect whether they're running locally or in production.
62+
63+
```typescript
64+
// Check for Supabase-specific environment variables
65+
const isLocal = !Boolean(
66+
Deno.env.get('DENO_DEPLOYMENT_ID') || Deno.env.get('SB_REGION')
67+
);
68+
```
69+
70+
**How it works:**
71+
72+
- These environment variables are automatically set by Supabase on hosted deployments
73+
- When running `supabase functions serve` locally, these variables are absent
74+
- Additional DB URL validation warns about unexpected configurations
75+
76+
**Result:**
77+
78+
- **Local**: Auto-compilation enabled - worker creates/updates flows automatically
79+
- **Production**: Conservative mode - requires explicit migrations for existing flows
80+
81+
---
82+
83+
## 🏭 Production Deployment
84+
85+
### Phase 1: Conservative Approach
86+
87+
**Behavior**:
88+
89+
- **New flows**: Auto-created on first deployment ✅
90+
- **Existing flows**: Worker fails, requires migration ❌
91+
92+
#### Deploy New Flow
93+
94+
```bash
95+
# 1. Deploy worker code
96+
supabase functions deploy my-worker
97+
98+
# 2. First request auto-creates flow
99+
curl https://your-project.supabase.co/functions/v1/my-worker
100+
# ✅ Ready to handle requests
101+
```
102+
103+
#### Update Existing Flow
104+
105+
```bash
106+
# 1. Generate migration
107+
pgflow compile flows/my-flow.ts
108+
109+
# 2. Deploy migration
110+
supabase db push
111+
112+
# 3. Deploy worker code
113+
supabase functions deploy my-worker
114+
# ✅ Worker verifies flow matches
115+
```
116+
117+
**Phase 1 Benefits**:
118+
119+
- ✅ Explicit control over production changes
120+
- ✅ Clear audit trail (migrations)
121+
- ✅ Fail-fast protection
122+
- ✅ Simple, predictable behavior
123+
124+
**Phase 1 Trade-off**:
125+
126+
- ⚠️ Even option-only changes require migration
127+
128+
---
129+
130+
### Phase 2: Smart Updates (Enhancement)
131+
132+
**Additional Behavior**:
133+
134+
- **Existing flows with matching structure**: Auto-updates runtime options ✅
135+
- **Existing flows with structure changes**: Still requires migration ❌
136+
137+
#### Update Runtime Options (No Migration Needed!)
138+
139+
```bash
140+
# 1. Change timeout/maxAttempts in code
141+
# 2. Deploy worker
142+
supabase functions deploy my-worker
143+
# ✅ Options updated automatically (no migration!)
144+
```
145+
146+
#### Update Flow Structure (Migration Required)
147+
148+
```bash
149+
# 1. Add new step or change dependencies
150+
# 2. Generate migration
151+
pgflow compile flows/my-flow.ts
152+
153+
# 3. Deploy migration + worker
154+
supabase db push
155+
supabase functions deploy my-worker
156+
```
157+
158+
**Phase 2 Benefits**:
159+
160+
- ✅ Faster deploys for option changes
161+
- ✅ Still safe (structure changes require migration)
162+
- ✅ Backward compatible with Phase 1
163+
164+
**Phase 2 Addition: Strict Mode** _(Optional)_
165+
166+
```bash
167+
# Require migrations even for new flows
168+
PGFLOW_REQUIRE_MIGRATIONS=true
169+
```
170+
171+
---
172+
173+
## ⚙️ Manual Compilation Command
174+
175+
Generate migration files for explicit deployment control.
176+
177+
### Basic Usage
178+
179+
```bash
180+
pgflow compile flows/my-flow.ts
181+
```
182+
183+
- Infers worker: `my-flow-worker` (basename + "-worker")
184+
- Checks staleness: compares file mtime with worker startup time
185+
- Returns compiled SQL if worker is fresh
186+
187+
### Custom Worker Name
188+
189+
```bash
190+
pgflow compile flows/my-flow.ts --worker custom-worker
191+
```
192+
193+
- Use when worker doesn't follow naming convention
194+
- Useful for horizontal scaling or specialized workers
195+
196+
**Success output:**
197+
198+
```
199+
✓ Compiled successfully: my_flow → SQL migration ready
200+
✓ Created: supabase/migrations/20250108120000_create_my_flow.sql
201+
```
202+
203+
**If worker needs restart:**
204+
205+
```
206+
Error: Worker code changed since startup
207+
Action: Restart worker and retry
208+
```
209+
210+
---
211+
212+
## ⚠️ Edge Cases & Solutions
213+
214+
### Multiple Worker Instances (Horizontal Scaling) ✅
215+
216+
```bash
217+
# All instances handle the same flow
218+
my-flow-worker-1, my-flow-worker-2, my-flow-worker-3
219+
```
220+
221+
-**Phase 1**: First instance creates, others fail gracefully and retry
222+
-**Phase 2**: First instance creates, others detect and continue
223+
- ✅ Advisory locks prevent race conditions
224+
225+
### Stale Worker (Code Changes) ❌
226+
227+
**Problem:** Worker started before code changes.
228+
229+
#### Solution: Restart Worker
230+
231+
```bash
232+
# Kill `functions serve` (Ctrl+C), then restart
233+
supabase functions serve
234+
```
235+
236+
```bash
237+
# Start worker with fresh code
238+
curl http://localhost:54321/functions/v1/my-worker
239+
```
240+
241+
**Detection:** CLI compares file modification time with worker startup time.
242+
243+
---
244+
245+
### Flow Definition Changes
246+
247+
#### Local Development ✅
248+
249+
**Phase 1**:
250+
251+
- ✅ Always drops and recreates
252+
- ✅ Guaranteed fresh state
253+
- ⚠️ Test data lost on every restart
254+
255+
**Phase 2**:
256+
257+
- ✅ Preserves test data when only options change
258+
- ✅ Only drops when structure changes (new steps, changed dependencies)
259+
- ✅ Better developer experience
260+
261+
---
262+
263+
#### Production Deployment
264+
265+
**Phase 1 - Any Change**:
266+
267+
```
268+
Error: Flow 'my_flow' already exists
269+
Action: Deploy migration first or use different slug
270+
```
271+
272+
Must generate and deploy migration for any change.
273+
274+
**Phase 2 - Structure Change**:
275+
276+
```
277+
Error: Flow 'my_flow' structure mismatch
278+
- Step 'process' dependencies changed: ['fetch'] → ['fetch', 'validate']
279+
- New step 'validate' added
280+
Action: Deploy migration first (pgflow compile flows/my-flow.ts)
281+
```
282+
283+
Structure changes still require migration (safe!).
284+
285+
**Phase 2 - Option Change**:
286+
287+
```
288+
✓ Runtime options updated for flow 'my_flow'
289+
- Step 'process': timeout 30s → 60s
290+
```
291+
292+
Option changes work automatically (convenient!).
293+
294+
---
295+
296+
## 📋 Behavior Summary
297+
298+
### What Gets Auto-Compiled
299+
300+
| Change Type | Local (Phase 1) | Local (Phase 2) | Production (Phase 1) | Production (Phase 2) |
301+
| -------------------- | ---------------- | ------------------ | -------------------- | -------------------- |
302+
| **New flow** | ✅ Auto-create | ✅ Auto-create | ✅ Auto-create | ✅ Auto-create |
303+
| **Runtime options** | ✅ Drop+recreate |**Update only** | ❌ Require migration |**Update only** |
304+
| **Structure change** | ✅ Drop+recreate | ✅ Drop+recreate | ❌ Require migration | ❌ Require migration |
305+
306+
**Key Insight**: Phase 2 adds smart updates that preserve data and allow option changes without migrations.
307+
308+
---
309+
310+
## 🎯 When to Use Each Phase
311+
312+
### Ship Phase 1 When:
313+
314+
- ✅ You want auto-compilation ASAP
315+
- ✅ You're okay with explicit migrations in production
316+
- ✅ You don't mind losing local test data on restarts
317+
- ✅ You want simple, predictable behavior
318+
319+
### Upgrade to Phase 2 When:
320+
321+
- ✅ Phase 1 is stable in production
322+
- ✅ You want better local dev experience (preserve test data)
323+
- ✅ You want faster production deploys (option changes without migrations)
324+
- ✅ You've validated Phase 1 works for your workflows
325+
326+
---
327+
328+
## 🔗 See Also
329+
330+
- **[PLAN_phase1.md](./PLAN_phase1.md)** - Detailed Phase 1 implementation plan
331+
- **[PLAN_phase2.md](./PLAN_phase2.md)** - Detailed Phase 2 enhancement plan

0 commit comments

Comments
 (0)