Skip to content

Commit

Permalink
fix(spx-backend): work around GORM's missing JOIN support in updates (
Browse files Browse the repository at this point in the history
#996)

Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
  • Loading branch information
aofei authored Oct 18, 2024
1 parent 805a472 commit ae82b14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
30 changes: 17 additions & 13 deletions spx-backend/internal/controller/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ func (ctrl *Controller) CreateProject(ctx context.Context, params *CreateProject
}
if err := tx.
Model(&model.Project{}).
Joins("JOIN project_release ON project_release.project_id = project.id").
Where("project_release.id = ?", mProject.RemixedFromReleaseID.Int64).
Update("project.remix_count", gorm.Expr("project.remix_count + 1")).
Where("id = (?)", tx.Model(&model.ProjectRelease{}).
Select("project_id").
Where("id = ?", mProject.RemixedFromReleaseID.Int64)).
Update("remix_count", gorm.Expr("remix_count + 1")).
Error; err != nil {
return err
}
Expand Down Expand Up @@ -588,20 +589,22 @@ func (ctrl *Controller) DeleteProject(ctx context.Context, owner, name string) e
}
if err := tx.
Model(&model.Project{}).
Joins("JOIN project_release ON project_release.project_id = project.id").
Where("project_release.id = ?", mProject.RemixedFromReleaseID.Int64).
Update("project.remix_count", gorm.Expr("project.remix_count - 1")).
Where("id = (?)", tx.Model(&model.ProjectRelease{}).
Select("project_id").
Where("id = ?", mProject.RemixedFromReleaseID.Int64)).
Update("remix_count", gorm.Expr("remix_count - 1")).
Error; err != nil {
return err
}
}

if err := tx.
Model(&model.User{}).
Joins("JOIN user_project_relationship ON user_project_relationship.user_id = user.id").
Where("user_project_relationship.project_id = ?", mProject.ID).
Where("user_project_relationship.liked_at IS NOT NULL").
Update("user.liked_project_count", gorm.Expr("user.liked_project_count - 1")).
Where("id IN (?)", tx.Model(&model.UserProjectRelationship{}).
Select("user_id").
Where("project_id = ?", mProject.ID).
Where("liked_at IS NOT NULL")).
Update("liked_project_count", gorm.Expr("liked_project_count - 1")).
Error; err != nil {
return err
}
Expand All @@ -614,9 +617,10 @@ func (ctrl *Controller) DeleteProject(ctx context.Context, owner, name string) e

if err := tx.
Model(&model.Project{}).
Joins("JOIN project_release ON project_release.id = project.remixed_from_release_id").
Where("project_release.project_id = ?", mProject.ID).
Update("project.remixed_from_release_id", sql.NullInt64{}).
Where("remixed_from_release_id IN (?)", tx.Model(&model.ProjectRelease{}).
Select("id").
Where("project_id = ?", mProject.ID)).
Update("remixed_from_release_id", sql.NullInt64{}).
Error; err != nil {
return err
}
Expand Down
23 changes: 13 additions & 10 deletions spx-backend/internal/controller/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ func TestControllerCreateProject(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true, SkipDefaultTransaction: true}).
Model(&model.Project{}).
Joins("JOIN project_release ON project_release.project_id = project.id").
Where("project_release.id = ?", mSourceProjectRelease.ID).
Update("project.remix_count", gorm.Expr("project.remix_count + 1")).
Where("id = (?)", ctrl.db.Model(&model.ProjectRelease{}).
Select("project_id").
Where("id = ?", mSourceProjectRelease.ID)).
Update("remix_count", gorm.Expr("remix_count + 1")).
Statement
dbMock.ExpectExec(regexp.QuoteMeta(dbMockStmt.SQL.String())).
WillReturnResult(sqlmock.NewResult(0, 1))
Expand Down Expand Up @@ -1585,10 +1586,11 @@ func TestControllerDeleteProject(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true, SkipDefaultTransaction: true}).
Model(&model.User{}).
Joins("JOIN user_project_relationship ON user_project_relationship.user_id = user.id").
Where("user_project_relationship.project_id = ?", mProject.ID).
Where("user_project_relationship.liked_at IS NOT NULL").
Update("user.liked_project_count", gorm.Expr("user.liked_project_count - 1")).
Where("id IN (?)", ctrl.db.Model(&model.UserProjectRelationship{}).
Select("user_id").
Where("project_id = ?", mProject.ID).
Where("liked_at IS NOT NULL")).
Update("liked_project_count", gorm.Expr("liked_project_count - 1")).
Statement
dbMock.ExpectExec(regexp.QuoteMeta(dbMockStmt.SQL.String())).
WillReturnResult(sqlmock.NewResult(0, 1))
Expand All @@ -1602,9 +1604,10 @@ func TestControllerDeleteProject(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true, SkipDefaultTransaction: true}).
Model(&model.Project{}).
Joins("JOIN project_release ON project_release.id = project.remixed_from_release_id").
Where("project_release.project_id = ?", mProject.ID).
Update("project.remixed_from_release_id", sql.NullInt64{}).
Where("remixed_from_release_id IN (?)", ctrl.db.Model(&model.ProjectRelease{}).
Select("id").
Where("project_id = ?", mProject.ID)).
Update("remixed_from_release_id", sql.NullInt64{}).
Statement
dbMock.ExpectExec(regexp.QuoteMeta(dbMockStmt.SQL.String())).
WillReturnResult(sqlmock.NewResult(0, 1))
Expand Down

0 comments on commit ae82b14

Please sign in to comment.