From f4fce746389f556d477a8733b814b1feeac70bac Mon Sep 17 00:00:00 2001 From: Matt Roberts Date: Thu, 26 Sep 2024 09:42:24 +0100 Subject: [PATCH] Revert "Merge pull request #1203 from Quentinchampenois/fix/pending_migrations" This reverts commit 42c9a98c921c087709a7eb61cf630ba533bf7721, reversing changes made to a9c05c5c8d7d3faddb9e8e6427dfcc30678406ef. --- app/pkg/dbx/migrate.go | 52 +++++-------------- app/pkg/dbx/migrate_test.go | 24 --------- .../209901010000_create.sql | 1 - .../210001010002_delete.sql | 1 - 4 files changed, 12 insertions(+), 66 deletions(-) delete mode 100644 app/pkg/dbx/testdata/migration_success_with_new_migrations/209901010000_create.sql delete mode 100644 app/pkg/dbx/testdata/migration_success_with_new_migrations/210001010002_delete.sql diff --git a/app/pkg/dbx/migrate.go b/app/pkg/dbx/migrate.go index eebe43154..9204c5cf7 100644 --- a/app/pkg/dbx/migrate.go +++ b/app/pkg/dbx/migrate.go @@ -4,9 +4,7 @@ import ( "context" "database/sql" stdErrors "errors" - "github.com/lib/pq" "os" - "slices" "sort" "strconv" "strings" @@ -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 { @@ -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 -} diff --git a/app/pkg/dbx/migrate_test.go b/app/pkg/dbx/migrate_test.go index 183107bc6..7ee8cc504 100644 --- a/app/pkg/dbx/migrate_test.go +++ b/app/pkg/dbx/migrate_test.go @@ -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() diff --git a/app/pkg/dbx/testdata/migration_success_with_new_migrations/209901010000_create.sql b/app/pkg/dbx/testdata/migration_success_with_new_migrations/209901010000_create.sql deleted file mode 100644 index 6f9bf34f9..000000000 --- a/app/pkg/dbx/testdata/migration_success_with_new_migrations/209901010000_create.sql +++ /dev/null @@ -1 +0,0 @@ -insert into dummy (id, description) values (400, 'Description 400A'); \ No newline at end of file diff --git a/app/pkg/dbx/testdata/migration_success_with_new_migrations/210001010002_delete.sql b/app/pkg/dbx/testdata/migration_success_with_new_migrations/210001010002_delete.sql deleted file mode 100644 index 2655a0b24..000000000 --- a/app/pkg/dbx/testdata/migration_success_with_new_migrations/210001010002_delete.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM dummy WHERE id = 400; \ No newline at end of file