Skip to content

Commit

Permalink
fix v1.4.0 migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Nov 25, 2023
1 parent 3e8d6ce commit f6bfe9c
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions store/migrate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package store

import (
"bytes"
"encoding/json"
"log/slog"
"time"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/ayoisaiah/focus/internal/models"
)

// Change session key to RFC3339Nano and update duration to nanoseconds
func migrateSessions_v1_4_0(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(sessionBucket))

Expand All @@ -24,14 +24,22 @@ func migrateSessions_v1_4_0(tx *bbolt.Tx) error {
return err
}

// s.Duration was in minutes, but must now be changed to nanoseconds
s.Duration = time.Duration(s.Duration) * time.Minute

newKey := []byte(s.StartTime.Format(time.RFC3339Nano))

err = cur.Delete()
if err != nil {
return err
}

err = bucket.Put(newKey, v)
b, err := json.Marshal(s)
if err != nil {
return err
}

err = bucket.Put(newKey, b)
if err != nil {
return err
}
Expand All @@ -40,33 +48,15 @@ func migrateSessions_v1_4_0(tx *bbolt.Tx) error {
return nil
}

// Delete all exisiting timers as it won't be possible to resume paused sessions
// after migrating the sessions
func migrateTimers_v1_4_0(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(timerBucket))

cur := bucket.Cursor()

type timer struct {
DateStarted time.Time `json:"date_started"`
}

for k, v := cur.First(); k != nil; k, v = cur.Next() {
var t timer

err := json.Unmarshal(v, &t)
if err != nil {
return err
}

newKey := []byte(t.DateStarted.Format(time.RFC3339Nano))

v = bytes.Replace(v, []byte("date_started"), []byte("start_time"), 1)

err = cur.Delete()
if err != nil {
return err
}

err = bucket.Put(newKey, v)
for k, _ := cur.First(); k != nil; k, _ = cur.Next() {
err := cur.Delete()
if err != nil {
return err
}
Expand Down

0 comments on commit f6bfe9c

Please sign in to comment.