You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v247.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_20 //nolint
  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. }