Skip to content

Commit

Permalink
Simple tests for db/migration (#421)
Browse files Browse the repository at this point in the history
## Description

Some quick/shallow test for migration definition.

## Why is this needed

#371 introduced a migration where none of the names matched :D, the Id & FuncName was fixed in #383 but that one missed the file name. Why make humans do things they're bad at when instead we can make a computer do something its good at? So this is that :D.

## How Has This Been Tested?

Ran `go test` and it found the bad file name. I artificially changed an Id and the test caught the FuncName|Id mismatch.


## How are existing users impacted? What migration steps/scripts do we need?

N/A

## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [x] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
mergify[bot] committed Jan 25, 2021
2 parents d9d9077 + 8d7fea1 commit 75bf4b6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
26 changes: 17 additions & 9 deletions db/migration/migration.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package migration

import migrate "github.com/rubenv/sql-migrate"
import (
migrate "github.com/rubenv/sql-migrate"
)

var migrations = []func() *migrate.Migration{
Get202009171251,
Get202010071530,
Get202010221010,
Get202012041103,
Get202012091055,
Get2020121691335,
}

func GetMigrations() *migrate.MemoryMigrationSource {
m := make([]*migrate.Migration, len(migrations))
for i := range migrations {
m[i] = migrations[i]()
}
return &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
Get202009171251(),
Get202010071530(),
Get202010221010(),
Get202012041103(),
Get202012091055(),
Get2020121691335(),
},
Migrations: m,
}
}
39 changes: 39 additions & 0 deletions db/migration/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migration

import (
"path"
"reflect"
"runtime"
"strings"
"testing"

assert "github.com/stretchr/testify/require"
)

func TestMigrationFuncNamesMatchIDs(t *testing.T) {
timestamps := map[string]bool{}
for _, m := range migrations {
pc := reflect.ValueOf(m).Pointer()
fn := runtime.FuncForPC(pc)

file, _ := fn.FileLine(pc)
file = path.Base(file)
root := strings.TrimSuffix(file, path.Ext(file))

migration := m()
mid := strings.Split(migration.Id, "-")[0]

assert.Equal(t, root, migration.Id, "file root name (%s) and migration id (%s) do not match", root, migration.Id)

fnName := strings.Split(fn.Name(), "migration.")[1]
assert.Equal(t, fnName, "Get"+mid, "migration func name and id timestamp mismatch, func: %s, migration.Id: %s", fnName, mid)

assert.NotContains(t, timestamps, mid)
}

}

func TestGetMigrations(t *testing.T) {
m := GetMigrations()
assert.Len(t, m.Migrations, len(migrations))
}

0 comments on commit 75bf4b6

Please sign in to comment.