migrator

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 12 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package migrator provides database migration utilities.

This package wraps golang-migrate/migrate and provides a simple API for running database migrations with optional pre/post migration scripts.

Basic Usage

//go:embed migrations
var migrationsFS embed.FS

func main() {
    if err := migrator.Up(context.Background(), migrationsFS); err != nil {
        log.Fatal(err)
    }
}

Directory Structure

The migrations filesystem should have this structure:

  • migrations/*.sql (actual migrations, e.g., 000001_init.up.sql)
  • migrations/common-pre-migration/*.sql (optional pre-migration scripts)
  • migrations/common-post-migration/*.sql (optional post-migration scripts)

Migration Files

Migration files follow golang-migrate naming convention:

  • {version}_{description}.up.sql (up migrations)
  • {version}_{description}.down.sql (down migrations - not used in up-only mode)

Example:

  • 000001_create_users.up.sql
  • 000002_add_email_index.up.sql

Advanced Usage

migrator, _ := migrator.New(
    migrator.WithServiceName("myservice"),
    migrator.WithLogger(logger.New()),
    migrator.WithPreScripts(true),
    migrator.WithPostScripts(true),
)
defer migrator.Close()

migrator.Run(ctx, migrationsFS)

Features

  • Uses golang-migrate/migrate for migration management
  • Service-specific migration tracking table ({service}_schema_migrations)
  • Optional pre/post migration script execution
  • Automatic sorting and execution order
  • Transaction support per migration file
  • Integration with sqldb package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(db *sqldb.DBClient, opts ...option) (*migrator, error)

New creates a new migrator with the given database and options. DB parameter is required for explicit dependency management.

func Up

func Up(ctx context.Context, migrationsFS fs.FS, opts ...option) error

Up is a convenience function that creates DB from environment and runs migrations.

func WithLogger

func WithLogger(log logger.Logger) option

WithLogger sets a custom logger.

func WithServiceName

func WithServiceName(name string) option

WithServiceName sets the service name for the migration table.

Types

This section is empty.

Jump to

Keyboard shortcuts

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