This guide will walk you through setting up and using Gomiger for database migrations.
- Go 1.21 or later
- A supported database (MongoDB, PostgreSQL coming soon)
- Basic understanding of Go programming
go install github.com/ParteeLabs/gomiger/core/cmd/gomiger-init@latestgo get github.com/ParteeLabs/gomiger/coreFor MongoDB:
go get github.com/ParteeLabs/gomiger/mongomigermkdir my-app && cd my-app
go mod init example.com/my-appCreate gomiger.rc.yaml in your project root:
path: './migrations'
pkg_name: 'migrations'
schema_store: 'schema_migrations'gomiger-initThis creates:
migrations/
├── cli.mg.go
└── migrator.mg.go
Edit migrations/migrator.mg.go:
package migrations
import (
"github.com/ParteeLabs/gomiger/core"
"github.com/ParteeLabs/gomiger/mongomiger"
)
type Migrator struct {
*mongomiger.Mongomiger
Config *core.GomigerConfig
}
func NewMigrator(config *core.GomigerConfig) core.Gomiger {
return &Migrator{
Mongomiger: mongomiger.NewMongomiger(config),
Config: config,
}
}Create main.go:
package main
import (
"github.com/my-app/migrations"
)
func main() {
migrations.Run()
}go run main.go new create_users_tableThis creates migrations/TIMESTAMP_create_users_table.mg.go:
package migrations
import "context"
func (m *Migrator) Migration_TIMESTAMP_create_users_table_Up(ctx context.Context) error {
// Your migration up code here
return nil
}
func (m *Migrator) Migration_TIMESTAMP_create_users_table_Down(ctx context.Context) error {
// Your migration down code here
return nil
}
// AUTO GENERATED, DO NOT MODIFY!
func (m *Migrator) Migration_TIMESTAMP_create_users_table_Version() string {
return "TIMESTAMP"
}For MongoDB:
func (m *Migrator) Migration_TIMESTAMP_create_users_table_Up(ctx context.Context) error {
// Create users collection with validation
validator := bson.M{
"$jsonSchema": bson.M{
"bsonType": "object",
"required": []string{"email", "created_at"},
"properties": bson.M{
"email": bson.M{
"bsonType": "string",
"description": "must be a string and is required",
},
"name": bson.M{
"bsonType": "string",
"description": "user's full name",
},
"created_at": bson.M{
"bsonType": "date",
"description": "must be a date and is required",
},
},
},
}
opts := options.CreateCollection().SetValidator(validator)
return m.Db.CreateCollection(ctx, "users", opts)
}
func (m *Migrator) Migration_TIMESTAMP_create_users_table_Down(ctx context.Context) error {
return m.Db.Collection("users").Drop(ctx)
}export GOMIGER_URI="mongodb://localhost:27017/myapp"# Run all pending migrations
go run main.go up
# Run up to specific version
go run main.go up 202410151200# Rollback to specific version
go run main.go down 202410151200# List migration status (if implemented in your CLI)
go run main.go statusComing soon...
-
Migration fails to connect
- Check your
GOMIGER_URIenvironment variable - Ensure the database is running and accessible
- Check your
-
Migration already applied
- Check your schema store collection/table
- Use
downcommand to rollback if needed
-
Import errors
- Run
go mod tidyto resolve dependencies - Check that all required packages are installed
- Run
- Open an issue on GitHub
- Join our Discussions
- Learn about Plugin Development
- Read our Best Practices Guide
- Explore Advanced Examples
- Join our community on GitHub Discussions