|
1 | | -# schema and migrations |
| 1 | +# Database schema maintenance |
2 | 2 |
|
| 3 | +### Small Schema Changes: Migrations |
3 | 4 |
|
4 | | -## Current schema |
| 5 | +Use migrations for targeted changes (adding a column, changing a function, adding an index): |
5 | 6 |
|
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 |
7 | 11 |
|
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 |
9 | 15 |
|
10 | | -Note that this is not required for the dockerized database. |
| 16 | +# 3. Preview against local DB |
| 17 | +./migrate.sh --dry-run |
11 | 18 |
|
12 | | -## Migrations |
| 19 | +# 4. Apply locally and test |
| 20 | +./migrate.sh |
13 | 21 |
|
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" |
15 | 25 |
|
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 | +``` |
18 | 29 |
|
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. |
20 | 31 |
|
21 | | - $ ./migrate.sh --dry-run |
22 | | - Current database version: 202604170339 |
23 | | - Pending migrations: 1 |
24 | 32 |
|
25 | | - Dry run — the following migrations would be applied: |
26 | | - 202604170407__fix_foo.sql |
| 33 | +### Major Changes: Regenerating schema.sql |
27 | 34 |
|
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. |
29 | 39 |
|
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