Skip to content

Commit

Permalink
Fix downgrade command
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Apr 4, 2024
1 parent d75a6a6 commit d325547
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ version:
@echo "GIT_TAG: ${GIT_TAG}"
@echo "GIT_COMMIT: ${GIT_COMMIT}"

build-docker-image:
build-docker-image: ## Build docker image
@docker login -u "${DOCKER_ID_USER}" -p "${DOCKER_PASS}" docker.io
@docker build \
--platform linux/x86_64 \
Expand Down
5 changes: 3 additions & 2 deletions internal/action/redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *Redo) Run(ctx *cli.Context) error {
return nil
}

reversedMigrations := make(entity.Migrations, 0, len(migrations))
reversedMigrations := make(entity.Migrations, 0, migrationsCount)
for i := range migrations {
migration := &migrations[i]
fileName, safely := r.fileNameBuilder.Down(migration.Version, false)
Expand All @@ -80,7 +80,7 @@ func (r *Redo) Run(ctx *cli.Context) error {
reversedMigrations = append(reversedMigrations, migrations[i])
}

for i := range reversedMigrations {
for i := migrationsCount - 1; i >= 0; i-- {
migration := &reversedMigrations[i]
fileName, safely := r.fileNameBuilder.Up(migration.Version, false)

Expand All @@ -96,5 +96,6 @@ func (r *Redo) Run(ctx *cli.Context) error {
console.NumberPlural(migrationsCount, migrationWas, migrationsWere),
)
console.SuccessLn("Migration redone successfully.\n")

return nil
}
1 change: 1 addition & 0 deletions internal/service/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (m *Migration) RevertFile(ctx context.Context, entity *entity.Migration, fi
if err != nil {
m.console.Errorf("*** failed to reverted %s (time: %.3fs)\n",
entity.Version, elapsedTime.Seconds())
return err
}
if err := m.repo.RemoveMigration(ctx, entity.Version); err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions internal/service/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"strings"
"testing"

"github.com/pkg/errors"
"github.com/raoptimus/db-migrator.go/internal/action/mockaction"
"github.com/raoptimus/db-migrator.go/internal/dal/entity"
"github.com/raoptimus/db-migrator.go/internal/service/mockservice"
"github.com/raoptimus/db-migrator.go/pkg/console"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestMigration_BeginCommand(t *testing.T) {
Expand Down Expand Up @@ -96,3 +98,30 @@ func TestMigration_ApplyFile_SimpleSTMT_Successfully(t *testing.T) {
)
assert.NoError(t, err)
}

func TestMigration_RevertFile_ApplyReturnsBadError(t *testing.T) {
ctx := context.Background()
badErr := errors.New("bad")
fileName := "000000_000000_test.up.sql"
sqlReaderCloser := io.NopCloser(strings.NewReader("select 1"))

file := mockservice.NewFile(t)
file.EXPECT().
Open(fileName).
Return(sqlReaderCloser, nil)
file.EXPECT().
Exists(fileName).
Return(true, nil)

repo := mockservice.NewRepository(t)
repo.EXPECT().
ForceSafely().
Return(true)
repo.EXPECT().
ExecQueryTransaction(ctx, mock.Anything).
Return(badErr)

serv := NewMigration(&Options{}, console.NewDummy(true), file, repo)
err := serv.RevertFile(ctx, &entity.Migration{}, fileName, true)
assert.ErrorIs(t, err, badErr)
}

0 comments on commit d325547

Please sign in to comment.