Database Migration Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Database Migration runs in O(n) time and O(n) space, in Go as in every other language.
- ✓No migration support in the standard library, but embed.FS in the standard library is what lets every Go migration tool ship its SQL inside the binary.
- ✓Embed the migrations directory with go:embed and run them from a dedicated command rather than from the server binary at startup, so a rolling deploy does not have every pod attempting the same change.
- ✓The Go trap: golang-migrate marks the schema DIRTY when a migration fails and then refuses to run anything until a human forces the version.
- ✓MySQL 5.7 rebuilt the table for many ALTERs and blocked writes for the duration; 8.0 added instant ADD COLUMN and atomic DDL.
What Database Migration actually is
Database Migration in Go: what you actually reach for
Go implementation
//go:embed migrations/*.sql
var migrationsFS embed.FS
func Migrate(ctx context.Context, dsn string) error {
src, err := iofs.New(migrationsFS, "migrations")
if err != nil {
return err
}
m, err := migrate.NewWithSourceInstance("iofs", src, dsn)
if err != nil {
return err
}
// a failure marks the schema DIRTY: with non-transactional DDL that flag
// is TRUE, so inspect the schema before ever calling m.Force(version)
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
return nil
}
// -- +goose Up
// ALTER TABLE users ADD COLUMN last_seen_at timestamptz;
// -- +goose Down
// ALTER TABLE users DROP COLUMN last_seen_at;Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Database Migration?
No migration support in the standard library, but embed.FS in the standard library is what lets every Go migration tool ship its SQL inside the binary. golang-migrate is the most used, goose annotates plain SQL files with -- +goose Up and -- +goose Down markers, and Atlas takes a declarative approach closer to Terraform.
What is the most common Go mistake when implementing Database Migration?
golang-migrate marks the schema DIRTY when a migration fails and then refuses to run anything until a human forces the version. That is a reasonable design against a database with transactional DDL, where the failed migration rolled back cleanly. Against MySQL before 8.0, where DDL is not atomic, the schema is genuinely half-changed, so the dirty flag is telling the truth and forcing the version without inspecting the table is how a broken schema gets marked healthy.
How does Database Migration actually perform in Go?
MySQL 5.7 rebuilt the table for many ALTERs and blocked writes for the duration; 8.0 added instant ADD COLUMN and atomic DDL. On PostgreSQL the equivalent care is CONCURRENTLY for indexes and NOT VALID plus VALIDATE for constraints. Neither tool knows any of this, the lock profile is entirely on the SQL you wrote. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.