Make using TimescaleDB optional.
Some checks failed
Build Docker image / build (push) Has been cancelled

This commit is contained in:
Pieter Hollander 2024-02-20 18:04:30 +01:00
parent 68e7f28f81
commit 7abe076fd2
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg
4 changed files with 18 additions and 9 deletions

View file

@ -28,6 +28,7 @@ services:
MQTT_USERNAME: ${MQTT_USERNAME}
MQTT_PASSWORD: ${MQTT_PASSWORD}
PG_DB: ${PG_DB}
TIMESCALEDB_ENABLE: ${TIMESCALEDB_ENABLED}
LOG_LEVEL: ${LOG_LEVEL}
depends_on:
timescaledb:

View file

@ -8,6 +8,7 @@ services:
MQTT_USERNAME: ${MQTT_USERNAME}
MQTT_PASSWORD: ${MQTT_PASSWORD}
PG_DB: ${PG_DB}
TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED}
LOG_LEVEL: ${LOG_LEVEL}
volumes:
- /etc/timezone:/etc/timezone:ro

View file

@ -4,5 +4,7 @@ MQTT_USERNAME=your_username
MQTT_PASSWORD=your_password
PG_DB='host=localhost port=5432 user=postgres password=secret-replace dbname=p1 sslmode=disable'
TIMESCALEDB_ENABLED=true
# LOG_LEVEL: DEBUG, INFO, WARN or ERROR. Standard: INFO
LOG_LEVEL=INFO

23
main.go
View file

@ -315,14 +315,6 @@ func connectToPostgreSQL(pgConnStr string) error {
time.Sleep(5 * time.Second) // Retry after 5 seconds
}
// Enable TimescaleDB
_, err = db.Exec(`
CREATE EXTENSION IF NOT EXISTS timescaledb;
`)
if err != nil {
log.Fatal("Error creating TimescaleDB extension:", err)
}
// Create table if not exists
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS p1 (
@ -349,11 +341,24 @@ func connectToPostgreSQL(pgConnStr string) error {
returning_l2 INT,
returning_l3 INT
);
SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE);
`)
if err != nil {
log.Fatal("Error creating table:", err)
}
timescaleDBEnabled := os.Getenv("TIMESCALEDB_ENABLED")
if timescaleDBEnabled == "true" {
// Enable TimescaleDB
_, err = db.Exec(`
CREATE EXTENSION IF NOT EXISTS timescaledb;
SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE);
`)
if err != nil {
log.Fatal("Error creating TimescaleDB extension:", err)
}
}
return nil
}