You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB

  1. // Copyright 2019 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. package migrations
  5. import (
  6. "crypto/sha1"
  7. "fmt"
  8. "xorm.io/xorm"
  9. )
  10. func hashContext(context string) string {
  11. return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
  12. }
  13. func addCommitStatusContext(x *xorm.Engine) error {
  14. type CommitStatus struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. ContextHash string `xorm:"char(40) index"`
  17. Context string `xorm:"TEXT"`
  18. }
  19. if err := x.Sync2(new(CommitStatus)); err != nil {
  20. return err
  21. }
  22. sess := x.NewSession()
  23. defer sess.Close()
  24. var start = 0
  25. for {
  26. var statuses = make([]*CommitStatus, 0, 100)
  27. err := sess.OrderBy("id").Limit(100, start).Find(&statuses)
  28. if err != nil {
  29. return err
  30. }
  31. if len(statuses) == 0 {
  32. break
  33. }
  34. if err = sess.Begin(); err != nil {
  35. return err
  36. }
  37. for _, status := range statuses {
  38. status.ContextHash = hashContext(status.Context)
  39. if _, err := sess.ID(status.ID).Cols("context_hash").Update(status); err != nil {
  40. return err
  41. }
  42. }
  43. if err := sess.Commit(); err != nil {
  44. return err
  45. }
  46. if len(statuses) < 100 {
  47. break
  48. }
  49. start += len(statuses)
  50. }
  51. return nil
  52. }