Gom is a database migration tool, it uses embedding SQL migrations. Requires Go v1.16 or higher.
- Supports SQLite
- CLI
- Embedded migrations
- Plain SQL for writing schema migrations
- Incremental migration version using timestamps
- Run migrations inside a transaction
- Works in Go v1.18+
# Install the gom binary in your $GOPATH/bin directory
go install github.com/alexandermac/gom/cmd/gom
gom [FLAGS] DRIVER DBSTRING COMMAND
Flags:
--dir Migrations directory name (absolute or relative path)
--name A new migration file suffix
--verbose Prints debug information
Drivers:
sqlite3
Commands:
help Shows this help
version Prints app version
init Creates the migration directory with a sample migration file and the migrations table in the database
create Creates a new migration file
migrate Migrates the DB to the most recent version available
rollback Roll backs the version by 1
Examples:
gom --dir db_migrations sqlite3 ./foo.db init
gom --dir db_migrations --name create_users sqlite3 ./foo.db create
gom sqlite3 ./foo.db migrate
gom sqlite3 ./foo.db rollback
It's possible to embed sql files into binary and corresponding filesystem abstraction. Such migrations can be applied when the app starts.
package main
import (
"database/sql"
"embed"
"github.com/alexandermac/gom"
)
//go:embed my_migrations
var migrationsDir embed.FS
func main() {
// connect the database
log.Println("Migrating the database")
gom.SetBaseFS(migrationsDir)
gom.SetMigrationsDir("my_migrations")
if err := gom.Migrate(db); err != nil {
panic(err)
}
}
Sets a base file system to discover migrations. Call this function to pass an embedded migrations variable.
Sets the migrations directory.
Sets the logger. Must be compatible with gom.Logger interface.
Creates a new migration file. Used in CLI tool.
Migrates the DB to the most recent version available.
Roll backs the version by 1.
Licensed under the MIT license.
Alexander Mac