Automatically applying D1 migrations when deploying pages
I've found a few topics on this, but they've sadly gone unanswered. What is the expected route for applying migrations to a bound D1 database when deploying?
I'd sort of expected this to 'just work' via the built in git integration (since almost everything else does) and my config is vanilla, but that doesn't seem to be the case.
It looks I need to create a CI/CD flow, avoiding the use of the built in git connection entirely and applying there. Though I can't see any docs pointing in that direction.
Is that correct, or am I missing something? All the docs I can find describe applying migrations manually, but that's not appropriate after the initial playing around stage. It would be an absolute dream if I could just use CloudFlare's native deploys.
4 Replies
Until I get a better way to do it, I'm sharing a gist of my GitHub actions flow to perform the deploy. It's working for me, even if I'd rather not having it in GitHub Actions. https://gist.github.com/manterfield/80bc8dff4e609eeeca9679360d358d98 hopeful it saves others a bit of time.
Gist
GitHub Actions workflow for deploying CloudFlare pages with automat...
GitHub Actions workflow for deploying CloudFlare pages with automatic D1 migrations - Uses SvelteKit and pnpm, but should be useful for others. - deploy.yaml
You could also just have the build process run the migrations?
@HardlyWorkin' It wasn't clear to me that that was possible. I initially tried it by adding the command to the build script in package.json, which failed due to missing the API key (fair enough, I could get one and add one).
Only thing that stopped me doing that next step was after some googling I found people saying it doesn't work at all via the git integration builds even with API keys, so I didn't bother. I can't find the threads on it now, but I think they were in open issues on one of the cloudflare repos.
it does work