|
| 1 | +// Copyright 2021 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 | + |
| 5 | +package migrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_addIssueResourceIndexTable(t *testing.T) { |
| 14 | + // Create the models used in the migration |
| 15 | + type Issue struct { |
| 16 | + ID int64 `xorm:"pk autoincr"` |
| 17 | + RepoID int64 `xorm:"UNIQUE(s)"` |
| 18 | + Index int64 `xorm:"UNIQUE(s)"` |
| 19 | + } |
| 20 | + |
| 21 | + // Prepare and load the testing database |
| 22 | + x, deferable := prepareTestEnv(t, 0, new(Issue)) |
| 23 | + if x == nil || t.Failed() { |
| 24 | + defer deferable() |
| 25 | + return |
| 26 | + } |
| 27 | + defer deferable() |
| 28 | + |
| 29 | + // Run the migration |
| 30 | + if err := addIssueResourceIndexTable(x); err != nil { |
| 31 | + assert.NoError(t, err) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + type ResourceIndex struct { |
| 36 | + GroupID int64 `xorm:"index unique(s)"` |
| 37 | + MaxIndex int64 `xorm:"index unique(s)"` |
| 38 | + } |
| 39 | + |
| 40 | + var start = 0 |
| 41 | + const batchSize = 1000 |
| 42 | + for { |
| 43 | + var indexes = make([]ResourceIndex, 0, batchSize) |
| 44 | + err := x.Table("issue_index").Limit(batchSize, start).Find(&indexes) |
| 45 | + assert.NoError(t, err) |
| 46 | + |
| 47 | + for _, idx := range indexes { |
| 48 | + var maxIndex int |
| 49 | + has, err := x.SQL("SELECT max(`index`) FROM issue WHERE repo_id = ?", idx.GroupID).Get(&maxIndex) |
| 50 | + assert.NoError(t, err) |
| 51 | + assert.True(t, has) |
| 52 | + assert.EqualValues(t, maxIndex, idx.MaxIndex) |
| 53 | + } |
| 54 | + if len(indexes) < batchSize { |
| 55 | + break |
| 56 | + } |
| 57 | + start += len(indexes) |
| 58 | + } |
| 59 | +} |
0 commit comments