Documentation
¶
Overview ¶
Package migrate provides schema migration and seed data execution using golang-migrate.
It supports PostgreSQL, MySQL, and SQLite. Migration files are read from the local filesystem; seed files are plain .sql files executed in directory order.
Basic usage ¶
m, err := migrate.New(migrate.MigrateConfig{
DSN: "postgres://user:pass@localhost/mydb?sslmode=disable",
Dialector: "postgres",
MigrationsPath: "./migrations",
})
if err != nil {
log.Fatal(err)
}
defer m.Close()
if err := m.Migrate(ctx); err != nil {
log.Fatal(err)
}
Seeding ¶
Populate executes all .sql files found in PopulationPath in alphabetical order. It is a no-op when PopulationPath is empty.
m, _ := migrate.New(migrate.MigrateConfig{
DSN: dsn,
Dialector: "postgres",
MigrationsPath: "./migrations",
PopulationPath: "./seeds",
})
_ = m.Migrate(ctx)
_ = m.Populate(ctx)
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidConfig = errors.New("invalid configuration") ErrDatabaseConnectionFailed = errors.New("failed to open database connection") ErrDatabasePingFailed = errors.New("failed to ping database") ErrAbsolutePathFailed = errors.New("failed to resolve absolute path") ErrMigrateInstanceFailed = errors.New("failed to create migrate instance") ErrGetVersionFailed = errors.New("failed to get current migration version") ErrDatabaseDirtyState = errors.New("database is in a dirty state, manual intervention required") ErrMigrationFailed = errors.New("failed to apply migrations") ErrGetNewVersionFailed = errors.New("failed to get new migration version after applying") ErrReadPopulationDirectory = errors.New("failed to read population directory") ErrReadPopulateFile = errors.New("failed to read seed file") ErrPopulateExecutionFailed = errors.New("failed to execute seed file") ErrDSNRequired = errors.New("dsn is required") ErrDialectorRequired = errors.New("dialector is required") ErrMigrationsPathRequired = errors.New("migrations path is required") ErrInvalidMigrationsPath = errors.New("migrations path does not exist") ErrUnsupportedDialect = errors.New("unsupported database dialect") )
Functions ¶
This section is empty.
Types ¶
type MigrateConfig ¶
type MigrateConfig struct {
DSN string
Dialector string
MigrationsPath string
PopulationPath string
}
MigrateConfig holds the database and migration settings.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator applies schema migrations and seed data using golang-migrate.
func New ¶
func New(config MigrateConfig) (*Migrator, error)
New creates a new Migrator, opening and validating the database connection.
Click to show internal directories.
Click to hide internal directories.