code.gitea.io/gitea@v1.21.7/models/migrations/v1_20/v251.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  func FixIncorrectOwnerTeamUnitAccessMode(x *xorm.Engine) error {
    13  	type UnitType int
    14  	type AccessMode int
    15  
    16  	type TeamUnit struct {
    17  		ID         int64    `xorm:"pk autoincr"`
    18  		OrgID      int64    `xorm:"INDEX"`
    19  		TeamID     int64    `xorm:"UNIQUE(s)"`
    20  		Type       UnitType `xorm:"UNIQUE(s)"`
    21  		AccessMode AccessMode
    22  	}
    23  
    24  	const (
    25  		// AccessModeOwner owner access
    26  		AccessModeOwner = 4
    27  	)
    28  
    29  	sess := x.NewSession()
    30  	defer sess.Close()
    31  
    32  	if err := sess.Begin(); err != nil {
    33  		return err
    34  	}
    35  
    36  	count, err := sess.Table("team_unit").
    37  		Where("team_id IN (SELECT id FROM team WHERE authorize = ?)", AccessModeOwner).
    38  		Update(&TeamUnit{
    39  			AccessMode: AccessModeOwner,
    40  		})
    41  	if err != nil {
    42  		return err
    43  	}
    44  	log.Debug("Updated %d owner team unit access mode to belong to owner instead of none", count)
    45  
    46  	return sess.Commit()
    47  }