7. Run database migrations¶
A fresh D1 database is empty — no tables. "Migrations" are small SQL files (in the migrations/ folder) that build the tables and load starter data. You run them once to set up the live database, and again whenever new migration files are added.
Two databases: local and remote
- local — a SQLite file on your computer under
.wrangler/, used when developing. Apply with--local. - remote — the real D1 database in your Cloudflare account that the live site uses. Apply with
--remote.
They're separate. Applying to one does not affect the other.
Apply migrations to the live (remote) database¶
Make sure you've run npx wrangler login (step 2) and that wrangler.toml has the correct database_id (step 3). Then, in the oncall-scheduler folder:
This runs wrangler d1 migrations apply oncall_db --remote. Wrangler lists the migrations it's about to apply and asks you to confirm — type y and press Enter.
You should see each migration applied in order, ending with something like 0010_snapshot_created_by.sql.
What the migrations include
Besides the table structure, the starter migrations seed the holidays and an initial roster so the app isn't empty on day one. You can edit people and holidays in the app afterwards.
Apply migrations locally (for development)¶
If you'll run the app on your machine:
This builds the local database file. It needs no Cloudflare login.
When new migrations are added later¶
Any time the code gains a new file in migrations/ (for example a future 0011_*.sql), re-run the same commands to apply it:
Applying is safe to repeat — Wrangler tracks which migrations already ran and only applies new ones.
Order matters; don't hand-edit applied migrations
Migrations run in numeric order and are recorded once applied. Never rename or change a migration that's already been applied to the remote database — add a new numbered file instead. (Changing an already-applied file can put the local and remote databases out of sync.)
Confirm¶
After the remote migration, open the live site (the *.pages.dev URL or your custom domain). The roster, holidays, and schedule screens should load with the seeded data instead of errors.