Skip to content

Latest commit

 

History

History

sqlmigrator

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

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)
}