code.gitea.io/gitea@v1.21.7/models/migrations/v1_6/v70.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package v1_6 //nolint
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"code.gitea.io/gitea/modules/setting"
    11  
    12  	"xorm.io/xorm"
    13  )
    14  
    15  func AddIssueDependencies(x *xorm.Engine) (err error) {
    16  	type IssueDependency struct {
    17  		ID           int64     `xorm:"pk autoincr"`
    18  		UserID       int64     `xorm:"NOT NULL"`
    19  		IssueID      int64     `xorm:"NOT NULL"`
    20  		DependencyID int64     `xorm:"NOT NULL"`
    21  		Created      time.Time `xorm:"-"`
    22  		CreatedUnix  int64     `xorm:"created"`
    23  		Updated      time.Time `xorm:"-"`
    24  		UpdatedUnix  int64     `xorm:"updated"`
    25  	}
    26  
    27  	const (
    28  		v16UnitTypeCode            = iota + 1 // 1 code
    29  		v16UnitTypeIssues                     // 2 issues
    30  		v16UnitTypePRs                        // 3 PRs
    31  		v16UnitTypeCommits                    // 4 Commits
    32  		v16UnitTypeReleases                   // 5 Releases
    33  		v16UnitTypeWiki                       // 6 Wiki
    34  		v16UnitTypeSettings                   // 7 Settings
    35  		v16UnitTypeExternalWiki               // 8 ExternalWiki
    36  		v16UnitTypeExternalTracker            // 9 ExternalTracker
    37  	)
    38  
    39  	if err = x.Sync(new(IssueDependency)); err != nil {
    40  		return fmt.Errorf("Error creating issue_dependency_table column definition: %w", err)
    41  	}
    42  
    43  	// Update Comment definition
    44  	// This (copied) struct does only contain fields used by xorm as the only use here is to update the database
    45  
    46  	// CommentType defines the comment type
    47  	type CommentType int
    48  
    49  	// TimeStamp defines a timestamp
    50  	type TimeStamp int64
    51  
    52  	type Comment struct {
    53  		ID               int64 `xorm:"pk autoincr"`
    54  		Type             CommentType
    55  		PosterID         int64 `xorm:"INDEX"`
    56  		IssueID          int64 `xorm:"INDEX"`
    57  		LabelID          int64
    58  		OldMilestoneID   int64
    59  		MilestoneID      int64
    60  		OldAssigneeID    int64
    61  		AssigneeID       int64
    62  		OldTitle         string
    63  		NewTitle         string
    64  		DependentIssueID int64
    65  
    66  		CommitID int64
    67  		Line     int64
    68  		Content  string `xorm:"TEXT"`
    69  
    70  		CreatedUnix TimeStamp `xorm:"INDEX created"`
    71  		UpdatedUnix TimeStamp `xorm:"INDEX updated"`
    72  
    73  		// Reference issue in commit message
    74  		CommitSHA string `xorm:"VARCHAR(40)"`
    75  	}
    76  
    77  	if err = x.Sync(new(Comment)); err != nil {
    78  		return fmt.Errorf("Error updating issue_comment table column definition: %w", err)
    79  	}
    80  
    81  	// RepoUnit describes all units of a repository
    82  	type RepoUnit struct {
    83  		ID          int64
    84  		RepoID      int64          `xorm:"INDEX(s)"`
    85  		Type        int            `xorm:"INDEX(s)"`
    86  		Config      map[string]any `xorm:"JSON"`
    87  		CreatedUnix int64          `xorm:"INDEX CREATED"`
    88  		Created     time.Time      `xorm:"-"`
    89  	}
    90  
    91  	// Updating existing issue units
    92  	units := make([]*RepoUnit, 0, 100)
    93  	err = x.Where("`type` = ?", v16UnitTypeIssues).Find(&units)
    94  	if err != nil {
    95  		return fmt.Errorf("Query repo units: %w", err)
    96  	}
    97  	for _, unit := range units {
    98  		if unit.Config == nil {
    99  			unit.Config = make(map[string]any)
   100  		}
   101  		if _, ok := unit.Config["EnableDependencies"]; !ok {
   102  			unit.Config["EnableDependencies"] = setting.Service.DefaultEnableDependencies
   103  		}
   104  		if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
   105  			return err
   106  		}
   107  	}
   108  
   109  	return err
   110  }