Documentation
¶
Index ¶
Constants ¶
View Source
const ( PathFieldName = "migrator.source" AutoMigrateFieldName = "migrator.auto" DriverReplacement = "{db.driver}" PathDefault = "file://./migrations/" + DriverReplacement AutoMigrateDefault = false )
Variables ¶
View Source
var Component = &component.Component{ Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewMigrator, ) }), BindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error { return container.Invoke(func(config *Config) { flagSet.StringVar(&config.Path, PathFieldName, PathDefault, "path to migrations directory") flagSet.BoolVar(&config.AutoMigrate, AutoMigrateFieldName, AutoMigrateDefault, "automatically migrate") }) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), ExecuteDuration: component.GetDurationFunc(func() time.Duration { return 0 }), Execute: component.StepFunc(func(container container.Container) error { return container.Invoke(func(config *Config, migrator *migrate.Migrate, informer logger.Informer) error { if !config.AutoMigrate { return nil } err := migrator.Up() if err != nil && !errors.Is(err, migrate.ErrNoChange) { return err } if errors.Is(err, migrate.ErrNoChange) { return nil } version, _, err := migrator.Version() informer.Infof("[db-migrator]: up to '%d' version", version) return nil }) }), }
Component is a ready-to-use Compogo component that provides database migrations. It automatically:
- Registers Config and Migrator in the DI container
- Adds command-line flags for migration configuration
- Applies configuration during Configuration phase
- Runs migrations (if enabled) during Execute phase with no timeout
Usage:
compogo.WithComponents(
db_client.Component, // database client (provides driver name)
db_migrator.Component, // migrations
// ... driver components (postgres, mysql, etc.)
)
The driver name is automatically taken from db-client configuration and used to select the appropriate migration driver.
Functions ¶
func NewMigrator ¶
func NewMigrator(config *Config, container container.Container, informer logger.Informer) (*migrate.Migrate, error)
NewMigrator creates a new golang-migrate instance based on the configuration. It performs the following steps:
- Replaces {db.driver} placeholder in the path with actual driver name
- Looks up the Getter for the configured driver
- Creates the migration driver instance
- Returns a configured migrate.Migrate instance
The migrator is not started automatically — use the Component for lifecycle management.
func Registration ¶
Registration registers a new database driver constructor for migrations. This function should be called during driver package initialization. The driver will then be available for use via the migrator component.
Example (in postgres driver):
func init() {
db_migrator.Registration(Postgres, NewPostgresMigrationDriver)
}
Types ¶
type Config ¶
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) *Config
Click to show internal directories.
Click to hide internal directories.