Documentation ¶
Overview ¶
Package gooseplus provides a configurable DB Migrator.
It extends github.com/pressly/goose/v3 with the following features: * rollbacks to the initial state of a deployment whenever a migration fails in a sequence of several migrations * supports multiple-environments, so that it is possible to define environment-specific migrations * supports options: such as structured logging with zap.
Index ¶
- Variables
- type Migrator
- type Option
- func SetEnvironments(envs []string) Option
- func WithBasePath(base string) Option
- func WithDialect(dialect string) Option
- func WithEnvironments(envs ...string) Option
- func WithFS(fsys fs.FS) Option
- func WithLogger(zlg *zap.Logger) Option
- func WithMigrationTimeout(timeout time.Duration) Option
- func WithTimeout(timeout time.Duration) Option
- func WithVersionTable(table string) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMigrationTable = errors.New("could not ensure goose migration table") ErrMergeMigrations = errors.New("error merging migrations") ErrRollForward = errors.New("error rolling forward migrations. Recovered error: the db has been rollbacked to its initial state") ErrRollBack = errors.New("error rolling back migrations") )
Errors returned by the Migrator
Functions ¶
This section is empty.
Types ¶
type Migrator ¶
Migrator knows how to apply changes (migrations) to a versioned database schema.
By default, the migrator will run migrations from the "base" environment folder.
If extra environments are added using options, the migrator will merge the migrations with the other folders corresponding to these environments.
Example ¶
package main import ( "context" "database/sql" "embed" "log" "os" "path/filepath" "github.com/fredbi/gooseplus" "go.uber.org/zap" // registers go migrations for unit tests _ "github.com/fredbi/gooseplus/test_sql/unittest" _ "github.com/fredbi/gooseplus/test_sql/unittest3" // init driver _ "github.com/mattn/go-sqlite3" ) //go:embed test_sql/*/*.sql //go:embed test_sql/*/*.go var embedMigrations embed.FS func main() { const ( dir = "exampledata" driver = "sqlite3" migrationsRoot = "test_sql" ) if err := os.MkdirAll(dir, 0700); err != nil { log.Println(err) return } defer func() { _ = os.RemoveAll(dir) }() tempDB, err := os.MkdirTemp(dir, "db") if err != nil { log.Println(err) return } db, err := sql.Open("sqlite3", filepath.Join(tempDB, "example.db")) if err != nil { log.Println(err) return } zlg := zap.NewExample() migrator := gooseplus.New(db, gooseplus.WithDialect(driver), gooseplus.WithFS(embedMigrations), gooseplus.WithBasePath(migrationsRoot), gooseplus.WithLogger(zlg), ) if err := migrator.Migrate(context.Background()); err != nil { log.Println(err) return } }
Output:
type Option ¶
type Option func(*options)
Option for the Migrator.
Default settings are:
dialect: "postgres", base: "sql", envs: []string{"base"}, timeout: 5 * time.Minute, migrationTimeout: 1 * time.Minute, logger: zap.NewExample(), fsys: os.DirFS("."),
func SetEnvironments ¶
SetEnvironments overrides environment-specific folders to merge with the migrations.
Setting to nil or to an empty slice will disable folders: migrations will be searched for in the base path only.
func WithBasePath ¶
WithBasePath provides the root directory where migrations are located on the FS.
func WithDialect ¶
WithDialect indicates the database SQL dialect.
For details see https://pkg.go.dev/github.com/pressly/goose/v3#Dialect
func WithEnvironments ¶
WithEnvironments appends environment-specific folders to merge with the migrations.
The default setting is a single folder "base".
func WithFS ¶
WithFS provides the file system where migrations are located.
The base is os.Dir(".").
func WithLogger ¶
WithLogger provides a structured zap logger to the migrator.
func WithMigrationTimeout ¶
WithMigrationTimeout specifies a timeout to apply for each individual migration.
The zero value disables the timeout.
Default is 1m.
func WithTimeout ¶
WithTimeout specifies a timeout to apply to the whole migration process.
NOTE: if Migrate(ctx) is called with a context that already contains a deadline, that deadline will override this option.
The zero value disables the timeout.
Default is 5m.
func WithVersionTable ¶ added in v0.1.1
WithVersionTable tells goose to use an non-default version table.
The default is "". Setting an empty table equates to using the default.