Intelligent database migration toolkit with GORM model integration and automated script generation.
- π Smart Schema Analysis: Auto-compare GORM models with actual database schemas
- π Automated Script Generation: Create migration scripts with intelligent version management
- π Flexible Migration Strategies: Support file-based, embedded, and database-driven approaches
- π― Comprehensive CLI: User-friendly Cobra commands for all migration operations
- π‘οΈ Safe Operations: DryRun mode and interactive confirmation for secure migrations
- π Multi-Database Support: Works with MySQL, PostgreSQL, SQLite through golang-migrate
go get github.com/go-xlan/go-migrate
- Go 1.22.8 or later
- Database driver for your target database
- GORM v2 for model definitions
package main
import (
"github.com/go-xlan/go-migrate/checkmigration"
"github.com/go-xlan/go-migrate/newmigrate"
"github.com/yyle88/must"
"gorm.io/gorm"
)
func main() {
// Initialize GORM database connection
db := setupDatabase() // Your database setup
// Check what migrations are needed
migrateSQLs := checkmigration.CheckMigrate(db, []any{&User{}, &Product{}})
// Create migration instance
migration := must.Nice(newmigrate.NewWithScriptsAndDatabase(&newmigrate.ScriptsAndDatabaseParam{
ScriptsInRoot: "./migrations",
DatabaseName: "mysql",
DatabaseInstance: databaseDriver, // Your database driver instance
}))
// Execute migrations
must.Done(migration.Up())
}
package main
import (
"github.com/go-xlan/go-migrate/cobramigration"
"github.com/go-xlan/go-migrate/newscripts"
"github.com/spf13/cobra"
"github.com/yyle88/must"
)
func main() {
// Setup database and migration instance (from previous example)
db := setupDatabase()
migration := setupMigration()
var rootCmd = &cobra.Command{Use: "app"}
// Add migration commands
rootCmd.AddCommand(cobramigration.NewMigrateCmd(migration))
rootCmd.AddCommand(newscripts.NextScriptCmd(&newscripts.Config{
Migration: migration,
Options: newscripts.NewOptions("./scripts"),
DB: db,
Objects: []any{&User{}, &Product{}},
}))
must.Done(rootCmd.Execute())
}
checkmigration.CheckMigrate(db, models)
- Compare schemas and return needed SQLcheckmigration.GetMigrateOps(db, models)
- Get detailed migration operations
newmigrate.NewWithScriptsAndDBSource[T](param)
- Create with connection stringnewmigrate.NewWithScriptsAndDatabase(param)
- Create with driver instancenewmigrate.NewWithEmbedFsAndDatabase(param)
- Create with embedded files
newscripts.GetNextScriptInfo(migration, options, naming)
- Analyze next script requirementsnewscripts.NextScriptCmd(config)
- CLI command for script generation
migrate
- Display current migration statusmigrate all
- Execute all pending migrationsmigrate inc
- Run next migration stepmigrate dec
- Rollback one migration step
go-migrate/
βββ checkmigration/ # Schema analysis and SQL generation
βββ newmigrate/ # Migration instance factory
βββ newscripts/ # Script generation and management
βββ cobramigration/ # Cobra CLI integration
βββ internal/ # Demos, examples, and utilities
βββ demos/ # Complete demo applications
βββ examples/ # Usage examples
βββ sketches/ # Development sketches
// MySQL configuration
migration := rese.V1(newmigrate.NewWithScriptsAndDatabase(&newmigrate.ScriptsAndDatabaseParam{
ScriptsInRoot: "./migrations",
DatabaseName: "mysql",
DatabaseInstance: mysqlDriver,
}))
// PostgreSQL configuration
migration := rese.V1(newmigrate.NewWithScriptsAndDBSource[*postgres.Postgres](&newmigrate.ScriptsAndDBSourceParam{
ScriptsInRoot: "./migrations",
ConnectSource: "postgres://user:pass@localhost/db?sslmode=disable",
}))
// SQLite configuration
migration := rese.V1(newmigrate.NewWithScriptsAndDBSource[*sqlite3.Sqlite](&newmigrate.ScriptsAndDBSourceParam{
ScriptsInRoot: "./migrations",
ConnectSource: "sqlite3://./database.db",
}))
//go:embed migrations
var migrationsFS embed.FS
migration := rese.V1(newmigrate.NewWithEmbedFsAndDatabase(&newmigrate.EmbedFsAndDatabaseParam{
MigrationsFS: &migrationsFS,
EmbedDirName: "migrations",
DatabaseName: "mysql",
DatabaseInstance: driver,
}))
naming := &newscripts.ScriptNaming{
NewScriptPrefix: func(version uint) string {
return fmt.Sprintf("%d_%s", version, description)
},
}
options := newscripts.NewOptions("./scripts").
WithDryRun(true).
WithSurveyWritten(true)
Check the internal/demos/
DIR for complete working examples:
- demo1x/: MySQL integration with Makefile commands
- demo2x/: PostgreSQL integration with Makefile commands
- examples/: Focused feature demonstrations
- sketches/: Development prototypes
Demo1x - MySQL Integration:
# Navigate to demo1x DIR
cd internal/demos/demo1x
# Generate migration scripts
make CREATE-SCRIPT-CREATE-TABLE
make CREATE-SCRIPT-ALTER-SCHEMA
# Execute migrations
make MIGRATE-ALL
make MIGRATE-INC
Demo2x - PostgreSQL Integration:
# Navigate to demo2x DIR
cd internal/demos/demo2x
# Generate migration scripts
make CREATE-SCRIPT-CREATE-TABLE
make CREATE-SCRIPT-ALTER-SCHEMA
# Execute migrations
make MIGRATE-ALL
make MIGRATE-INC
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a bug? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share your use cases to help us understand requirements
- β‘ Performance issue? Help us optimize by reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo for new releases and features
- π Success stories? Share how this package improved your workflow
- π¬ General feedback? All suggestions and comments are welcome
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage interface).
- Clone: Clone the forked project (
git clone https://github.com/yourname/go-migrate.git
). - Navigate: Navigate to the cloned project (
cd go-migrate
) - Branch: Create a feature branch (
git checkout -b feature/xxx
). - Code: Implement your changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...
) and follow Go code style conventions - Documentation: Update documentation for user-facing changes and use meaningful commit messages
- Stage: Stage changes (
git add .
) - Commit: Commit changes (
git commit -m "Add feature xxx"
) ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx
). - PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project by submitting pull requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Happy Coding with this package! π