-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from pressly/timestamp_versioning
add `fix` and timestamped migrations by default
- Loading branch information
Showing
9 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package goose | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func Fix(dir string) error { | ||
migrations, err := CollectMigrations(dir, minVersion, maxVersion) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// split into timestamped and versioned migrations | ||
tsMigrations, err := migrations.timestamped() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
vMigrations, err := migrations.versioned() | ||
if err != nil { | ||
return err | ||
} | ||
// Initial version. | ||
version := int64(1) | ||
if last, err := vMigrations.Last(); err == nil { | ||
version = last.Version + 1 | ||
} | ||
|
||
// fix filenames by replacing timestamps with sequential versions | ||
for _, tsm := range tsMigrations { | ||
oldPath := tsm.Source | ||
newPath := strings.Replace(oldPath, fmt.Sprintf("%d", tsm.Version), fmt.Sprintf("%05v", version), 1) | ||
|
||
if err := os.Rename(oldPath, newPath); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("RENAMED %s => %s", filepath.Base(oldPath), filepath.Base(newPath)) | ||
version++ | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package goose | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFix(t *testing.T) { | ||
dir, err := ioutil.TempDir("", "tmptest") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer os.RemoveAll(dir) // clean up | ||
defer os.Remove("goose") // clean up | ||
|
||
commands := []string{ | ||
"go build -i -o goose ./cmd/goose", | ||
fmt.Sprintf("./goose -dir=%s create create_table", dir), | ||
fmt.Sprintf("./goose -dir=%s create add_users", dir), | ||
fmt.Sprintf("./goose -dir=%s create add_indices", dir), | ||
fmt.Sprintf("./goose -dir=%s create update_users", dir), | ||
fmt.Sprintf("./goose -dir=%s fix", dir), | ||
} | ||
|
||
for _, cmd := range commands { | ||
args := strings.Split(cmd, " ") | ||
time.Sleep(1 * time.Second) | ||
out, err := exec.Command(args[0], args[1:]...).CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) | ||
} | ||
} | ||
|
||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// check that the files are in order | ||
for i, f := range files { | ||
expected := fmt.Sprintf("%05v", i+1) | ||
if !strings.HasPrefix(f.Name(), expected) { | ||
t.Errorf("failed to find %s prefix in %s", expected, f.Name()) | ||
} | ||
} | ||
|
||
// add more migrations and then fix it | ||
commands = []string{ | ||
fmt.Sprintf("./goose -dir=%s create remove_column", dir), | ||
fmt.Sprintf("./goose -dir=%s create create_books_table", dir), | ||
fmt.Sprintf("./goose -dir=%s fix", dir), | ||
} | ||
|
||
for _, cmd := range commands { | ||
args := strings.Split(cmd, " ") | ||
time.Sleep(1 * time.Second) | ||
out, err := exec.Command(args[0], args[1:]...).CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out) | ||
} | ||
} | ||
|
||
files, err = ioutil.ReadDir(dir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// check that the files still in order | ||
for i, f := range files { | ||
expected := fmt.Sprintf("%05v", i+1) | ||
if !strings.HasPrefix(f.Name(), expected) { | ||
t.Errorf("failed to find %s prefix in %s", expected, f.Name()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters