gitea源码

v247.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_20
  4. import (
  5. "code.gitea.io/gitea/modules/log"
  6. "xorm.io/xorm"
  7. )
  8. // FixIncorrectProjectType: set individual project's type from 3(TypeOrganization) to 1(TypeIndividual)
  9. func FixIncorrectProjectType(x *xorm.Engine) error {
  10. type User struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. Type int
  13. }
  14. const (
  15. UserTypeIndividual int = 0
  16. TypeIndividual uint8 = 1
  17. TypeOrganization uint8 = 3
  18. )
  19. type Project struct {
  20. OwnerID int64 `xorm:"INDEX"`
  21. Type uint8
  22. Owner *User `xorm:"extends"`
  23. }
  24. sess := x.NewSession()
  25. defer sess.Close()
  26. if err := sess.Begin(); err != nil {
  27. return err
  28. }
  29. count, err := sess.Table("project").
  30. Where("type = ? AND owner_id IN (SELECT id FROM `user` WHERE type = ?)", TypeOrganization, UserTypeIndividual).
  31. Update(&Project{
  32. Type: TypeIndividual,
  33. })
  34. if err != nil {
  35. return err
  36. }
  37. log.Debug("Updated %d projects to belong to a user instead of an organization", count)
  38. return sess.Commit()
  39. }