gitea源码

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