Skip to content

Commit

Permalink
Revert "Merge pull request #1203 from Quentinchampenois/fix/pending_m…
Browse files Browse the repository at this point in the history
…igrations"

This reverts commit 42c9a98, reversing
changes made to a9c05c5.
  • Loading branch information
mattwoberts committed Sep 26, 2024
1 parent a7257e8 commit f4fce74
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 66 deletions.
52 changes: 12 additions & 40 deletions app/pkg/dbx/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"database/sql"
stdErrors "errors"
"github.com/lib/pq"
"os"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -65,23 +63,20 @@ func Migrate(ctx context.Context, path string) error {

totalMigrationsExecuted := 0

pendingVersions, err := getPendingMigrations(versions)
if err != nil {
return errors.Wrap(err, "failed to get pending migrations")
}

// Apply all migrations
for _, version := range pendingVersions {
fileName := versionFiles[version]
log.Infof(ctx, "Running Version: @{Version} (@{FileName})", dto.Props{
"Version": version,
"FileName": fileName,
})
err := runMigration(ctx, version, path, fileName)
if err != nil {
return errors.Wrap(err, "failed to run migration '%s'", fileName)
for _, version := range versions {
if version > lastVersion {
fileName := versionFiles[version]
log.Infof(ctx, "Running Version: @{Version} (@{FileName})", dto.Props{
"Version": version,
"FileName": fileName,
})
err := runMigration(ctx, version, path, fileName)
if err != nil {
return errors.Wrap(err, "failed to run migration '%s'", fileName)
}
totalMigrationsExecuted++
}
totalMigrationsExecuted++
}

if totalMigrationsExecuted > 0 {
Expand Down Expand Up @@ -145,26 +140,3 @@ func getLastMigration() (int, error) {

return int(lastVersion.Int64), nil
}

func getPendingMigrations(versions []int) ([]int, error) {
pendingMigrations := append([]int(nil), versions...)

rows, err := conn.Query("SELECT version FROM migrations_history WHERE version = ANY($1)", pq.Array(pendingMigrations))
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var version int
if err := rows.Scan(&version); err != nil {
return nil, errors.Wrap(err, "failed to scan version")
}
i := slices.Index(pendingMigrations, version)
if i != -1 {
pendingMigrations = slices.Delete(pendingMigrations, i, i+1)
}
}

return pendingMigrations, nil
}
24 changes: 0 additions & 24 deletions app/pkg/dbx/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ func TestMigrate_Success(t *testing.T) {
trx.MustRollback()
}

func TestMigrate_SuccessWithPastMigration(t *testing.T) {
setupMigrationTest(t)
ctx := context.Background()

err := dbx.Migrate(ctx, "/app/pkg/dbx/testdata/migration_success")
Expect(err).IsNil()

err = dbx.Migrate(ctx, "/app/pkg/dbx/testdata/migration_success_with_new_migrations")
Expect(err).IsNil()

trx, _ := dbx.BeginTx(ctx)
var version string
err = trx.Scalar(&version, "SELECT version FROM migrations_history WHERE version = '209901010000' LIMIT 1")
Expect(err).IsNil()
Expect(version).Equals("209901010000")

var count int
err = trx.Scalar(&count, "SELECT COUNT(*) FROM migrations_history WHERE version IN (209901010000,210001010002)")
Expect(err).IsNil()
Expect(count).Equals(2)

trx.MustRollback()
}

func TestMigrate_Failure(t *testing.T) {
setupMigrationTest(t)
ctx := context.Background()
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit f4fce74

Please sign in to comment.