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 ¶
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 ¶
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 ¶
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 ¶
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).
type DriverFunc ¶
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{})
}