Fix order of application for migrations (#4)

This commit is contained in:
Adrian Macneil 2016-08-15 22:42:07 -07:00 committed by GitHub
parent 058bab453d
commit 6bbffde52c

View file

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"time"
)
@ -161,12 +162,13 @@ func openDatabaseForMigration(ctx *cli.Context) (Driver, *sql.DB, error) {
// MigrateCommand migrates database to the latest version
func MigrateCommand(ctx *cli.Context) error {
migrationsDir := ctx.GlobalString("migrations-dir")
available, err := findAvailableMigrations(migrationsDir)
re := regexp.MustCompile(`^\d.*\.sql$`)
files, err := findMigrationFiles(migrationsDir, re)
if err != nil {
return err
}
if len(available) == 0 {
if len(files) == 0 {
return fmt.Errorf("No migration files found.")
}
@ -181,7 +183,7 @@ func MigrateCommand(ctx *cli.Context) error {
return err
}
for filename := range available {
for _, filename := range files {
ver := migrationVersion(filename)
if ok := applied[ver]; ok {
// migration already applied
@ -238,26 +240,11 @@ func findMigrationFiles(dir string, re *regexp.Regexp) ([]string, error) {
matches = append(matches, name)
}
sort.Strings(matches)
return matches, nil
}
func findAvailableMigrations(dir string) (map[string]struct{}, error) {
re := regexp.MustCompile(`^\d.*\.sql$`)
files, err := findMigrationFiles(dir, re)
if err != nil {
return nil, err
}
// why does go not have Set?
// convert into map for easier lookups
migrations := map[string]struct{}{}
for _, name := range files {
migrations[name] = struct{}{}
}
return migrations, nil
}
func findMigrationFile(dir string, ver string) (string, error) {
if ver == "" {
panic("migration version is required")