golangmigrator

package module
v0.0.0-...-20c5a8e Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 10 Imported by: 0

README

golangmigrator

go get github.com/kodergarten/pgtestdb/migrators/golangmigrator@latest

golangmigrator provides a migrator that can be used out of the box with projects that use golang-migrate/migrate for migrations.

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

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

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

func TestMigrateFromEmbeddedFS(t *testing.T) {
  gm := golangmigrator.New(
    "migrations",
    golangmigrator.WithFS(exampleFS),
  )

  db := pgtestdb.New(t, pgtestdb.Config{
    Host:     "localhost",
    User:     "postgres",
    Password: "password",
    Port:     "5433",
    Options:  "sslmode=disable",
  }, gm)
  assert.NotEqual(t, nil, db)
}

func TestMigrateFromDisk(t *testing.T) {
  gm := golangmigrator.New("migrations")
  db := pgtestdb.New(t, pgtestdb.Config{
    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

This section is empty.

Functions

This section is empty.

Types

type GolangMigrator

type GolangMigrator struct {
	// Where the migrations come from
	MigrationsDir string
	FS            fs.FS
}

GolangMigrator is a pgtestdb.Migrator that uses golang-migrate 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.

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

func New

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

New returns a GolangMigrator, which is a pgtestdb.Migrator that uses golang-migrate to perform migrations.

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

You can configure the behavior of dbmate by passing Options:

  • WithFS allows you to use an embedded filesystem.

func (*GolangMigrator) Hash

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

func (*GolangMigrator) Migrate

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

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

func (*GolangMigrator) Prepare

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

Prepare is a no-op method.

func (*GolangMigrator) Verify

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

Verify is a no-op method.

type Option

type Option func(*GolangMigrator)

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

golang-migrate documentation: https://github.com/golang-migrate/migrate

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)

Jump to

Keyboard shortcuts

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