gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_21
  4. import (
  5. "context"
  6. "errors"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "xorm.io/xorm"
  10. )
  11. func AddBranchTable(x *xorm.Engine) error {
  12. type Branch struct {
  13. ID int64
  14. RepoID int64 `xorm:"UNIQUE(s)"`
  15. Name string `xorm:"UNIQUE(s) NOT NULL"`
  16. CommitID string
  17. CommitMessage string `xorm:"TEXT"`
  18. PusherID int64
  19. IsDeleted bool `xorm:"index"`
  20. DeletedByID int64
  21. DeletedUnix timeutil.TimeStamp `xorm:"index"`
  22. CommitTime timeutil.TimeStamp // The commit
  23. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  24. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  25. }
  26. if err := x.Sync(new(Branch)); err != nil {
  27. return err
  28. }
  29. if exist, err := x.IsTableExist("deleted_branches"); err != nil {
  30. return err
  31. } else if !exist {
  32. return nil
  33. }
  34. type DeletedBranch struct {
  35. ID int64
  36. RepoID int64 `xorm:"index UNIQUE(s)"`
  37. Name string `xorm:"UNIQUE(s) NOT NULL"`
  38. Commit string
  39. DeletedByID int64
  40. DeletedUnix timeutil.TimeStamp
  41. }
  42. var adminUserID int64
  43. has, err := x.Table("user").
  44. Select("id").
  45. Where("is_admin=?", true).
  46. Asc("id"). // Reliably get the admin with the lowest ID.
  47. Get(&adminUserID)
  48. if err != nil {
  49. return err
  50. } else if !has {
  51. return errors.New("no admin user found")
  52. }
  53. branches := make([]Branch, 0, 100)
  54. if err := db.Iterate(context.Background(), nil, func(ctx context.Context, deletedBranch *DeletedBranch) error {
  55. branches = append(branches, Branch{
  56. RepoID: deletedBranch.RepoID,
  57. Name: deletedBranch.Name,
  58. CommitID: deletedBranch.Commit,
  59. PusherID: adminUserID,
  60. IsDeleted: true,
  61. DeletedByID: deletedBranch.DeletedByID,
  62. DeletedUnix: deletedBranch.DeletedUnix,
  63. })
  64. if len(branches) >= 100 {
  65. _, err := x.Insert(&branches)
  66. if err != nil {
  67. return err
  68. }
  69. branches = branches[:0]
  70. }
  71. return nil
  72. }); err != nil {
  73. return err
  74. }
  75. if len(branches) > 0 {
  76. if _, err := x.Insert(&branches); err != nil {
  77. return err
  78. }
  79. }
  80. return x.DropTables(new(DeletedBranch))
  81. }