I recently migrated an internal tool from Cloudflare Worker to self-hosting it on fly.io. The main reason driving this move is that we had ran into some weird issues with using fetch calls on the server-side. (Like custom domain would somehow cause it to not work so we had to redirect users to .pages.dev domain first, and POST for some reason does not work in our Rust-based wasm code.)
We were using the D1 database which is a SQLite wrapped in some easy to use APIs (both in JS and over HTTP, however, no DB GUI tools have support for these APIs natively yet so management is a bit annoying) so we need to move them. I decided to also just use a sqlite DB but store it on fly.io’s LiteFS. Importing data is easy, we just need to export the data as SQL, put it in a sqlite3 db and put that file into LiteFS. However, now we do not have a CLI to do migrations anymore. Therefore, I wrote a simple script to facilitate this. The code can be found at the bottom of the post.
Usage
To use the script, call it by: Usage: migration.sh <dbpath> <list|up>.
The list command list out all migrations and if they’re applied, and up runs the migrations. We just use the d1_migration table that Cloudflare D1 created, if you do not have it in your DB, you can create one via:
Use it with LiteFS
LiteFS provides a mount command that took care of setting itself up and also running your application. You can take advantage of that mechanism to run this migration script, to do so, add this to your exec section:
The if-candidate option will ensure it runs on the primary node.