Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data loss due to unnecessarily running migrations #25

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions store/migrate.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package store

import (
"bytes"
"encoding/json"
"log/slog"
"time"

"go.etcd.io/bbolt"

"github.com/ayoisaiah/focus/internal/models"
)

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

cur := bucket.Cursor()
Expand All @@ -23,14 +24,22 @@ func migrateSessions(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 = bucket.Put(newKey, v)
err = cur.Delete()
if err != nil {
return err
}

err = cur.Delete()
b, err := json.Marshal(s)
if err != nil {
return err
}

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

func migrateTimers(tx *bbolt.Tx) error {
// 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 = bucket.Put(newKey, v)
if err != nil {
return err
}

err = cur.Delete()
for k, _ := cur.First(); k != nil; k, _ = cur.Next() {
err := cur.Delete()
if err != nil {
return err
}
Expand All @@ -74,11 +65,15 @@ func migrateTimers(tx *bbolt.Tx) error {
return nil
}

func (c *Client) migrate(tx *bbolt.Tx) error {
err := migrateSessions(tx)
func (c *Client) migrate_v1_4_0(tx *bbolt.Tx) error {
slog.Info(
"running db migrations to v1.4.0 format",
)

err := migrateSessions_v1_4_0(tx)
if err != nil {
return err
}

return migrateTimers(tx)
return migrateTimers_v1_4_0(tx)
}
17 changes: 15 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

bolt "go.etcd.io/bbolt"
"golang.org/x/exp/slog"

"github.com/ayoisaiah/focus/config"
"github.com/ayoisaiah/focus/internal/models"
Expand Down Expand Up @@ -292,11 +293,23 @@ func NewClient(dbFilePath string) (*Client, error) {
bucket := tx.Bucket([]byte(focusBucket))
version := string(bucket.Get([]byte("version")))

if version != config.Version {
err = c.migrate(tx)
// prior to v1.4.0, no version info was stored in the database
// if upgrading from earlier version, run migrations to meet
// current database format
// Does nothing for new users
if version == "" {
err = c.migrate_v1_4_0(tx)
if err != nil {
return err
}
}

if version != config.Version {
slog.Info(
"updating db version",
slog.Any("previous_version", version),
slog.Any("new_version", config.Version),
)

return bucket.Put([]byte("version"), []byte(config.Version))
}
Expand Down