migration

package
v0.3.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package migration runs schema migrations against a database.

Config carries the GORM database, embedded source, path, and DriverFunc; its Up, Down, Steps, Version, and Reset methods drive a migration source through the driver, giving composition roots explicit, ordered control over schema evolution instead of implicit auto-migration.

Package migration provides file-based database migration utilities for GORM. It uses golang-migrate with embedded SQL files for version-controlled schema changes.

This package is driver-agnostic. Users must provide a DriverFunc that creates the appropriate database driver for their chosen database (PostgreSQL, MySQL, SQLite, etc.).

Example usage with PostgreSQL:

import (
    "embed"
    "github.com/kbukum/gokit/database/migration"
    migratepg "github.com/golang-migrate/migrate/v4/database/postgres"
)

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

driverFunc := func(db *sql.DB) (database.Driver, error) {
    return migratepg.WithInstance(db, &migratepg.Config{})
}

err := (migration.Config{DB: gormDB, FS: migrationsFS, Path: "migrations", Driver: driverFunc}).Up()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DB     *gorm.DB
	FS     embed.FS
	Path   string
	Driver DriverFunc
}

Config describes a migration run: the GORM-managed database, the embedded migration source, the source path within it, and the driver factory.

func (Config) Down

func (c Config) Down() error

Down rolls back all versioned migrations. This will undo all applied migrations. Use Steps for partial rollback. Returns nil if there are no migrations to roll back (migrate.ErrNoChange is suppressed).

func (Config) Reset

func (c Config) Reset() error

Reset drops everything and re-applies all migrations. WARNING: This will destroy all data in the database. Use with caution. Typically used in development/testing environments only.

func (Config) Steps

func (c Config) Steps(n int) error

Steps runs n migrations (positive = up, negative = down). Use positive n to apply n forward migrations, negative n to roll back n migrations. Returns nil if the requested number of migrations cannot be applied (migrate.ErrNoChange is suppressed).

func (Config) Up

func (c Config) Up() error

Up runs all pending versioned migrations from the embedded FS. Migration files should follow the pattern: VERSION_name.up.sql and VERSION_name.down.sql. Returns nil if there are no new migrations to apply (migrate.ErrNoChange is suppressed).

func (Config) Version

func (c Config) Version() (version uint, dirty bool, err error)

Version returns the current migration version and dirty flag.

type DriverFunc

type DriverFunc func(*sql.DB) (database.Driver, error)

DriverFunc creates a migrate database driver from sql.DB. Users provide this function to specify their database driver.

Example for PostgreSQL:

import migratepg "github.com/golang-migrate/migrate/v4/database/postgres"
driverFunc := func(db *sql.DB) (database.Driver, error) {
    return migratepg.WithInstance(db, &migratepg.Config{})
}

Example for MySQL:

import migratemysql "github.com/golang-migrate/migrate/v4/database/mysql"
driverFunc := func(db *sql.DB) (database.Driver, error) {
    return migratemysql.WithInstance(db, &migratemysql.Config{})
}

Jump to

Keyboard shortcuts

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