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 ¶
- func MigrateDown(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error
- func MigrateReset(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error
- func MigrateSteps(gormDB *gorm.DB, migrationsFS embed.FS, path string, n int, ...) error
- func MigrateUp(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) error
- func MigrateVersion(gormDB *gorm.DB, migrationsFS embed.FS, path string, driverFunc DriverFunc) (version uint, dirty bool, err error)
- type DriverFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MigrateDown ¶
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 ¶
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).
Types ¶
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{})
}