bunmigrator

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 9 Imported by: 0

README

bunmigrator

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

bunmigrator provides a migrator that can be used with projects that make use of uptrace/bun for migrations.

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

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


import (
	// Initialize the "pgdriver" sql.DB driver
	// Use this by setting `config.DriverName` to "pg".
	// For more information, see https://bun.uptrace.dev/postgres/#pgdriver
	_ "github.com/uptrace/bun/driver/pgdriver"
)

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

func TestMigrateFromEmbeddedFS(t *testing.T) {
	t.Parallel()
	ctx := context.Background()
	bm := bunmigrator.New("migrations", bunmigrator.WithFS(exampleFS))
	db := pgtestdb.New(t, pgtestdb.Config{
		// Use bun's "pgdriver" to connect.
		DriverName: "pg",
		Host:       "localhost",
		User:       "postgres",
		Password:   "password",
		Port:       "5433",
		Options:    "sslmode=disable",
	}, bm)

	assert.NotEqual(t, nil, db)
}

func TestMigrateFromDisk(t *testing.T) {
	t.Parallel()
	ctx := context.Background()
	bm := bunmigrator.New("migrations")
	db := pgtestdb.New(t, pgtestdb.Config{
        // Use bun's "pgdriver" to connect.
		DriverName: "pg",
		Host:       "localhost",
		User:       "postgres",
		Password:   "password",
		Port:       "5433",
		Options:    "sslmode=disable",
	}, bm)

	assert.NotEqual(t, nil, db)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BunMigrator

type BunMigrator struct {
	MigrationsDir string
	FS            fs.FS
	BunDBOpts     []bun.DBOption
	MigratorOpts  []migrate.MigratorOption
	MigrationOpts []migrate.MigrationOption
}

BunMigrator is a pgtestdb.Migrator that uses bun to perform 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.

BunMigrator does not perform any Verify() logic.

func New

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

New returns a BunMigrator, which is a pgtestdb.Migrator that uses bun to perform migrations.

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

You can configure the behaviour of bun by passing Options:

  • WithFS allows you to use an embedded filesystem.
  • WithBunDBOpts allows you to pass options to the underlying bun.DB struct.
  • WithMigrationOpts allows you to pass options to the Migrate() function.
  • WithMigratorOpts allows you to pass options to the Migrator struct.

func (*BunMigrator) Hash

func (bm *BunMigrator) Hash() (string, error)

func (*BunMigrator) Migrate

func (bm *BunMigrator) Migrate(ctx context.Context, sqldb *sql.DB, _ pgtestdb.Config) error

Migrate migrates the template database.

type Option

type Option func(*BunMigrator)

Option provides a way to configure the BunMigrator struct and its behaviour.

bun migration documentation: https://bun.uptrace.dev/guide/migrations.html

See:

func WithBunDBOpts

func WithBunDBOpts(opts ...bun.DBOption) Option

WithBunDBOpts passes options to the bun.DB struct.

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)

func WithMigrationOpts

func WithMigrationOpts(opts ...migrate.MigrationOption) Option

WithMigrationOpts passes options to the migrate.Migrator.Migrate() func.

func WithMigratorOpts

func WithMigratorOpts(opts ...migrate.MigratorOption) Option

WithMigratorOpts passes options to the migrate.Migrator.

Jump to

Keyboard shortcuts

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