code.gitea.io/gitea@v1.21.7/models/migrations/v1_16/v195.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package v1_16 //nolint 5 6 import ( 7 "fmt" 8 9 "xorm.io/xorm" 10 ) 11 12 func AddTableCommitStatusIndex(x *xorm.Engine) error { 13 // CommitStatusIndex represents a table for commit status index 14 type CommitStatusIndex struct { 15 ID int64 16 RepoID int64 `xorm:"unique(repo_sha)"` 17 SHA string `xorm:"unique(repo_sha)"` 18 MaxIndex int64 `xorm:"index"` 19 } 20 21 if err := x.Sync(new(CommitStatusIndex)); err != nil { 22 return fmt.Errorf("Sync: %w", err) 23 } 24 25 sess := x.NewSession() 26 defer sess.Close() 27 28 if err := sess.Begin(); err != nil { 29 return err 30 } 31 32 // Remove data we're goint to rebuild 33 if _, err := sess.Table("commit_status_index").Where("1=1").Delete(&CommitStatusIndex{}); err != nil { 34 return err 35 } 36 37 // Create current data for all repositories with issues and PRs 38 if _, err := sess.Exec("INSERT INTO commit_status_index (repo_id, sha, max_index) " + 39 "SELECT max_data.repo_id, max_data.sha, max_data.max_index " + 40 "FROM ( SELECT commit_status.repo_id AS repo_id, commit_status.sha AS sha, max(commit_status.`index`) AS max_index " + 41 "FROM commit_status GROUP BY commit_status.repo_id, commit_status.sha) AS max_data"); err != nil { 42 return err 43 } 44 45 return sess.Commit() 46 }