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

Commit

Permalink
Fix Idempotency of base.sql and tests thereof
Browse files Browse the repository at this point in the history
This commit fixes the idempotency of base.sql (we forgot an OR REPLACE)
and also fixes the tests that migrating to the same version works; we
had been using an artificial version number which didn't conform to our
versioning scheme, and thus broke the migration path.
  • Loading branch information
JLockerman committed Aug 17, 2020
1 parent cd76d85 commit f3c4a7f
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apk update && apk add --no-cache git \
&& go mod download \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& CGO_ENABLED=0 go build -a \
--ldflags '-w' --ldflags "-X main.CommitHash=$GIT_COMMIT" \
--ldflags '-w' --ldflags "-X version.CommitHash=$GIT_COMMIT" \
-o /go/timescale-prometheus ./cmd/timescale-prometheus

# Final image
Expand Down
9 changes: 5 additions & 4 deletions cmd/timescale-prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/timescale/timescale-prometheus/pkg/pgmodel"
"github.com/timescale/timescale-prometheus/pkg/query"
"github.com/timescale/timescale-prometheus/pkg/util"
"github.com/timescale/timescale-prometheus/pkg/version"
)

type config struct {
Expand Down Expand Up @@ -149,16 +150,16 @@ func init() {
func main() {
cfg, err := parseFlags()
if err != nil {
fmt.Println("Version: ", Version, "Commit Hash: ", CommitHash)
fmt.Println("Version: ", version.Version, "Commit Hash: ", version.CommitHash)
fmt.Println("Fatal error: cannot parse flags ", err)
}
err = log.Init(cfg.logLevel)
if err != nil {
fmt.Println("Version: ", Version, "Commit Hash: ", CommitHash)
fmt.Println("Version: ", version.Version, "Commit Hash: ", version.CommitHash)
fmt.Println("Fatal error: cannot start logger", err)
os.Exit(1)
}
log.Info("msg", "Version:"+Version+"; Commit Hash: "+CommitHash)
log.Info("msg", "Version:"+version.Version+"; Commit Hash: "+version.CommitHash)
log.Info("config", util.MaskPassword(fmt.Sprintf("%+v", cfg)))

elector, err = initElector(cfg)
Expand Down Expand Up @@ -374,7 +375,7 @@ func migrate(cfg *pgclient.Config) error {
}
defer db.Close()

err = pgmodel.Migrate(db, pgmodel.VersionInfo{Version: Version, CommitHash: CommitHash})
err = pgmodel.Migrate(db, pgmodel.VersionInfo{Version: version.Version, CommitHash: version.CommitHash})

if err != nil {
return fmt.Errorf("Error while trying to migrate DB: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/pgmodel/end_to_end_tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/timescale/timescale-prometheus/pkg/internal/testhelpers"
"github.com/timescale/timescale-prometheus/pkg/log"
"github.com/timescale/timescale-prometheus/pkg/pgmodel"
"github.com/timescale/timescale-prometheus/pkg/version"

_ "github.com/jackc/pgx/v4/stdlib"

Expand Down Expand Up @@ -108,7 +109,7 @@ func performMigrate(t testing.TB, connectURL string) {
t.Fatal(err)
}
defer migratePool.Close()
err = Migrate(migratePool, pgmodel.VersionInfo{Version: expectedVersion, CommitHash: "azxtestcommit"})
err = Migrate(migratePool, pgmodel.VersionInfo{Version: version.Version, CommitHash: "azxtestcommit"})
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 5 additions & 8 deletions pkg/pgmodel/end_to_end_tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ import (
"github.com/timescale/timescale-prometheus/pkg/internal/testhelpers"
"github.com/timescale/timescale-prometheus/pkg/pgmodel"
"github.com/timescale/timescale-prometheus/pkg/pgmodel/test_migrations"
)

const (
expectedVersion = "0.0.1-alpha"
"github.com/timescale/timescale-prometheus/pkg/version"
)

func TestMigrate(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
withDB(t, *testDatabase, func(db *pgxpool.Pool, t testing.TB) {
var version string
err := db.QueryRow(context.Background(), "SELECT version FROM prom_schema_migrations").Scan(&version)
var dbVersion string
err := db.QueryRow(context.Background(), "SELECT version FROM prom_schema_migrations").Scan(&dbVersion)
if err != nil {
t.Fatal(err)
}
if version != expectedVersion {
t.Errorf("Version unexpected:\ngot\n%s\nwanted\n%s", version, expectedVersion)
if dbVersion != version.Version {
t.Errorf("Version unexpected:\ngot\n%s\nwanted\n%s", dbVersion, version.Version)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/pgmodel/migrations/migration_files_generated.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/pgmodel/migrations/sql/idempotent/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ LANGUAGE SQL STABLE;
GRANT EXECUTE ON FUNCTION SCHEMA_CATALOG.get_metrics_that_need_drop_chunk() TO prom_reader;

--public procedure to be called by cron
CREATE PROCEDURE SCHEMA_PROM.drop_chunks()
CREATE OR REPLACE PROCEDURE SCHEMA_PROM.drop_chunks()
AS $$
DECLARE
r RECORD;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package version

var (
// Rules for Versioning:
Expand Down

0 comments on commit f3c4a7f

Please sign in to comment.