pgmigrator

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: 8 Imported by: 0

README

pgmigrator

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

pgmigrator provides a migrator that can be used out of the box with projects that use peterldowns/pgmigrate for migrations.

You can use migrations from disk or from an embedded FS, and you can set the table name that the migration records are stored in. Here's an example:

func TestPGMigratorFromDisk(t *testing.T) {
	dir := os.DirFS("migrations")
	pgm, err := pgmigrator.New(dir)
	assert.Nil(t, err)
	db := pgtestdb.New(t, pgtestdb.Config{
		DriverName: "pgx",
		Host:       "localhost",
		User:       "postgres",
		Password:   "password",
		Port:       "5433",
		Options:    "sslmode=disable",
	}, pgm)
	assert.NotEqual(t, nil, db)
}

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

func TestPGMigratorFromFSAndWithOptions(t *testing.T) {
	logger := pgmigrate.NewTestLogger(t)
	pgm, err := pgmigrator.New(
		exampleFS,
		pgmigrator.WithTableName("example_table_name"),
		pgmigrator.WithLogger(logger),
	)
	assert.Nil(t, err)
	db := pgtestdb.New(t, pgtestdb.Config{
		DriverName: "pgx",
		Host:       "localhost",
		User:       "postgres",
		Password:   "password",
		Port:       "5433",
		Options:    "sslmode=disable",
	}, pgm)
	assert.NotEqual(t, nil, db)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*PGMigrator)

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

pgmigrate documentation: https://github.com/peterldowns/pgmigrate

See:

func WithLogger

func WithLogger(logger pgmigrate.Logger) Option

WithLogger sets the pgmigrate.Logger to use when applying migrations.

You probably want to use `pgmigrate.NewTestLogger(t)`.

Default: `nil`

func WithTableName

func WithTableName(tableName string) Option

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

Default: `"pgmigrate_migrations"`

type PGMigrator

type PGMigrator struct {
	// contains filtered or unexported fields
}

PGMigrator is a pgtestdb.Migrator that uses pgmigrate to perform migrations.

PGMigrator does not perform any Prepare() logic, but does implement Verify().

func New

func New(dir fs.FS, opts ...Option) (*PGMigrator, error)

New returns a PGMigrator, which is a pgtestdb.Migrator that uses pgmigrate to perform migrations.

func (*PGMigrator) Hash

func (pgm *PGMigrator) Hash() (string, error)

func (*PGMigrator) Migrate

func (pgm *PGMigrator) Migrate(
	ctx context.Context,
	db *sql.DB,
	_ pgtestdb.Config,
) error

func (*PGMigrator) Prepare

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

Prepare is a no-op method.

func (*PGMigrator) Verify

func (pgm *PGMigrator) Verify(
	ctx context.Context,
	db *sql.DB,
	_ pgtestdb.Config,
) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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