goosemigrator

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 7 Imported by: 0

README

goosemigrator

go get github.com/peterldowns/pgtestdb/migrators/goosemigrator@latest

goosemigrator provides migrators that can be used out of the box with projects that use pressly/goose for migrations.

Currently only SQL migrations are supported, and can be read from a directory on disk or from an embedded filesystem. Golang-defined migrations are not supported by default.

You can configure the migrations directory, the table name, and the filesystem being used. Here's an example:

func TestGooseMigratorFromDisk(t *testing.T) {
  m := goosemigrator.New("migrations")
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, m)
  assert.NotEqual(t, nil, db)
}

//go:embed migrations/*.sql
var exampleFS embed.FS

func TestGooseMigratorFromFS(t *testing.T) {
  gm := goosemigrator.New(
    "migrations",
    goosemigrator.WithFS(exampleFS),
    goosemigrator.WithTableName("goose_example_migrations"),
  )
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, gm)
  assert.NotEqual(t, nil, db)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTableName = goose.TableName() //nolint:gochecknoglobals

Goose doesn't provide a constant for the default value. This will be `"goose_db_version"`.

Functions

This section is empty.

Types

type GooseMigrator

type GooseMigrator struct {
	TableName     string
	MigrationsDir string
	FS            fs.FS
}

GooseMigrator is a pgtestdb.Migrator that uses goose to perform migrations.

Because Hash() requires calculating a unique hash based on the contents of the migrations, database, this implementation only supports reading migration files from disk or an embedded filesystem.

GooseMigrator doe snot perform any Verify() or Prepare() logic.

func New

func New(migrationsDir string, opts ...Option) *GooseMigrator

New returns a GooseMigrator, which is a pgtestdb.Migrator that uses goose to perform migrations.

`migrationsDir` is the path to the directory containing migration files.

You can configure the behavior of goose by passing Options:

func (*GooseMigrator) Hash

func (gm *GooseMigrator) Hash() (string, error)

func (*GooseMigrator) Migrate

func (gm *GooseMigrator) Migrate(
	_ context.Context,
	db *sql.DB,
	_ pgtestdb.Config,
) error

Migrate runs migrate.Up() to migrate the template database.

func (*GooseMigrator) Prepare

func (*GooseMigrator) Prepare(
	_ context.Context,
	_ *sql.DB,
	_ pgtestdb.Config,
) error

Prepare is a no-op method.

func (*GooseMigrator) Verify

func (*GooseMigrator) Verify(
	_ context.Context,
	_ *sql.DB,
	_ pgtestdb.Config,
) error

Verify is a no-op method.

type Option

type Option func(*GooseMigrator)

Option provides a way to configure the GooseMigrator struct and its behavior.

goose-migrate documentation: https://github.com/pressly/goose#migrations

See:

func WithFS

func WithFS(dir fs.FS) Option

WithFS specifies a `fs.FS` from which to read the migration files.

Default: `<nil>` (reads from the real filesystem)

https://github.com/pressly/goose#embedded-sql-migrations

func WithTableName

func WithTableName(tableName string) Option

WithTableName specifies the name of the table in which goose will store its migration records.

Default: `"goose_db_version"`

Equivalent to `-table` https://github.com/pressly/goose#usage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL