Make using TimescaleDB optional.
Some checks failed
Build Docker image / build (push) Has been cancelled
Some checks failed
Build Docker image / build (push) Has been cancelled
This commit is contained in:
parent
68e7f28f81
commit
7abe076fd2
4 changed files with 18 additions and 9 deletions
|
@ -28,6 +28,7 @@ services:
|
||||||
MQTT_USERNAME: ${MQTT_USERNAME}
|
MQTT_USERNAME: ${MQTT_USERNAME}
|
||||||
MQTT_PASSWORD: ${MQTT_PASSWORD}
|
MQTT_PASSWORD: ${MQTT_PASSWORD}
|
||||||
PG_DB: ${PG_DB}
|
PG_DB: ${PG_DB}
|
||||||
|
TIMESCALEDB_ENABLE: ${TIMESCALEDB_ENABLED}
|
||||||
LOG_LEVEL: ${LOG_LEVEL}
|
LOG_LEVEL: ${LOG_LEVEL}
|
||||||
depends_on:
|
depends_on:
|
||||||
timescaledb:
|
timescaledb:
|
||||||
|
|
|
@ -8,6 +8,7 @@ services:
|
||||||
MQTT_USERNAME: ${MQTT_USERNAME}
|
MQTT_USERNAME: ${MQTT_USERNAME}
|
||||||
MQTT_PASSWORD: ${MQTT_PASSWORD}
|
MQTT_PASSWORD: ${MQTT_PASSWORD}
|
||||||
PG_DB: ${PG_DB}
|
PG_DB: ${PG_DB}
|
||||||
|
TIMESCALEDB_ENABLED: ${TIMESCALEDB_ENABLED}
|
||||||
LOG_LEVEL: ${LOG_LEVEL}
|
LOG_LEVEL: ${LOG_LEVEL}
|
||||||
volumes:
|
volumes:
|
||||||
- /etc/timezone:/etc/timezone:ro
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
|
|
@ -4,5 +4,7 @@ MQTT_USERNAME=your_username
|
||||||
MQTT_PASSWORD=your_password
|
MQTT_PASSWORD=your_password
|
||||||
|
|
||||||
PG_DB='host=localhost port=5432 user=postgres password=secret-replace dbname=p1 sslmode=disable'
|
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: DEBUG, INFO, WARN or ERROR. Standard: INFO
|
||||||
LOG_LEVEL=INFO
|
LOG_LEVEL=INFO
|
23
main.go
23
main.go
|
@ -315,14 +315,6 @@ func connectToPostgreSQL(pgConnStr string) error {
|
||||||
time.Sleep(5 * time.Second) // Retry after 5 seconds
|
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
|
// Create table if not exists
|
||||||
_, err = db.Exec(`
|
_, err = db.Exec(`
|
||||||
CREATE TABLE IF NOT EXISTS p1 (
|
CREATE TABLE IF NOT EXISTS p1 (
|
||||||
|
@ -349,11 +341,24 @@ func connectToPostgreSQL(pgConnStr string) error {
|
||||||
returning_l2 INT,
|
returning_l2 INT,
|
||||||
returning_l3 INT
|
returning_l3 INT
|
||||||
);
|
);
|
||||||
SELECT create_hypertable('p1', 'timestamp', if_not_exists => TRUE);
|
|
||||||
`)
|
`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error creating table:", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue