sqlmigrator

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

README

sqlmigrator

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

sqlmigrator provides migrators that can be used out of the box with projects that use rubenv/sql-migrate for migrations.

sqlmigrator supports any migration source and any configuration settings allowed with sql-migrate. Instead of using the global migration instance, you pass in a migrate.MigrationSet, which means that this is parallel/concurrency safe.

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

func TestSQLMigratorFromDisk(t *testing.T) {
  sm := sqlmigrator.New(&migrate.FileMigrationSource{
    Dir: "migrations",
  }, nil)
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, sm)
  assert.NotEqual(t, nil, db)
}

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

func TestSQLMigratorFromFSWithSomeConfiguration(t *testing.T) {
  sm := sqlmigrator.New(
    &migrate.EmbedFileSystemMigrationSource{
      FileSystem: exampleFS,
      Root:       "migrations",
    },
    &migrate.MigrationSet{
      SchemaName: "altschema",
      TableName:  "alt_migrations_table_name",
    },
  )
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, sm)
  assert.NotEqual(t, nil, db)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SQLMigrator

type SQLMigrator struct {
	Source       migrate.MigrationSource
	MigrationSet *migrate.MigrationSet
}

SQLMigrator is a pgtestdb.Migrator that uses sql-migrate to perform migrations.

func New

func New(source migrate.MigrationSource, migrationSet *migrate.MigrationSet) *SQLMigrator

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

`source` and `migrationSet` are both types defined by sql-migrate, for more information see their documentation.

https://github.com/rubenv/sql-migrate#as-a-library

func (*SQLMigrator) Hash

func (sm *SQLMigrator) Hash() (string, error)

func (*SQLMigrator) Migrate

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

Migrate runs migrationSet.Exec() to migrate the template database.

func (*SQLMigrator) Prepare

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

Prepare is a no-op method.

func (*SQLMigrator) Verify

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

Verify is a no-op method.

Jump to

Keyboard shortcuts

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