From 9280067d8773386f32ab0a553f6416facb66706a Mon Sep 17 00:00:00 2001 From: saji Date: Thu, 6 Jul 2023 15:21:41 -0500 Subject: [PATCH] db migration fixes - fix offbyone for executing migrations - fix user_version setter not working - add one more migration for WIP weather table --- internal/db/db.go | 2 +- internal/db/migration.go | 2 +- internal/db/migration_test.go | 2 ++ internal/db/migrations/3_weather_down.sql | 1 + internal/db/migrations/3_weather_up.sql | 6 ++++++ 5 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 internal/db/migrations/3_weather_down.sql create mode 100644 internal/db/migrations/3_weather_up.sql diff --git a/internal/db/db.go b/internal/db/db.go index d30153f..0272ea3 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -65,7 +65,7 @@ func (tdb *TelemDb) GetVersion() (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) return err } diff --git a/internal/db/migration.go b/internal/db/migration.go index b81e924..ef0c355 100644 --- a/internal/db/migration.go +++ b/internal/db/migration.go @@ -100,7 +100,7 @@ func RunMigrations(tdb *TelemDb) (finalVer int, err error) { if err != nil { return } - for v := currentVer + 1; v < finalVer; v++ { + for v := currentVer + 1; v <= finalVer; v++ { // attempt to get the "up" migration. mMap, ok := migrations[v] if !ok { diff --git a/internal/db/migration_test.go b/internal/db/migration_test.go index cfd935f..51e018b 100644 --- a/internal/db/migration_test.go +++ b/internal/db/migration_test.go @@ -6,6 +6,8 @@ import ( "testing" ) +// import just the first and second migrations to ensure stability. +// //go:embed migrations/1_*.sql //go:embed migrations/2_*.sql var testFs embed.FS diff --git a/internal/db/migrations/3_weather_down.sql b/internal/db/migrations/3_weather_down.sql new file mode 100644 index 0000000..1c4ad6b --- /dev/null +++ b/internal/db/migrations/3_weather_down.sql @@ -0,0 +1 @@ +DROP TABLE "weather_station_logs"; \ No newline at end of file diff --git a/internal/db/migrations/3_weather_up.sql b/internal/db/migrations/3_weather_up.sql new file mode 100644 index 0000000..839efcb --- /dev/null +++ b/internal/db/migrations/3_weather_up.sql @@ -0,0 +1,6 @@ +CREATE TABLE "weather_station_logs" ( + "ts" INTEGER NOT NULL, + "wind_speed" REAL, + "ground speed" REAL, + "heading" REAL +); \ No newline at end of file