migration

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

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.MigrateUp(gormDB, migrationsFS, "migrations", driverFunc)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrateDown

func MigrateDown(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error

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

func MigrateReset

func MigrateReset(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error

MigrateReset 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 MigrateSteps

func MigrateSteps(gormDB *gorm.DB, migrationsFS embed.FS, path string, n int, driverFunc DriverFunc) error

MigrateSteps 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 MigrateUp

func MigrateUp(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error

MigrateUp 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 MigrateVersion

func MigrateVersion(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) (version uint, dirty bool, err error)

MigrateVersion returns the current migration version and dirty flag.

Types

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