Data model and migrations
Add a column or a table the way the project does it, and understand why the migration count is a release fact.
The schema is versioned with numbered SQL migrations embedded in the server binary. On start the server applies every migration that is not yet recorded in schema_migrations, in filename order, each in its own transaction. That is why /api/version reports schema_migrations: it is a compatibility check you can read before an upgrade or a rollback.
The core tables
- site, functional_location, equipment: the physical hierarchy.
- work_order: type, priority, status, cost buckets, failure data, AI classification.
- pm_schedule: RFC 5545 time trigger, meter trigger and next due values.
- job_plan: tasks, materials, tools and a blocking checklist.
- meter_reading and meter groups: the condition data.
- audit_log: the append-only record of actions.
Adding a migration
- Create the next numbered file in cmd/server/migrations.
- Write forward-only SQL. If a change is destructive, ship a backfill first.
- Remember that the server applies the file automatically at start: there is no separate migrate command to run.
- Add the indexes the query will need, not the indexes you might need later.
- Run the server against a copy of production data and time the migration.
- Update the schema documentation if the table is part of the public model.
The JSON rule
Do not let nullable database types leak into JSON. Scan into pointer types or explicit null wrappers so the client receives null or a value, never an object that looks like a value but is not. A leaking null type is a client crash waiting for the right record.
A published migration is immutable. Fix a mistake with the next migration, never by editing the applied file.
Add a nullable column with a default, run the server, and confirm the migration count increased by one and the API returns null rather than a wrapper object.
Terms
- Migration
- A numbered, forward-only SQL file applied once per database.
- Backfill
- Filling existing rows with a value before a constraint or a new column becomes mandatory.
OSMAINT Academy · Developer · Data model and migrations · osmaint.org/academy/