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

Commit

Permalink
Fix extension installed check for already migrated DB schema
Browse files Browse the repository at this point in the history
  • Loading branch information
antekresic committed Aug 7, 2020
1 parent f096af6 commit 1f8cf32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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 @@ -9,6 +9,7 @@ import (

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

const (
Expand Down Expand Up @@ -37,6 +38,16 @@ func TestMigrateTwice(t *testing.T) {
}
testhelpers.WithDB(t, *testDatabase, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
performMigrate(t, connectURL)
if *useExtension && !pgmodel.ExtensionIsInstalled {
t.Errorf("extension is not installed, expected it to be installed")
}

//reset the flag to make sure it's set correctly again.
pgmodel.ExtensionIsInstalled = false

performMigrate(t, connectURL)
if *useExtension && !pgmodel.ExtensionIsInstalled {
t.Errorf("extension is not installed, expected it to be installed")
}
})
}
23 changes: 16 additions & 7 deletions pkg/pgmodel/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (p prefixedNames) getNames() []string {
func Migrate(db *pgxpool.Pool, versionInfo VersionInfo) (err error) {
migrateMutex.Lock()
defer migrateMutex.Unlock()
ExtensionIsInstalled = false

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

// If already at correct version, nothing to migrate.
if dbVersion.Compare(appVersion) == 0 {
installExtension(conn)

metadataUpdate(db, ExtensionIsInstalled, "version", versionInfo.Version)
metadataUpdate(db, ExtensionIsInstalled, "commit_hash", versionInfo.CommitHash)
return nil
}

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

_, extErr := conn.Exec(context.Background(), fmt.Sprintf(extensionInstall, extSchema))
if extErr != nil {
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
ExtensionIsInstalled = false
} else {
ExtensionIsInstalled = true
}
installExtension(conn)

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

func installExtension(conn *pgxpool.Conn) {
_, extErr := conn.Exec(context.Background(), fmt.Sprintf(extensionInstall, extSchema))
if extErr != nil {
log.Warn("msg", "timescale_prometheus_extra extension not installed", "cause", extErr)
ExtensionIsInstalled = false
} else {
ExtensionIsInstalled = true
}
}

func setDBVersion(tx pgx.Tx, version *semver.Version) error {
if _, err := tx.Exec(context.Background(), truncateMigrationsTable); err != nil {
return fmt.Errorf("unable to truncate migrations table: %w", err)
Expand Down

0 comments on commit 1f8cf32

Please sign in to comment.