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

Commit 1f8cf32

Browse files
committed
Fix extension installed check for already migrated DB schema
1 parent f096af6 commit 1f8cf32

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

pkg/pgmodel/end_to_end_tests/migrate_test.go

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

1010
"github.com/jackc/pgx/v4/pgxpool"
1111
"github.com/timescale/timescale-prometheus/pkg/internal/testhelpers"
12+
"github.com/timescale/timescale-prometheus/pkg/pgmodel"
1213
)
1314

1415
const (
@@ -37,6 +38,16 @@ func TestMigrateTwice(t *testing.T) {
3738
}
3839
testhelpers.WithDB(t, *testDatabase, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
3940
performMigrate(t, connectURL)
41+
if *useExtension && !pgmodel.ExtensionIsInstalled {
42+
t.Errorf("extension is not installed, expected it to be installed")
43+
}
44+
45+
//reset the flag to make sure it's set correctly again.
46+
pgmodel.ExtensionIsInstalled = false
47+
4048
performMigrate(t, connectURL)
49+
if *useExtension && !pgmodel.ExtensionIsInstalled {
50+
t.Errorf("extension is not installed, expected it to be installed")
51+
}
4152
})
4253
}

pkg/pgmodel/migrate.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (p prefixedNames) getNames() []string {
9090
func Migrate(db *pgxpool.Pool, versionInfo VersionInfo) (err error) {
9191
migrateMutex.Lock()
9292
defer migrateMutex.Unlock()
93+
ExtensionIsInstalled = false
9394

9495
// Getting an early connection to install the extra extension.
9596
// TODO: Investigate why this is required. Installing the extension on the
@@ -115,6 +116,10 @@ func Migrate(db *pgxpool.Pool, versionInfo VersionInfo) (err error) {
115116

116117
// If already at correct version, nothing to migrate.
117118
if dbVersion.Compare(appVersion) == 0 {
119+
installExtension(conn)
120+
121+
metadataUpdate(db, ExtensionIsInstalled, "version", versionInfo.Version)
122+
metadataUpdate(db, ExtensionIsInstalled, "commit_hash", versionInfo.CommitHash)
118123
return nil
119124
}
120125

@@ -155,13 +160,7 @@ func Migrate(db *pgxpool.Pool, versionInfo VersionInfo) (err error) {
155160
return fmt.Errorf("unable to commit migration transaction: %w", err)
156161
}
157162

158-
_, extErr := conn.Exec(context.Background(), fmt.Sprintf(extensionInstall, extSchema))
159-
if extErr != nil {
160-
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
161-
ExtensionIsInstalled = false
162-
} else {
163-
ExtensionIsInstalled = true
164-
}
163+
installExtension(conn)
165164

166165
metadataUpdate(db, ExtensionIsInstalled, "version", versionInfo.Version)
167166
metadataUpdate(db, ExtensionIsInstalled, "commit_hash", versionInfo.CommitHash)
@@ -361,6 +360,16 @@ func upgradeVersion(tx pgx.Tx, from, to semver.Version) error {
361360
return nil
362361
}
363362

363+
func installExtension(conn *pgxpool.Conn) {
364+
_, extErr := conn.Exec(context.Background(), fmt.Sprintf(extensionInstall, extSchema))
365+
if extErr != nil {
366+
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
367+
ExtensionIsInstalled = false
368+
} else {
369+
ExtensionIsInstalled = true
370+
}
371+
}
372+
364373
func setDBVersion(tx pgx.Tx, version *semver.Version) error {
365374
if _, err := tx.Exec(context.Background(), truncateMigrationsTable); err != nil {
366375
return fmt.Errorf("unable to truncate migrations table: %w", err)

0 commit comments

Comments
 (0)