mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 08:00:04 +01:00
Fix order of application for migrations (#4)
This commit is contained in:
parent
058bab453d
commit
6bbffde52c
1 changed files with 7 additions and 20 deletions
27
commands.go
27
commands.go
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue