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.

v195_test.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_16 //nolint
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/migrations/base"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_AddTableCommitStatusIndex(t *testing.T) {
  10. // Create the models used in the migration
  11. type CommitStatus struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  14. RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  15. SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
  16. }
  17. // Prepare and load the testing database
  18. x, deferable := base.PrepareTestEnv(t, 0, new(CommitStatus))
  19. if x == nil || t.Failed() {
  20. defer deferable()
  21. return
  22. }
  23. defer deferable()
  24. // Run the migration
  25. if err := AddTableCommitStatusIndex(x); err != nil {
  26. assert.NoError(t, err)
  27. return
  28. }
  29. type CommitStatusIndex struct {
  30. ID int64
  31. RepoID int64 `xorm:"unique(repo_sha)"`
  32. SHA string `xorm:"unique(repo_sha)"`
  33. MaxIndex int64 `xorm:"index"`
  34. }
  35. start := 0
  36. const batchSize = 1000
  37. for {
  38. indexes := make([]CommitStatusIndex, 0, batchSize)
  39. err := x.Table("commit_status_index").Limit(batchSize, start).Find(&indexes)
  40. assert.NoError(t, err)
  41. for _, idx := range indexes {
  42. var maxIndex int
  43. has, err := x.SQL("SELECT max(`index`) FROM commit_status WHERE repo_id = ? AND sha = ?", idx.RepoID, idx.SHA).Get(&maxIndex)
  44. assert.NoError(t, err)
  45. assert.True(t, has)
  46. assert.EqualValues(t, maxIndex, idx.MaxIndex)
  47. }
  48. if len(indexes) < batchSize {
  49. break
  50. }
  51. start += len(indexes)
  52. }
  53. }