@@ -22,7 +22,7 @@ import (
22
22
23
23
const (
24
24
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;"
26
26
)
27
27
28
28
type mySrc struct {
@@ -71,7 +71,7 @@ func (t *mySrc) ReadDown(version uint) (r io.ReadCloser, identifier string, err
71
71
}
72
72
73
73
// Migrate performs a database migration to the latest version
74
- func Migrate (db * sql.DB ) error {
74
+ func Migrate (db * sql.DB ) ( err error ) {
75
75
// The migration table will be put in the public schema not in any of our schema because we never want to drop it and
76
76
// our scripts and our last down script drops our shemas
77
77
driver , err := postgres .WithInstance (db , & postgres.Config {MigrationsTable : "prom_schema_migrations" })
@@ -94,21 +94,35 @@ func Migrate(db *sql.DB) error {
94
94
if err != nil {
95
95
return err
96
96
}
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
+ }()
97
112
98
113
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
104
117
}
105
-
106
- sErr , dErr := m .Close ()
107
- if sErr != nil {
108
- return sErr
118
+ if err != nil {
119
+ return err
109
120
}
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 )
112
125
}
113
- return err
126
+
127
+ return nil
114
128
}
0 commit comments