code.gitea.io/gitea@v1.21.7/models/migrations/v1_20/v247.go (about)

     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  }