migrate

package module
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package migrate runs versioned SQL migrations against a gormx-managed database using goose. It is an opt-in module so the core framework stays free of a migration-tool dependency: import it only where you need migrations.

Typical use is from a host Setup handler, which runs after the database is connected but before the server starts serving:

//go:embed migrations/*.sql
var migrationsFS embed.FS

app.WithModule(gormx.Module(postgres.Driver())).
    Setup(func(a *host.App) error {
        return migrate.Up(context.Background(), gormx.Of(a), migrationsFS)
    })

Index

Constants

View Source
const DefaultDir = "migrations"

DefaultDir is the directory within the provided fs.FS that holds migration files. It matches the common `//go:embed migrations/*.sql` layout.

Variables

This section is empty.

Functions

func Up

func Up(ctx context.Context, db *gorm.DB, fsys fs.FS, opts ...Option) error

Up is a convenience wrapper that builds a Migrator and applies all pending migrations. Use it directly from a host Setup handler.

Types

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

Migrator applies and inspects migrations for a single database. Build one with New and reuse it; it is safe for sequential use.

func New

func New(db *gorm.DB, fsys fs.FS, opts ...Option) (*Migrator, error)

New builds a Migrator for db, reading migration files from fsys. The goose dialect is derived from the gorm driver, so the same call works for Postgres, SQLite, and MySQL.

func (*Migrator) Down

func (m *Migrator) Down(ctx context.Context) error

Down rolls back the most recently applied migration.

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) error

Up applies every pending migration in order. It is a no-op when the database is already current.

func (*Migrator) Version

func (m *Migrator) Version(ctx context.Context) (int64, error)

Version returns the current schema version (0 when no migration has run).

type Option

type Option func(*config)

Option configures a Migrator.

func WithDir

func WithDir(dir string) Option

WithDir overrides the directory within the fs.FS that holds migration files (default "migrations"). Pass "." when the fs.FS is already rooted at the migrations directory.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger used to report applied/rolled-back migrations.

Jump to

Keyboard shortcuts

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