Skip to content

Commit be7626d

Browse files
committed
more details on db maintenance workflow
1 parent 5f58fc6 commit be7626d

1 file changed

Lines changed: 65 additions & 17 deletions

File tree

db/README.md

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,78 @@
1-
# schema and migrations
1+
# Database schema maintenance
22

3+
### Small Schema Changes: Migrations
34

4-
## Current schema
5+
Use migrations for targeted changes (adding a column, changing a function, adding an index):
56

6-
FWA tables/functions/etc are defined in `schema.sql`. To load the schema:
7+
```bash
8+
# 1. Create a migration file
9+
cd db
10+
./create_migration.sh add_gradient_to_streams
711

8-
psql $DATABASE_URL -f schema.sql
12+
# 2. Edit the generated file
13+
# db/migrations/202604171030__add_gradient_to_streams.sql
14+
# Write your SQL between BEGIN and COMMIT
915

10-
Note that this is not required for the dockerized database.
16+
# 3. Preview against local DB
17+
./migrate.sh --dry-run
1118

12-
## Migrations
19+
# 4. Apply locally and test
20+
./migrate.sh
1321

14-
To update the database, create a migration file or files.
22+
# 5. Commit the migration file
23+
git add db/migrations/202604171030__add_gradient_to_streams.sql
24+
git commit -m "add gradient column to streams table"
1525

16-
$ ./create_migration.sh fix_foo
17-
Created migrations/202604170407__fix_foo.sql
26+
# 6. Open a PR — CI runs test-migration.yml automatically
27+
# (triggers on migrations/*.sql changes targeting main)
28+
```
1829

19-
After adding the sql to the migration files, consider checking the latest migration already applied to the db and listing all the pending migrations:
30+
Migrations accumulate in `db/migrations/` and are applied in order on any database that is behind.
2031

21-
$ ./migrate.sh --dry-run
22-
Current database version: 202604170339
23-
Pending migrations: 1
2432

25-
Dry run — the following migrations would be applied:
26-
202604170407__fix_foo.sql
33+
### Major Changes: Regenerating schema.sql
2734

28-
When ready, run the new migration(s).
35+
`schema.sql` is a `pg_dump` snapshot of the full database.
36+
This is the authoritative definition for fresh installs.
37+
It is **not** auto-updated by migrations — it must be manually regenerated when significant structural changes accumulate.
38+
When this has occureed, regenerate `schema.sql` from a clean, fully-loaded database.
2939

30-
./migrate.sh
40+
**Recommended workflow:**
41+
42+
```bash
43+
# Dump a new schema.sql
44+
# Use --schema-only to exclude data, --no-owner / --no-privileges
45+
# to keep it portable
46+
pg_dump \
47+
--schema-only \
48+
--no-owner \
49+
--no-privileges \
50+
--no-tablespaces \
51+
"$DATABASE_URL" \
52+
-f db/schema.sql
53+
54+
# 5. Inspect the diff to confirm only intended changes appear
55+
git diff db/schema.sql
56+
57+
# 6. Commit schema.sql alongside any remaining migration files
58+
git add db/schema.sql
59+
git commit -m "regenerate schema.sql after v0.8.0 migrations"
60+
61+
# 7. Tag the release
62+
git tag v0.8.0
63+
git push origin main --tags
64+
# CI builds and pushes updated Docker images tagged v0.8.0 and :main
65+
```
66+
67+
**When to regenerate schema.sql:**
68+
69+
- Before a versioned release (e.g., v0.8.0)
70+
- After adding new extensions or schemas
71+
- After large-scale function rewrites where the diff becomes hard to review
72+
- Periodically, to prevent the migration chain from growing unwieldy for new installs
73+
74+
**What NOT to do:**
75+
76+
- Do not hand-edit `schema.sql` — always regenerate it from a live dump
77+
- Do not delete applied migration files — they are the audit trail and may be needed to bring old databases forward
78+
- Do not skip the `db_version` table update step — always run changes to the db via the migrate script

0 commit comments

Comments
 (0)