Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for pending migrations #420

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions tests/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down