Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Make migrations idempotent
Browse files Browse the repository at this point in the history
Previously migrations failed if they were run on an already migrated database.
This made the connector fail if it was ever restarted.
  • Loading branch information
cevian committed May 7, 2020
1 parent e60f185 commit 26988fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: timescale-prometheus
version: 0.1.0-alpha.2
version: 0.1.0-alpha.3
apiVersion: v2
description: Timescale Prometheus Connector deployment
11 changes: 11 additions & 0 deletions pkg/pgmodel/end_to_end_tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/timescale/timescale-prometheus/pkg/internal/testhelpers"
)

const (
Expand All @@ -34,3 +35,13 @@ func TestMigrate(t *testing.T) {

})
}

func TestMigrateTwice(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
testhelpers.WithDB(t, *testDatabase, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
performMigrate(t, *testDatabase, connectURL)
performMigrate(t, *testDatabase, connectURL)
})
}
42 changes: 28 additions & 14 deletions pkg/pgmodel/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
timescaleInstall = "CREATE EXTENSION IF NOT EXISTS timescaledb WITH SCHEMA public;"
extensionInstall = "CREATE EXTENSION timescale_prometheus_extra WITH SCHEMA %s;"
extensionInstall = "CREATE EXTENSION IF NOT EXISTS timescale_prometheus_extra WITH SCHEMA %s;"
)

type mySrc struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (t *mySrc) ReadDown(version uint) (r io.ReadCloser, identifier string, err
}

// Migrate performs a database migration to the latest version
func Migrate(db *sql.DB) error {
func Migrate(db *sql.DB) (err error) {
// The migration table will be put in the public schema not in any of our schema because we never want to drop it and
// our scripts and our last down script drops our shemas
driver, err := postgres.WithInstance(db, &postgres.Config{MigrationsTable: "prom_schema_migrations"})
Expand All @@ -94,21 +94,35 @@ func Migrate(db *sql.DB) error {
if err != nil {
return err
}
defer func() {
sourceErr, databaseErr := m.Close()
//don't override error if already set
if err != nil {
return
}
if sourceErr != nil {
err = sourceErr
return
}
if databaseErr != nil {
err = databaseErr
return
}
}()

err = m.Up()
if err == nil {
_, extErr := db.Exec(fmt.Sprintf(extensionInstall, extSchema))
if extErr != nil {
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
}
//ignore no change errors as we want this idempotent. Being up to date is not a bad thing.
if err == migrate.ErrNoChange {
err = nil
}

sErr, dErr := m.Close()
if sErr != nil {
return sErr
if err != nil {
return err
}
if dErr != nil {
return dErr

_, extErr := db.Exec(fmt.Sprintf(extensionInstall, extSchema))
if extErr != nil {
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
}
return err

return nil
}

1 comment on commit 26988fd

@jpigree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to create an issue for this. Glad it was fixed beforehand. :)
I upgraded my timescale-prometheus to the 0.1.0-alpha.3.1 version and it indeed solved the issue.

Please sign in to comment.