Handle TimescaleDBEnabled as bool.
All checks were successful
Build Docker image / build (push) Successful in 1m6s
Build Golang packages / release (push) Successful in 1m58s

This commit is contained in:
Pieter Hollander 2024-07-25 19:26:36 +02:00
parent 5282a22879
commit 2d843ceb3f
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

16
main.go
View file

@ -15,6 +15,7 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"strconv"
"time" "time"
_ "time/tzdata" _ "time/tzdata"
@ -147,7 +148,7 @@ type InverterSettingsData struct {
type Config struct { type Config struct {
DB string `json:"db"` DB string `json:"db"`
OpenDTU string `json:"opendtu"` OpenDTU string `json:"opendtu"`
TimescaleDBEnabled bool `json:"timescaledb"` TimescaleDB bool `json:"timescaledb"`
TZ string `json:"tz"` TZ string `json:"tz"`
} }
@ -180,7 +181,14 @@ func LoadConfig() Config {
log.Fatal("REMOTE_URL environment variable is not set.") log.Fatal("REMOTE_URL environment variable is not set.")
} }
config.TimescaleDBEnabled = os.Getenv("TIMESCALEDB_ENABLED") timescaleDBStr := os.Getenv("TIMESCALEDB_ENABLED")
if timescaleDBStr != "" {
timescaleDB, err := strconv.ParseBool(timescaleDBStr)
if err != nil {
log.Fatalf("Error parsing TIMESCALEDB_ENABLED: %v", err)
}
config.TimescaleDB = timescaleDB
}
config.TZ = os.Getenv("TZ") config.TZ = os.Getenv("TZ")
} }
@ -394,7 +402,7 @@ func createTables(db *sql.DB) {
if err != nil { if err != nil {
log.Fatal("Error creating tables: ", err) log.Fatal("Error creating tables: ", err)
} }
timescaleEnabled := config.TimescaleDBEnabled timescaleEnabled := config.TimescaleDB
enableTimescaleDB := ` enableTimescaleDB := `
-- CREATE EXTENSION IF NOT EXISTS timescaledb; -- CREATE EXTENSION IF NOT EXISTS timescaledb;
@ -406,7 +414,7 @@ func createTables(db *sql.DB) {
SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
` `
if timescaleEnabled == "true" { if timescaleEnabled {
_, err := db.Exec(enableTimescaleDB) _, err := db.Exec(enableTimescaleDB)
if err != nil { if err != nil {
log.Fatal("Error enabling TimescaleDB: ", err) log.Fatal("Error enabling TimescaleDB: ", err)