Skip to content

Commit

Permalink
Merge pull request #102 from jovandeginste/clean-map-data
Browse files Browse the repository at this point in the history
Ensure the workout-mapdata relation is a 1-1 mapping
  • Loading branch information
jovandeginste authored Apr 14, 2024
2 parents 99908b1 + 41443e6 commit fc88008
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
24 changes: 22 additions & 2 deletions pkg/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ func Connect(driver, dsn string, debug bool, logger *slog.Logger) (*gorm.DB, err
return nil, err
}

if err := db.AutoMigrate(&User{}, &Profile{}, &Workout{}, &GPXData{}, &MapData{}, &MapDataDetails{}); err != nil {
if err := preMigrationActions(db); err != nil {
return nil, err
}

if err := convertWorkouts(db); err != nil {
if err := db.AutoMigrate(
&User{}, &Profile{}, &Workout{}, &GPXData{}, &MapData{}, &MapDataDetails{},
); err != nil {
return nil, err
}

if err := postMigrationActions(db); err != nil {
return nil, err
}

Expand All @@ -58,6 +64,20 @@ func Connect(driver, dsn string, debug bool, logger *slog.Logger) (*gorm.DB, err
return db, nil
}

func preMigrationActions(db *gorm.DB) error {
if !db.Migrator().HasTable(&MapData{}) {
return nil
}

q := db.Unscoped().Where("id < (select max(id) from map_data as m where m.workout_id = map_data.workout_id)").Delete(&MapData{})

return q.Error
}

func postMigrationActions(db *gorm.DB) error {
return convertWorkouts(db)
}

func setUserAPIKeys(db *gorm.DB) error {
users, err := GetUsers(db)
if err != nil {
Expand Down
54 changes: 29 additions & 25 deletions pkg/database/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ var ErrInvalidData = errors.New("could not convert data to a GPX structure")

type Workout struct {
gorm.Model
Name string `gorm:"not null"`
Date *time.Time `gorm:"not null"`
UserID uint `gorm:"not null;index"`
Dirty bool
User *User
Notes string
Type WorkoutType
Data *MapData `json:",omitempty"`
GPX *GPXData `json:",omitempty"`

MapData *MapData `gorm:"serializer:json;column:data" json:"-"`
GPXData []byte `gorm:"type:mediumtext" json:"-"`
Filename string `json:"-"`
Checksum []byte `gorm:"default:legacy" json:"-"`
Name string `gorm:"not null"`
Date *time.Time `gorm:"not null"`
UserID uint `gorm:"not null;index"`
Dirty bool
User *User
Notes string
Type WorkoutType
Data *MapData `json:",omitempty"`
GPX *GPXData `json:",omitempty"`
MapData *MapData `gorm:"serializer:json;column:data" json:"-"`

GPXData []byte `gorm:"type:mediumtext" json:"-"` // To be removed
Filename string `json:"-"` // To be removed
Checksum []byte `gorm:"default:legacy" json:"-"` // To be removed
}

type GPXData struct {
Expand Down Expand Up @@ -209,23 +209,27 @@ func (w *Workout) AsGPX() (*gpx.GPX, error) {
return converters.Parse(w.GPX.Filename, w.GPX.Content)
}

func (w *Workout) setData(data *MapData) {
if w.Data == nil {
w.Data = data
return
}

dataID := w.Data.ID
dataCreatedAt := w.Data.CreatedAt

w.Data = data
w.Data.ID = dataID
w.Data.CreatedAt = dataCreatedAt
}

func (w *Workout) UpdateData(db *gorm.DB) error {
gpxContent, err := w.AsGPX()
if err != nil {
return err
}

dataID := uint(0)
dataCreatedAt := time.Now()

if w.Data != nil {
dataID = w.Data.ID
dataCreatedAt = w.Data.CreatedAt
}

w.Data = gpxAsMapData(gpxContent)
w.Data.ID = dataID
w.Data.CreatedAt = dataCreatedAt
w.setData(gpxAsMapData(gpxContent))

if err := w.Data.Save(db); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/workouts_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func correctAltitude(creator string, lat, long, alt float64) float64 {

type MapData struct {
gorm.Model
WorkoutID uint `gorm:"not null"`
WorkoutID uint `gorm:"not null;uniqueIndex"`
Creator string
Name string
Center MapCenter `gorm:"serializer:json"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/workouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func TestWorkout_UpdateData(t *testing.T) {
require.NoError(t, w.Save(db))

ud := w.UpdatedAt
d := w.Data

w.Data = dummyMapData()
d := w.Data
w.setData(dummyMapData())
require.NoError(t, w.Save(db))

assert.NotEqual(t, d, w.Data)
Expand Down

0 comments on commit fc88008

Please sign in to comment.