Skip to content

Latest commit

 

History

History

atlasmigrator

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

atlasmigrator

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

atlasmigrator provides migrators that can be used out of the box with projects that use ariga/atlas for migrations.

As recommended by the Atlas maintainers, these migrators expect the atlas CLI program to exist on your path at the time that the tests are run, and shells out to that program to run migrations. These migrators do not call the Atlas golang code directly.

DirMigrator for Versioned Workflows

The DirMigrator runs migrations by calling atlas migrate apply:

atlas migrate apply \
    --url "$DB" \
    --dir "file://$migrationsDirPath"

where migrationsDirPath is the path to a folder full of migration files as described in the Atlas documentation for "Versioned Workflows".

You can use it like this:

func TestWithDirMigrator(t *testing.T) {
  m := atlasmigrator.NewDirMigrator("migrations")
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, m)
  assert.NotEqual(t, nil, db)
}

SchemaMigrator for Declarative Workflows

The SchemaMigrator runs migrations by calling atlas schema apply:

atlas schema apply \
    --auto-approve
    --url "$DB" \
    --to "file://$schemaFilePath"

where schemaFilePath is the path to a .hcl schema file as described in the Atlas documented for "Declarative Workflows".

You can use it like this:

func TestWithSchemaMigrator(t *testing.T) {
  m := atlasmigrator.NewSchemaMigrator("schema.hcl")
  db := pgtestdb.New(t, pgtestdb.Config{
    DriverName: "pgx",
    Host:       "localhost",
    User:       "postgres",
    Password:   "password",
    Port:       "5433",
    Options:    "sslmode=disable",
  }, m)
  assert.NotEqual(t, nil, db)
}