files-workflow
The two-phase workflow: generate migration files from models with one
command, then apply them later with a separate binary that never imports
your models at all.
main.go is nothing but migratecmd.Models -- the same shape as
examples/basic. What is different is how it is used here: instead of
running migrate directly against a database, --out writes the rendered
migrations to disk, and the bun-migrator binary from cmd/bun-migrator
applies that directory later, possibly from a different machine, a deploy
pipeline, or anyone who never checked out your Go module at all.
The migrations/ directory in this folder is checked in, generated by the
--out command below, so you can see exactly what that step produces
without running anything yourself.
Run it
Generate the files (this is how migrations/ here was made):
go run ./examples/files-workflow --out ./examples/files-workflow/migrations
Install the binary that applies them:
go install github.com/jalavosus/bun-migrator/cmd/bun-migrator@latest
Apply, check status, and roll back, against a real Postgres 14+ database:
bun-migrator --dsn "$DATABASE_URL" apply ./examples/files-workflow/migrations
bun-migrator --dsn "$DATABASE_URL" status ./examples/files-workflow/migrations
bun-migrator --dsn "$DATABASE_URL" rollback ./examples/files-workflow/migrations
If a run is interrupted mid-migration, a later apply fails at the lock
step; clear it with:
bun-migrator --dsn "$DATABASE_URL" unlock
--dsn can be replaced with DATABASE_URL or the PG_* environment
variables instead, same as everywhere else in this module.
What you should see
--out writes three migrations (task_status, auto_update_updated_at,
tasks) as .tx.up.sql / .tx.down.sql pairs. apply prints
migrated to group #1: followed by one ADDED ... line per migration;
status lists each as applied (group 1); rollback prints
rolled back group #1: with one DROPPED ... line per migration, in
reverse order.