Skip to content

Commit

Permalink
Merge pull request #34 from ShanghaitechGeekPie/fix-ranklist
Browse files Browse the repository at this point in the history
feat(database/upgrade.UpgradeDBFrom3To4): set default for user reward
  • Loading branch information
Prince213 authored May 22, 2024
2 parents 1c3ca2f + 10839f0 commit 439a5c1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/database/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"coursebench-backend/pkg/log"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"

"gorm.io/gorm"
)

Expand All @@ -48,7 +49,11 @@ func UpgradeDB() {
case 2:
log.Println("Upgrading database version from 2 to 3...")
UpgradeDBFrom2To3()
fallthrough
case 3:
log.Println("Upgrading database version from 3 to 4...")
UpgradeDBFrom3To4()
case 4:
default:
log.Panicf("The version of database is: %d, which is newer than the backend.", metadata.DBVersion)
}
Expand Down Expand Up @@ -106,3 +111,17 @@ func UpgradeDBFrom2To3() {
log.Panicln(err)
}
}

func UpgradeDBFrom3To4() {
db := database.GetDB()
err := db.Transaction(func(tx *gorm.DB) error {
err := tx.Model(&models.User{}).Where("reward is null").Update("reward", 0).Error
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}
return nil
})
if err != nil {
log.Panicln(err)
}
}

0 comments on commit 439a5c1

Please sign in to comment.