summaryrefslogtreecommitdiffstats
path: root/models/migrations/v39.go
blob: 1312cb33134b5318c6c2faf9e8de16a71d7bff8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
	"fmt"

	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/log"

	"github.com/go-xorm/xorm"
)

// ReleaseV39 describes the added field for Release
type ReleaseV39 struct {
	IsTag bool `xorm:"NOT NULL DEFAULT false"`
}

// TableName will be invoked by XORM to customrize the table name
func (*ReleaseV39) TableName() string {
	return "release"
}

func releaseAddColumnIsTagAndSyncTags(x *xorm.Engine) error {
	if err := x.Sync2(new(ReleaseV39)); err != nil {
		return fmt.Errorf("Sync2: %v", err)
	}

	// For the sake of SQLite3, we can't use x.Iterate here.
	offset := 0
	pageSize := models.RepositoryListDefaultPageSize
	for {
		repos := make([]*models.Repository, 0, pageSize)
		if err := x.Table("repository").Cols("id", "name", "owner_id").Asc("id").Limit(pageSize, offset).Find(&repos); err != nil {
			return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
		}
		for _, repo := range repos {
			gitRepo, err := git.OpenRepository(repo.RepoPath())
			if err != nil {
				log.Warn("OpenRepository: %v", err)
				continue
			}

			if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
				log.Warn("SyncReleasesWithTags: %v", err)
			}
		}
		if len(repos) < pageSize {
			break
		}
		offset += pageSize
	}
	return nil
}