go-migrate

module
v0.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2025 License: MIT

README ΒΆ

go-migrate

Intelligent database migration toolkit with GORM model integration and automated script generation.

CHINESE README

δΈ­ζ–‡θ―΄ζ˜Ž

✨ Features

  • πŸ” 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
  • πŸ” Migration Preview: Zero-cost error recovery with transaction rollback testing
  • πŸ”— Multi-Database Support: Works with MySQL, PostgreSQL, SQLite through golang-migrate

πŸ“¦ Installation

go get github.com/go-xlan/go-migrate
Prerequisites
  • Go 1.22.8 or later
  • Database driver for your target database
  • GORM v2 for model definitions

πŸš€ Quick Start

Basic Usage
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())
}
CLI Integration
package main

import (
    "github.com/go-xlan/go-migrate/cobramigration"
    "github.com/go-xlan/go-migrate/previewmigrate"
    "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(previewmigrate.NewPreviewCmd(migration, db, "./scripts"))
    rootCmd.AddCommand(newscripts.NextScriptCmd(&newscripts.Config{
        Migration: migration,
        Options:   newscripts.NewOptions("./scripts"),
        DB:        db,
        Objects:   []any{&User{}, &Product{}},
    }))
    
    must.Done(rootCmd.Execute())
}

πŸ“‹ Core API Reference

Migration Analysis
  • checkmigration.CheckMigrate(db, models) - Compare schemas and return needed SQL
  • checkmigration.GetMigrateOps(db, models) - Get detailed migration operations
Migration Creation
  • newmigrate.NewWithScriptsAndDBSource[T](param) - Create with connection string
  • newmigrate.NewWithScriptsAndDatabase(param) - Create with driver instance
  • newmigrate.NewWithEmbedFsAndDatabase(param) - Create with embedded files
Script Management
  • newscripts.GetNextScriptInfo(migration, options, naming) - Analyze next script requirements
  • newscripts.NextScriptCmd(config) - CLI command for script generation
CLI Commands
  • migrate - Display current migration status
  • migrate all - Execute all pending migrations
  • migrate inc - Run next migration step
  • migrate dec - Rollback one migration step
  • preview inc - Preview next migration without changes

πŸ“ Project Structure

go-migrate/
β”œβ”€β”€ checkmigration/     # Schema analysis and SQL generation
β”œβ”€β”€ newmigrate/         # Migration instance factory
β”œβ”€β”€ newscripts/         # Script generation and management  
β”œβ”€β”€ cobramigration/     # Cobra CLI integration
β”œβ”€β”€ previewmigrate/     # Migration preview and testing
└── internal/           # Demos, examples, and utilities
    β”œβ”€β”€ demos/          # Complete demo applications
    β”œβ”€β”€ examples/       # Usage examples
    └── sketches/       # Development sketches

πŸ”§ Configuration Examples

Database Setup
// 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",
}))
Embedded Migrations
//go:embed migrations
var migrationsFS embed.FS

migration := rese.V1(newmigrate.NewWithEmbedFsAndDatabase(&newmigrate.EmbedFsAndDatabaseParam{
    MigrationsFS:     &migrationsFS,
    EmbedDirName:     "migrations",
    DatabaseName:     "mysql",
    DatabaseInstance: driver,
}))

🎯 Advanced Features

Custom Script Naming
naming := &newscripts.ScriptNaming{
    NewScriptPrefix: func(version uint) string {
        return fmt.Sprintf("%d_%s", version, description)
    },
}
Migration Options
options := newscripts.NewOptions("./scripts").
    WithDryRun(true).
    WithSurveyWritten(true)

πŸ“– Examples

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
Demo Commands

Demo1x - MySQL Integration:

# Navigate to demo1x DIR
cd internal/demos/demo1x

# Generate migration scripts
make CREATE-SCRIPT-CREATE-TABLE
make CREATE-SCRIPT-ALTER-SCHEMA

# Preview and execute migrations
make MIGRATE-PREVIEW-INC
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

# Preview and execute migrations
make MIGRATE-PREVIEW-INC
make MIGRATE-ALL
make MIGRATE-INC

πŸ“„ License

MIT License. See LICENSE.


🀝 Contributing

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 the use cases to help us understand requirements
  • ⚑ Performance issue? Help us optimize via reporting slow operations
  • πŸ”§ Configuration problem? Ask questions about complex setups
  • πŸ“’ Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • πŸ’¬ Common feedback? Each suggestion and comment is welcome

πŸ”§ Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge 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! πŸŽ‰


GitHub Stars

Stargazers

Directories ΒΆ

Path Synopsis
Package checkmigration: Database schema migration validation and SQL generation engine Performs intelligent comparison between GORM model definitions and actual database schemas Captures and parses SQL statements from GORM's DryRun mode to identify migration requirements Generates both forward and reverse migration scripts with proper categorization
Package checkmigration: Database schema migration validation and SQL generation engine Performs intelligent comparison between GORM model definitions and actual database schemas Captures and parses SQL statements from GORM's DryRun mode to identify migration requirements Generates both forward and reverse migration scripts with proper categorization
Package cobramigration: Cobra CLI integration for database migration operations Provides command-line interface for migration execution with user-friendly commands Features version display, batch migration, and step-by-step migration control Integrates seamlessly with golang-migrate for robust migration management
Package cobramigration: Cobra CLI integration for database migration operations Provides command-line interface for migration execution with user-friendly commands Features version display, batch migration, and step-by-step migration control Integrates seamlessly with golang-migrate for robust migration management
internal
demos/demo1x command
demos/demo2x command
utils
Package utils: Internal utility functions for migration operations and error handling Provides common helper functions for UUID generation and migration result processing Includes specialized error handling for golang-migrate specific error cases
Package utils: Internal utility functions for migration operations and error handling Provides common helper functions for UUID generation and migration result processing Includes specialized error handling for golang-migrate specific error cases
Package newmigrate: Database migration instance factory with multiple initialization strategies Provides flexible migration creation supporting file systems, embedded resources and database drivers Features generic type parameters for automatic driver registration and configuration Integrates with golang-migrate library for robust version control and execution
Package newmigrate: Database migration instance factory with multiple initialization strategies Provides flexible migration creation supporting file systems, embedded resources and database drivers Features generic type parameters for automatic driver registration and configuration Integrates with golang-migrate library for robust version control and execution
Package newscripts: Intelligent migration script generation and management system Provides automated script creation with version control and naming conventions Features smart version progression and content generation based on database schema changes Integrates with GORM model analysis to generate appropriate migration scripts
Package newscripts: Intelligent migration script generation and management system Provides automated script creation with version control and naming conventions Features smart version progression and content generation based on database schema changes Integrates with GORM model analysis to generate appropriate migration scripts
Package previewmigrate: Migration preview functionality for safe SQL testing Provides zero-cost error recovery by testing migrations in transactions that always rollback Features intelligent script discovery and integration with existing migration workflows Prevents dirty state issues in automated migration processes
Package previewmigrate: Migration preview functionality for safe SQL testing Provides zero-cost error recovery by testing migrations in transactions that always rollback Features intelligent script discovery and integration with existing migration workflows Prevents dirty state issues in automated migration processes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL