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