From 289b79a6d6566ffa85b00a6ecdb112a40d832abf Mon Sep 17 00:00:00 2001 From: nkwtnb Date: Thu, 1 Dec 2022 18:13:05 +0900 Subject: [PATCH] Add check pending migrations --- status.go | 17 +++++++++++++++++ tests/clickhouse/clickhouse_test.go | 12 ++++++++++++ tests/e2e/migrations_test.go | 15 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/status.go b/status.go index f53f1bece..e2f1e3dd4 100644 --- a/status.go +++ b/status.go @@ -62,3 +62,20 @@ func printMigrationStatus(db *sql.DB, version int64, script string) error { log.Printf(" %-24s -- %v\n", appliedAt, script) return nil } + +// Check if migration is pending. +func HasPending(db *sql.DB, dir string) (bool, error) { + migrations, _ := CollectMigrations(dir, minVersion, maxVersion) + q := GetDialect().migrationSQL() + for _, migration := range migrations { + var row MigrationRecord + err := db.QueryRow(q, migration.Version).Scan(&row.TStamp, &row.IsApplied) + if err != nil && err != sql.ErrNoRows { + return true, fmt.Errorf("failed to query the latest migration: %w", err) + } + if !row.IsApplied { + return true, nil + } + } + return false, nil +} diff --git a/tests/clickhouse/clickhouse_test.go b/tests/clickhouse/clickhouse_test.go index fb2643510..747504812 100644 --- a/tests/clickhouse/clickhouse_test.go +++ b/tests/clickhouse/clickhouse_test.go @@ -60,12 +60,18 @@ func TestClickUpDownAll(t *testing.T) { currentVersion, err := goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, 0) + hasPending, err := goose.HasPending(db, migrationDir) + check.NoError(t, err) + check.Bool(t, hasPending, true) err = goose.Up(db, migrationDir) check.NoError(t, err) currentVersion, err = goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, len(migrations)) + hasPending, err = goose.HasPending(db, migrationDir) + check.NoError(t, err) + check.Bool(t, hasPending, false) err = goose.DownTo(db, migrationDir, 0) check.NoError(t, err) @@ -78,6 +84,9 @@ func TestClickUpDownAll(t *testing.T) { currentVersion, err = goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, 0) + hasPending, err = goose.HasPending(db, migrationDir) + check.NoError(t, err) + check.Bool(t, hasPending, true) } func TestClickHouseFirstThree(t *testing.T) { @@ -164,6 +173,9 @@ func TestRemoteImportMigration(t *testing.T) { check.NoError(t, err) _, err = goose.GetDBVersion(db) check.NoError(t, err) + hasPending, err := goose.HasPending(db, migrationDir) + check.NoError(t, err) + check.Bool(t, hasPending, false) var count int err = db.QueryRow(`SELECT count(*) FROM taxi_zone_dictionary`).Scan(&count) diff --git a/tests/e2e/migrations_test.go b/tests/e2e/migrations_test.go index fa58a9cba..5f4f4c752 100644 --- a/tests/e2e/migrations_test.go +++ b/tests/e2e/migrations_test.go @@ -27,6 +27,9 @@ func TestMigrateUpWithReset(t *testing.T) { currentVersion, err := goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, migrations[len(migrations)-1].Version) + hasPending, err := goose.HasPending(db, migrationsDir) + check.NoError(t, err) + check.Bool(t, hasPending, false) // Validate the db migration version actually matches what goose claims it is gotVersion, err := getCurrentGooseVersion(db, goose.TableName()) check.NoError(t, err) @@ -39,6 +42,9 @@ func TestMigrateUpWithReset(t *testing.T) { currentVersion, err = goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, 0) + hasPending, err = goose.HasPending(db, migrationsDir) + check.NoError(t, err) + check.Bool(t, hasPending, true) } func TestMigrateUpWithRedo(t *testing.T) { @@ -54,6 +60,9 @@ func TestMigrateUpWithRedo(t *testing.T) { startingVersion, err := goose.EnsureDBVersion(db) check.NoError(t, err) check.Number(t, startingVersion, 0) + hasPending, err := goose.HasPending(db, migrationsDir) + check.NoError(t, err) + check.Bool(t, hasPending, true) // Migrate all for _, migration := range migrations { err = migration.Up(db) @@ -74,6 +83,9 @@ func TestMigrateUpWithRedo(t *testing.T) { currentVersion, err := goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, maxVersion) + hasPending, err = goose.HasPending(db, migrationsDir) + check.NoError(t, err) + check.Bool(t, hasPending, false) } func TestMigrateUpTo(t *testing.T) { @@ -174,6 +186,9 @@ func TestMigrateFull(t *testing.T) { currentVersion, err := goose.GetDBVersion(db) check.NoError(t, err) check.Number(t, currentVersion, migrations[len(migrations)-1].Version) + hasPending, err := goose.HasPending(db, migrationsDir) + check.NoError(t, err) + check.Bool(t, hasPending, false) // Validate the db migration version actually matches what goose claims it is gotVersion, err := getCurrentGooseVersion(db, goose.TableName()) check.NoError(t, err)