dbmatemigrator

package module
v0.0.0-...-d942f83 Latest Latest
Warning

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

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

README

dbmatemigrator

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

dbmatemigrator provides a migrator that can be used out of the box with projects that use amacneil/dbmate for migrations.

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

//go:embed migrations/*.sql more/*.sql
var migrationsFS embed.FS

func TestDbmateMigratorWithFSAndOptions(t *testing.T) {
  m := dbmatemigrator.New(
    // Use the embedded filesystem
    dbmatemigrator.WithFS(migrationsFS),
    // Use migrations in the "migrations" and "more" directories
    dbmatemigrator.WithDir("migrations", "more"),
    // Use "dbmate_migrations_example" as the name of the table in which to
    // store records about which migrations are applied.
    dbmatemigrator.WithTableName("dbmate_migrations_example"),
  )
  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)
}

func TestDbmateMigratorWithDefaults(t *testing.T) {
  // If you're using the default settings, you don't need to pass any options.
  // This will read migrations from disk, from the folder "./db/migrations",
  // and store the results in the "schema_migrations" table.
  m := dbmatemigrator.New()
  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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DbmateMigrator

type DbmateMigrator struct {
	MigrationsDir       []string
	MigrationsTableName string
	FS                  fs.FS
}

DbmateMigrator is a pgtestdb.Migrator that uses dbmate to perform migrations.

DbmateMigrator does not perform any Verify() or Prepare() logic.

func New

func New(opts ...Option) *DbmateMigrator

New returns a DbmateMigrator, which is a pgtestdb.Migrator that uses dbmate to perform migrations.

You can configure the behavior of dbmate by passing Options:

  • WithDir is the same as --migrations-dir
  • WithTableName is the same as --migrations-table
  • WithFS allows you to use an embedded filesystem.

func (*DbmateMigrator) Hash

func (m *DbmateMigrator) Hash() (string, error)

func (*DbmateMigrator) Migrate

func (m *DbmateMigrator) Migrate(
	_ context.Context,
	_ *sql.DB,
	templateConfig pgtestdb.Config,
) error

Migrate runs dbmate.CreateAndMigrate() to migrate the template database.

func (*DbmateMigrator) Prepare

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

Prepare is a no-op method.

func (*DbmateMigrator) Verify

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

Verify is a no-op method.

type Option

type Option func(*DbmateMigrator)

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

dbmate documentation: https://github.com/amacneil/dbmate#command-line-options

See:

func WithDir

func WithDir(dir ...string) Option

WithDir specifies the location(s) of the migration files. If you have migrations in multiple directories, you should pass each path here instead of passing WithDir multiple times.

Default: `"./db/migrations"`

Equivalent to `--migrations-dir` https://github.com/amacneil/dbmate#command-line-options

func WithFS

func WithFS(x fs.FS) Option

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

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

func WithTableName

func WithTableName(name string) Option

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

Default: `"schema_migrations"`

Equivalent to `--migrations-table` https://github.com/amacneil/dbmate#command-line-options

Jump to

Keyboard shortcuts

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