loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Do not store user projects as organization projects (#23353)

A part of https://github.com/go-gitea/gitea/pull/22865

At first, I think we do not need 3 ProjectTypes, as we can check user
type, but it seems that it is not database friendly.

---------

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>

authored by

yp05327
delvh
Lunny Xiao
6543
and committed by
GitHub
8e45fcb6 8120c0c2

+72 -7
+2
models/migrations/migrations.go
··· 471 471 NewMigration("Rename Webhook org_id to owner_id", v1_20.RenameWebhookOrgToOwner), 472 472 // v246 -> v247 473 473 NewMigration("Add missed column owner_id for project table", v1_20.AddNewColumnForProject), 474 + // v247 -> v248 475 + NewMigration("Fix incorrect project type", v1_20.FixIncorrectProjectType), 474 476 } 475 477 476 478 // GetCurrentDBVersion returns the current db version
+50
models/migrations/v1_20/v247.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package v1_20 //nolint 5 + 6 + import ( 7 + "code.gitea.io/gitea/modules/log" 8 + 9 + "xorm.io/xorm" 10 + ) 11 + 12 + // FixIncorrectProjectType: set individual project's type from 3(TypeOrganization) to 1(TypeIndividual) 13 + func FixIncorrectProjectType(x *xorm.Engine) error { 14 + type User struct { 15 + ID int64 `xorm:"pk autoincr"` 16 + Type int 17 + } 18 + 19 + const ( 20 + UserTypeIndividual int = 0 21 + 22 + TypeIndividual uint8 = 1 23 + TypeOrganization uint8 = 3 24 + ) 25 + 26 + type Project struct { 27 + OwnerID int64 `xorm:"INDEX"` 28 + Type uint8 29 + Owner *User `xorm:"extends"` 30 + } 31 + 32 + sess := x.NewSession() 33 + defer sess.Close() 34 + 35 + if err := sess.Begin(); err != nil { 36 + return err 37 + } 38 + 39 + count, err := sess.Table("project"). 40 + Where("type = ? AND owner_id IN (SELECT id FROM `user` WHERE type = ?)", TypeOrganization, UserTypeIndividual). 41 + Update(&Project{ 42 + Type: TypeIndividual, 43 + }) 44 + if err != nil { 45 + return err 46 + } 47 + log.Debug("Updated %d projects to belong to a user instead of an organization", count) 48 + 49 + return sess.Commit() 50 + }
+1 -1
models/project/project.go
··· 172 172 // IsTypeValid checks if a project type is valid 173 173 func IsTypeValid(p Type) bool { 174 174 switch p { 175 - case TypeRepository, TypeOrganization: 175 + case TypeIndividual, TypeRepository, TypeOrganization: 176 176 return true 177 177 default: 178 178 return false
+1 -1
models/project/project_test.go
··· 20 20 typ Type 21 21 valid bool 22 22 }{ 23 - {TypeIndividual, false}, 23 + {TypeIndividual, true}, 24 24 {TypeRepository, true}, 25 25 {TypeOrganization, true}, 26 26 {UnknownType, false},
+18 -5
routers/web/org/projects.go
··· 51 51 page = 1 52 52 } 53 53 54 + var projectType project_model.Type 55 + if ctx.ContextUser.IsOrganization() { 56 + projectType = project_model.TypeOrganization 57 + } else { 58 + projectType = project_model.TypeIndividual 59 + } 54 60 projects, total, err := project_model.FindProjects(ctx, project_model.SearchOptions{ 55 61 OwnerID: ctx.ContextUser.ID, 56 62 Page: page, 57 63 IsClosed: util.OptionalBoolOf(isShowClosed), 58 64 SortType: sortType, 59 - Type: project_model.TypeOrganization, 65 + Type: projectType, 60 66 }) 61 67 if err != nil { 62 68 ctx.ServerError("FindProjects", err) ··· 66 72 opTotal, err := project_model.CountProjects(ctx, project_model.SearchOptions{ 67 73 OwnerID: ctx.ContextUser.ID, 68 74 IsClosed: util.OptionalBoolOf(!isShowClosed), 69 - Type: project_model.TypeOrganization, 75 + Type: projectType, 70 76 }) 71 77 if err != nil { 72 78 ctx.ServerError("CountProjects", err) ··· 143 149 return 144 150 } 145 151 146 - if err := project_model.NewProject(&project_model.Project{ 152 + newProject := project_model.Project{ 147 153 OwnerID: ctx.ContextUser.ID, 148 154 Title: form.Title, 149 155 Description: form.Content, 150 156 CreatorID: ctx.Doer.ID, 151 157 BoardType: form.BoardType, 152 - Type: project_model.TypeOrganization, 153 - }); err != nil { 158 + } 159 + 160 + if ctx.ContextUser.IsOrganization() { 161 + newProject.Type = project_model.TypeOrganization 162 + } else { 163 + newProject.Type = project_model.TypeIndividual 164 + } 165 + 166 + if err := project_model.NewProject(&newProject); err != nil { 154 167 ctx.ServerError("NewProject", err) 155 168 return 156 169 }