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

Commit 26988fd

Browse files
committed
Make migrations idempotent
Previously migrations failed if they were run on an already migrated database. This made the connector fail if it was ever restarted.
1 parent e60f185 commit 26988fd

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

helm-chart/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: timescale-prometheus
2-
version: 0.1.0-alpha.2
2+
version: 0.1.0-alpha.3
33
apiVersion: v2
44
description: Timescale Prometheus Connector deployment

pkg/pgmodel/end_to_end_tests/migrate_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/jackc/pgx/v4/pgxpool"
11+
"github.com/timescale/timescale-prometheus/pkg/internal/testhelpers"
1112
)
1213

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

3536
})
3637
}
38+
39+
func TestMigrateTwice(t *testing.T) {
40+
if testing.Short() {
41+
t.Skip("skipping integration test")
42+
}
43+
testhelpers.WithDB(t, *testDatabase, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
44+
performMigrate(t, *testDatabase, connectURL)
45+
performMigrate(t, *testDatabase, connectURL)
46+
})
47+
}

pkg/pgmodel/migrate.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
timescaleInstall = "CREATE EXTENSION IF NOT EXISTS timescaledb WITH SCHEMA public;"
25-
extensionInstall = "CREATE EXTENSION timescale_prometheus_extra WITH SCHEMA %s;"
25+
extensionInstall = "CREATE EXTENSION IF NOT EXISTS timescale_prometheus_extra WITH SCHEMA %s;"
2626
)
2727

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

7373
// Migrate performs a database migration to the latest version
74-
func Migrate(db *sql.DB) error {
74+
func Migrate(db *sql.DB) (err error) {
7575
// The migration table will be put in the public schema not in any of our schema because we never want to drop it and
7676
// our scripts and our last down script drops our shemas
7777
driver, err := postgres.WithInstance(db, &postgres.Config{MigrationsTable: "prom_schema_migrations"})
@@ -94,21 +94,35 @@ func Migrate(db *sql.DB) error {
9494
if err != nil {
9595
return err
9696
}
97+
defer func() {
98+
sourceErr, databaseErr := m.Close()
99+
//don't override error if already set
100+
if err != nil {
101+
return
102+
}
103+
if sourceErr != nil {
104+
err = sourceErr
105+
return
106+
}
107+
if databaseErr != nil {
108+
err = databaseErr
109+
return
110+
}
111+
}()
97112

98113
err = m.Up()
99-
if err == nil {
100-
_, extErr := db.Exec(fmt.Sprintf(extensionInstall, extSchema))
101-
if extErr != nil {
102-
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
103-
}
114+
//ignore no change errors as we want this idempotent. Being up to date is not a bad thing.
115+
if err == migrate.ErrNoChange {
116+
err = nil
104117
}
105-
106-
sErr, dErr := m.Close()
107-
if sErr != nil {
108-
return sErr
118+
if err != nil {
119+
return err
109120
}
110-
if dErr != nil {
111-
return dErr
121+
122+
_, extErr := db.Exec(fmt.Sprintf(extensionInstall, extSchema))
123+
if extErr != nil {
124+
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
112125
}
113-
return err
126+
127+
return nil
114128
}

0 commit comments

Comments
 (0)