[#163] fixed migrate down cmd

This commit is contained in:
Kenneth Lee 2022-07-18 16:00:54 -04:00 committed by GitHub
parent f56adf26f4
commit 571c4dcc8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View File

@ -194,11 +194,9 @@ func (r *Runner) Up() ([]string, error) {
// //
// On success returns list with the reverted migrations file names. // On success returns list with the reverted migrations file names.
func (r *Runner) Down(toRevertCount int) ([]string, error) { func (r *Runner) Down(toRevertCount int) ([]string, error) {
applied := []string{} reverted := make([]string, 0, toRevertCount)
err := r.db.Transactional(func(tx *dbx.Tx) error { err := r.db.Transactional(func(tx *dbx.Tx) error {
totalReverted := 0
for i := len(r.migrationsList.Items()) - 1; i >= 0; i-- { for i := len(r.migrationsList.Items()) - 1; i >= 0; i-- {
m := r.migrationsList.Item(i) m := r.migrationsList.Item(i)
@ -208,7 +206,7 @@ func (r *Runner) Down(toRevertCount int) ([]string, error) {
} }
// revert limit reached // revert limit reached
if toRevertCount-totalReverted <= 0 { if toRevertCount-len(reverted) <= 0 {
break break
} }
@ -220,7 +218,7 @@ func (r *Runner) Down(toRevertCount int) ([]string, error) {
return fmt.Errorf("Failed to save reverted migration info for %s: %w", m.file, err) return fmt.Errorf("Failed to save reverted migration info for %s: %w", m.file, err)
} }
applied = append(applied, m.file) reverted = append(reverted, m.file)
} }
return nil return nil
@ -229,7 +227,7 @@ func (r *Runner) Down(toRevertCount int) ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return applied, nil return reverted, nil
} }
func (r *Runner) createMigrationsTable() error { func (r *Runner) createMigrationsTable() error {

View File

@ -104,7 +104,8 @@ func TestRunnerUpAndDown(t *testing.T) {
// Down() // Down()
// --- // ---
if _, err := r.Down(2); err != nil { // revert one migration
if _, err := r.Down(1); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -112,8 +113,12 @@ func TestRunnerUpAndDown(t *testing.T) {
t.Fatal("Didn't expect 3_test to be reverted.") t.Fatal("Didn't expect 3_test to be reverted.")
} }
if !test1DownCalled || !test2DownCalled { if !test2DownCalled {
t.Fatalf("Expected 1_test and 2_test to be reverted, got %v and %v", test1DownCalled, test2DownCalled) t.Fatal("Expected 2_test to be reverted.")
}
if test1DownCalled {
t.Fatal("Didn't expect 1_test to be reverted.")
} }
} }