Implement proper DB migrations using Goose. Upgrade go packages.
This commit is contained in:
parent
6902facab6
commit
444e5065a4
12 changed files with 255 additions and 117 deletions
11
go.mod
11
go.mod
|
@ -3,8 +3,15 @@ module git.hollander.online/energy/opendtu-logger
|
||||||
go 1.22
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/websocket v1.5.1
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
|
github.com/pressly/goose/v3 v3.21.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/net v0.17.0 // indirect
|
require (
|
||||||
|
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||||
|
github.com/sethvargo/go-retry v0.2.4 // indirect
|
||||||
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
|
golang.org/x/net v0.27.0 // indirect
|
||||||
|
golang.org/x/sync v0.7.0 // indirect
|
||||||
|
)
|
||||||
|
|
55
go.sum
55
go.sum
|
@ -1,6 +1,57 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
|
||||||
|
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||||
|
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||||
|
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/pressly/goose/v3 v3.21.1 h1:5SSAKKWej8LVVzNLuT6KIvP1eFDuPvxa+B6H0w78buQ=
|
||||||
|
github.com/pressly/goose/v3 v3.21.1/go.mod h1:sqthmzV8PitchEkjecFJII//l43dLOCzfWh8pHEe+vE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
|
||||||
|
github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
|
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||||
|
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
|
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||||
|
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
||||||
|
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
|
||||||
|
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
|
||||||
|
modernc.org/libc v1.41.0 h1:g9YAc6BkKlgORsUWj+JwqoB1wU3o4DE3bM3yvA3k+Gk=
|
||||||
|
modernc.org/libc v1.41.0/go.mod h1:w0eszPsiXoOnoMJgrXjglgLuDy/bt5RR4y3QzUUeodY=
|
||||||
|
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||||
|
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||||
|
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
|
||||||
|
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||||
|
modernc.org/sqlite v1.29.6 h1:0lOXGrycJPptfHDuohfYgNqoe4hu+gYuN/pKgY5XjS4=
|
||||||
|
modernc.org/sqlite v1.29.6/go.mod h1:S02dvcmm7TnTRvGhv8IGYyLnIt7AS2KPaB1F/71p75U=
|
||||||
|
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
|
||||||
|
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
|
|
147
main.go
147
main.go
|
@ -11,6 +11,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -19,8 +20,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
_ "time/tzdata"
|
_ "time/tzdata"
|
||||||
|
|
||||||
|
"git.hollander.online/energy/opendtu-logger/migrations"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
"github.com/pressly/goose/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VUD contains three variables used for most metrics sent by OpenDTU:
|
// VUD contains three variables used for most metrics sent by OpenDTU:
|
||||||
|
@ -212,7 +215,7 @@ func main() {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Create tables if they don't exist
|
// Create tables if they don't exist
|
||||||
createTables(db)
|
migrateDB(db)
|
||||||
|
|
||||||
// Create WebSocket URL from config variable
|
// Create WebSocket URL from config variable
|
||||||
wsURL := "ws://" + config.OpenDTU + "/livedata"
|
wsURL := "ws://" + config.OpenDTU + "/livedata"
|
||||||
|
@ -287,122 +290,14 @@ func handleMessage(message []byte, db *sql.DB) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTables(db *sql.DB) {
|
func migrateDB(db *sql.DB) {
|
||||||
// Execute SQL statements to create tables if they don't exist
|
|
||||||
// inverter_serial is TEXT as some non-Hoymiles inverters use non-numeric serial numbers.
|
|
||||||
// An additional advantage is that it makes plotting in Grafana easier.
|
|
||||||
// TODO: Foreign keys commented out as TimescaleDB hypertables don't support them.
|
// TODO: Foreign keys commented out as TimescaleDB hypertables don't support them.
|
||||||
createTableSQL := `
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_log (
|
|
||||||
timestamp TIMESTAMPTZ UNIQUE DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
power NUMERIC,
|
|
||||||
yieldday NUMERIC,
|
|
||||||
yieldtotal NUMERIC
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_inverters (
|
// Perform DB migrations
|
||||||
timestamp TIMESTAMPTZ,
|
err := migrateFS(db, migrations.FS, ".")
|
||||||
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
|
||||||
inverter_serial TEXT,
|
|
||||||
name TEXT,
|
|
||||||
producing BOOL,
|
|
||||||
limit_relative NUMERIC,
|
|
||||||
limit_absolute NUMERIC
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_ac (
|
|
||||||
timestamp TIMESTAMPTZ,
|
|
||||||
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
|
||||||
inverter_serial TEXT,
|
|
||||||
ac_number INT,
|
|
||||||
power NUMERIC,
|
|
||||||
voltage NUMERIC,
|
|
||||||
current NUMERIC,
|
|
||||||
frequency NUMERIC,
|
|
||||||
powerfactor NUMERIC,
|
|
||||||
reactivepower NUMERIC
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_dc (
|
|
||||||
-- id SERIAL PRIMARY KEY,
|
|
||||||
timestamp TIMESTAMPTZ,
|
|
||||||
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
|
||||||
inverter_serial TEXT,
|
|
||||||
dc_number INT,
|
|
||||||
name TEXT,
|
|
||||||
power NUMERIC,
|
|
||||||
voltage NUMERIC,
|
|
||||||
current NUMERIC,
|
|
||||||
yieldday NUMERIC,
|
|
||||||
yieldtotal NUMERIC,
|
|
||||||
irradiation NUMERIC
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_inverters_inv (
|
|
||||||
-- id SERIAL PRIMARY KEY,
|
|
||||||
timestamp TIMESTAMPTZ,
|
|
||||||
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
|
||||||
inverter_serial TEXT,
|
|
||||||
temperature NUMERIC,
|
|
||||||
power_dc NUMERIC,
|
|
||||||
yieldday NUMERIC,
|
|
||||||
yieldtotal NUMERIC,
|
|
||||||
efficiency NUMERIC
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_events (
|
|
||||||
-- id SERIAL PRIMARY KEY,
|
|
||||||
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
inverter_serial TEXT,
|
|
||||||
message_id INT,
|
|
||||||
message TEXT,
|
|
||||||
start_time INT,
|
|
||||||
end_time INT
|
|
||||||
);
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
-- Check if start_timestamp column exists
|
|
||||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
|
||||||
WHERE table_name='opendtu_events'
|
|
||||||
AND column_name='start_timestamp') THEN
|
|
||||||
-- Add start_timestamp column
|
|
||||||
ALTER TABLE opendtu_events
|
|
||||||
ADD COLUMN start_timestamp TIMESTAMPTZ;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
-- Check if end_timestamp column exists
|
|
||||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
|
||||||
WHERE table_name='opendtu_events'
|
|
||||||
AND column_name='end_timestamp') THEN
|
|
||||||
-- Add end_timestamp column
|
|
||||||
ALTER TABLE opendtu_events
|
|
||||||
ADD COLUMN end_timestamp TIMESTAMPTZ;
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS opendtu_hints (
|
|
||||||
-- id SERIAL PRIMARY KEY,
|
|
||||||
timestamp TIMESTAMPTZ,
|
|
||||||
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
|
||||||
time_sync BOOL,
|
|
||||||
radio_problem BOOL,
|
|
||||||
default_password BOOL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_log_timestamp_idx ON opendtu_log (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_timestamp_idx ON opendtu_inverters (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_ac_timestamp_idx ON opendtu_inverters_ac (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_dc_timestamp_idx ON opendtu_inverters_dc (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_inverters_inv_timestamp_idx ON opendtu_inverters_inv (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_events_timestamp_idx ON opendtu_events (timestamp);
|
|
||||||
CREATE INDEX IF NOT EXISTS opendtu_hints_timestamp_idx ON opendtu_hints (timestamp);
|
|
||||||
|
|
||||||
`
|
|
||||||
|
|
||||||
_, err := db.Exec(createTableSQL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error creating tables: ", err)
|
log.Fatal("Error performing database migrations: ", err)
|
||||||
}
|
}
|
||||||
timescaleEnabled := config.TimescaleDB
|
timescaleEnabled := config.TimescaleDB
|
||||||
|
|
||||||
|
@ -502,6 +397,32 @@ func insertLiveData(db *sql.DB, inverter Inverter, total Total, hints Hints) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrate(db *sql.DB, dir string) error {
|
||||||
|
err := goose.SetDialect("postgres")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("migrate: %w", err)
|
||||||
|
}
|
||||||
|
err = goose.Up(db, dir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("migrate: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func migrateFS(db *sql.DB, migrationFS fs.FS, dir string) error {
|
||||||
|
// In case the dir is an empty string, they probably meant the current directory and goose wants a period for that.
|
||||||
|
if dir == "" {
|
||||||
|
dir = "."
|
||||||
|
}
|
||||||
|
goose.SetBaseFS(migrationFS)
|
||||||
|
defer func() {
|
||||||
|
// Ensure that we remove the FS on the off chance some other part of our app uses goose for migrations and doesn't want to use our FS.
|
||||||
|
goose.SetBaseFS(nil)
|
||||||
|
}()
|
||||||
|
return migrate(db, dir)
|
||||||
|
}
|
||||||
|
|
||||||
func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
|
func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
|
||||||
remoteURL := config.OpenDTU
|
remoteURL := config.OpenDTU
|
||||||
endpoint := fmt.Sprintf("http://"+remoteURL+"/api/eventlog/status?inv=%s", inverterSerial)
|
endpoint := fmt.Sprintf("http://"+remoteURL+"/api/eventlog/status?inv=%s", inverterSerial)
|
||||||
|
|
14
migrations/00001_log.sql
Normal file
14
migrations/00001_log.sql
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_log (
|
||||||
|
timestamp TIMESTAMPTZ UNIQUE DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
power NUMERIC,
|
||||||
|
yieldday NUMERIC,
|
||||||
|
yieldtotal NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_log_timestamp_idx ON opendtu_log (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
19
migrations/00002_inverters.sql
Normal file
19
migrations/00002_inverters.sql
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_inverters (
|
||||||
|
timestamp TIMESTAMPTZ,
|
||||||
|
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
||||||
|
-- inverter_serial is TEXT as some non-Hoymiles inverters use non-numeric serial numbers.
|
||||||
|
-- An additional advantage is that it makes plotting in Grafana easier.
|
||||||
|
inverter_serial TEXT,
|
||||||
|
name TEXT,
|
||||||
|
producing BOOL,
|
||||||
|
limit_relative NUMERIC,
|
||||||
|
limit_absolute NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_inverters_timestamp_idx ON opendtu_inverters (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
20
migrations/00003_inverters_ac.sql
Normal file
20
migrations/00003_inverters_ac.sql
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_inverters_ac (
|
||||||
|
timestamp TIMESTAMPTZ,
|
||||||
|
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
||||||
|
inverter_serial TEXT,
|
||||||
|
ac_number INT,
|
||||||
|
power NUMERIC,
|
||||||
|
voltage NUMERIC,
|
||||||
|
current NUMERIC,
|
||||||
|
frequency NUMERIC,
|
||||||
|
powerfactor NUMERIC,
|
||||||
|
reactivepower NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_inverters_ac_timestamp_idx ON opendtu_inverters_ac (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
22
migrations/00004_inverters_dc.sql
Normal file
22
migrations/00004_inverters_dc.sql
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_inverters_dc (
|
||||||
|
-- id SERIAL PRIMARY KEY,
|
||||||
|
timestamp TIMESTAMPTZ,
|
||||||
|
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
||||||
|
inverter_serial TEXT,
|
||||||
|
dc_number INT,
|
||||||
|
name TEXT,
|
||||||
|
power NUMERIC,
|
||||||
|
voltage NUMERIC,
|
||||||
|
current NUMERIC,
|
||||||
|
yieldday NUMERIC,
|
||||||
|
yieldtotal NUMERIC,
|
||||||
|
irradiation NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_inverters_dc_timestamp_idx ON opendtu_inverters_dc (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
19
migrations/00005_inverters_inv.sql
Normal file
19
migrations/00005_inverters_inv.sql
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_inverters_inv (
|
||||||
|
-- id SERIAL PRIMARY KEY,
|
||||||
|
timestamp TIMESTAMPTZ,
|
||||||
|
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
||||||
|
inverter_serial TEXT,
|
||||||
|
temperature NUMERIC,
|
||||||
|
power_dc NUMERIC,
|
||||||
|
yieldday NUMERIC,
|
||||||
|
yieldtotal NUMERIC,
|
||||||
|
efficiency NUMERIC
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_inverters_inv_timestamp_idx ON opendtu_inverters_inv (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
17
migrations/00006_events.sql
Normal file
17
migrations/00006_events.sql
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_events (
|
||||||
|
-- id SERIAL PRIMARY KEY,
|
||||||
|
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
inverter_serial TEXT,
|
||||||
|
message_id INT,
|
||||||
|
message TEXT,
|
||||||
|
start_time INT,
|
||||||
|
end_time INT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_events_timestamp_idx ON opendtu_events (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
26
migrations/00007_events.sql
Normal file
26
migrations/00007_events.sql
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- Check if start_timestamp column exists
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name='opendtu_events'
|
||||||
|
AND column_name='start_timestamp') THEN
|
||||||
|
-- Add start_timestamp column
|
||||||
|
ALTER TABLE opendtu_events
|
||||||
|
ADD COLUMN start_timestamp TIMESTAMPTZ;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Check if end_timestamp column exists
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name='opendtu_events'
|
||||||
|
AND column_name='end_timestamp') THEN
|
||||||
|
-- Add end_timestamp column
|
||||||
|
ALTER TABLE opendtu_events
|
||||||
|
ADD COLUMN end_timestamp TIMESTAMPTZ;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
16
migrations/00008_hints.sql
Normal file
16
migrations/00008_hints.sql
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE IF NOT EXISTS opendtu_hints (
|
||||||
|
-- id SERIAL PRIMARY KEY,
|
||||||
|
timestamp TIMESTAMPTZ,
|
||||||
|
-- FOREIGN KEY (timestamp) REFERENCES opendtu_log(timestamp),
|
||||||
|
time_sync BOOL,
|
||||||
|
radio_problem BOOL,
|
||||||
|
default_password BOOL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS opendtu_hints_timestamp_idx ON opendtu_hints (timestamp);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- +goose StatementEnd
|
6
migrations/fs.go
Normal file
6
migrations/fs.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package migrations
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed *.sql
|
||||||
|
var FS embed.FS
|
Loading…
Reference in a new issue