Skip to content

Commit

Permalink
Merge pull request #103 from jovandeginste/uniq-starttime-per-user
Browse files Browse the repository at this point in the history
Ensure a user can not have two workouts with the same time
  • Loading branch information
jovandeginste authored Apr 14, 2024
2 parents 3173f25 + 229a0d0 commit 1131e68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion pkg/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ func preMigrationActions(db *gorm.DB) error {
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{})
q := db.Unscoped().
Where("id < (select max(id) from map_data as m where m.workout_id = map_data.workout_id)").
Delete(&MapData{})
if q.Error != nil {
return q.Error
}

q = db.Unscoped().
Where("id < (select max(id) from workouts as w where w.date = workouts.date and w.user_id = workouts.user_id)").
Delete(&Workout{})

return q.Error
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var ErrInvalidData = errors.New("could not convert data to a GPX structure")

type Workout struct {
gorm.Model
Name string `gorm:"not null"` // The name of the workout
Date *time.Time `gorm:"not null"` // The timestamp the workout was recorded
UserID uint `gorm:"not null;index"` // The ID of the user who owns the workout
Name string `gorm:"not null"` // The name of the workout
Date *time.Time `gorm:"not null;uniqueIndex:idx_start_user"` // The timestamp the workout was recorded
UserID uint `gorm:"not null;index;uniqueIndex:idx_start_user"` // The ID of the user who owns the workout
Dirty bool // Whether the workout has been modified and the details should be re-rendered
User *User // The user who owns the workout
Notes string // The notes associated with the workout, in markdown
Expand Down

0 comments on commit 1131e68

Please sign in to comment.