code.gitea.io/gitea@v1.21.7/models/migrations/v1_21/v264.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package v1_21 //nolint
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	"code.gitea.io/gitea/modules/timeutil"
    12  
    13  	"xorm.io/xorm"
    14  )
    15  
    16  func AddBranchTable(x *xorm.Engine) error {
    17  	type Branch struct {
    18  		ID            int64
    19  		RepoID        int64  `xorm:"UNIQUE(s)"`
    20  		Name          string `xorm:"UNIQUE(s) NOT NULL"`
    21  		CommitID      string
    22  		CommitMessage string `xorm:"TEXT"`
    23  		PusherID      int64
    24  		IsDeleted     bool `xorm:"index"`
    25  		DeletedByID   int64
    26  		DeletedUnix   timeutil.TimeStamp `xorm:"index"`
    27  		CommitTime    timeutil.TimeStamp // The commit
    28  		CreatedUnix   timeutil.TimeStamp `xorm:"created"`
    29  		UpdatedUnix   timeutil.TimeStamp `xorm:"updated"`
    30  	}
    31  
    32  	if err := x.Sync(new(Branch)); err != nil {
    33  		return err
    34  	}
    35  
    36  	if exist, err := x.IsTableExist("deleted_branches"); err != nil {
    37  		return err
    38  	} else if !exist {
    39  		return nil
    40  	}
    41  
    42  	type DeletedBranch struct {
    43  		ID          int64
    44  		RepoID      int64  `xorm:"index UNIQUE(s)"`
    45  		Name        string `xorm:"UNIQUE(s) NOT NULL"`
    46  		Commit      string
    47  		DeletedByID int64
    48  		DeletedUnix timeutil.TimeStamp
    49  	}
    50  
    51  	var adminUserID int64
    52  	has, err := x.Table("user").
    53  		Select("id").
    54  		Where("is_admin=?", true).
    55  		Asc("id"). // Reliably get the admin with the lowest ID.
    56  		Get(&adminUserID)
    57  	if err != nil {
    58  		return err
    59  	} else if !has {
    60  		return fmt.Errorf("no admin user found")
    61  	}
    62  
    63  	branches := make([]Branch, 0, 100)
    64  	if err := db.Iterate(context.Background(), nil, func(ctx context.Context, deletedBranch *DeletedBranch) error {
    65  		branches = append(branches, Branch{
    66  			RepoID:      deletedBranch.RepoID,
    67  			Name:        deletedBranch.Name,
    68  			CommitID:    deletedBranch.Commit,
    69  			PusherID:    adminUserID,
    70  			IsDeleted:   true,
    71  			DeletedByID: deletedBranch.DeletedByID,
    72  			DeletedUnix: deletedBranch.DeletedUnix,
    73  		})
    74  		if len(branches) >= 100 {
    75  			_, err := x.Insert(&branches)
    76  			if err != nil {
    77  				return err
    78  			}
    79  			branches = branches[:0]
    80  		}
    81  		return nil
    82  	}); err != nil {
    83  		return err
    84  	}
    85  
    86  	if len(branches) > 0 {
    87  		if _, err := x.Insert(&branches); err != nil {
    88  			return err
    89  		}
    90  	}
    91  
    92  	return x.DropTables(new(DeletedBranch))
    93  }