Skip to content

Commit 2882938

Browse files
committed
docs: Add migration best practice for modifying functions/procedures
1 parent 6684d00 commit 2882938

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

AGENTS.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ cd cli && ./bin/statbus migrate up # Apply migration
5858
cd cli && ./bin/statbus migrate down # ⚠️ Rollback (destructive)
5959
```
6060

61+
**Migration Best Practice for Modifying Existing Functions/Procedures:**
62+
63+
When modifying an existing database function or procedure, **always dump the current definition first** rather than rewriting from scratch:
64+
65+
```bash
66+
# Dump function definition to use as base for both up and down migrations
67+
echo "\sf schema.function_name" | ./devops/manage-statbus.sh psql > tmp/function_def.sql
68+
69+
# For procedures
70+
echo "\sf schema.procedure_name" | ./devops/manage-statbus.sh psql > tmp/procedure_def.sql
71+
```
72+
73+
Then:
74+
1. Copy the dumped definition into the **down migration** (this restores the original)
75+
2. Copy it into the **up migration** and make only the necessary modifications
76+
3. Add `CREATE OR REPLACE` prefix and wrap in `BEGIN;`/`END;`
77+
78+
This approach:
79+
- Preserves exact current behavior in the down migration
80+
- Ensures surgical changes rather than accidental rewrites
81+
- Reduces risk of introducing bugs from manual recreation
82+
- Maintains all edge cases, exception handling, and comments
83+
84+
**CRITICAL**: Never rewrite large functions/procedures from scratch. The `\sf` dump ensures you're modifying the *actual current code*, not an outdated or incomplete version. For large procedures (100+ lines), focus only on the specific lines that need changing.
85+
6186
### Next.js Application (from app/ directory)
6287
```bash
6388
cd app && pnpm run dev # Development server with Turbopack

0 commit comments

Comments
 (0)