db migration fixes

- fix offbyone for executing migrations
- fix user_version setter not working
- add one more migration for WIP weather table
This commit is contained in:
saji 2023-07-06 15:21:41 -05:00
parent 62e162e939
commit 9280067d87
5 changed files with 11 additions and 2 deletions

View file

@ -65,7 +65,7 @@ func (tdb *TelemDb) GetVersion() (int, error) {
} }
func (tdb *TelemDb) SetVersion(version int) error { func (tdb *TelemDb) SetVersion(version int) error {
stmt := fmt.Sprintf("PRAGMA user_version %d", version) stmt := fmt.Sprintf("PRAGMA user_version = %d", version)
_, err := tdb.db.Exec(stmt) _, err := tdb.db.Exec(stmt)
return err return err
} }

View file

@ -100,7 +100,7 @@ func RunMigrations(tdb *TelemDb) (finalVer int, err error) {
if err != nil { if err != nil {
return return
} }
for v := currentVer + 1; v < finalVer; v++ { for v := currentVer + 1; v <= finalVer; v++ {
// attempt to get the "up" migration. // attempt to get the "up" migration.
mMap, ok := migrations[v] mMap, ok := migrations[v]
if !ok { if !ok {

View file

@ -6,6 +6,8 @@ import (
"testing" "testing"
) )
// import just the first and second migrations to ensure stability.
//
//go:embed migrations/1_*.sql //go:embed migrations/1_*.sql
//go:embed migrations/2_*.sql //go:embed migrations/2_*.sql
var testFs embed.FS var testFs embed.FS

View file

@ -0,0 +1 @@
DROP TABLE "weather_station_logs";

View file

@ -0,0 +1,6 @@
CREATE TABLE "weather_station_logs" (
"ts" INTEGER NOT NULL,
"wind_speed" REAL,
"ground speed" REAL,
"heading" REAL
);