v21_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 The Gogs 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 and LICENSE.gogs file.
  4. // Copyright 2025 Huan-Gogs Authors. All rights reserved.
  5. // Use of this source code is governed by a MIT-style
  6. // license that can be found in the LICENSE file.
  7. package migrations
  8. import (
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "gogs.io/gogs/internal/dbtest"
  13. )
  14. type actionPreV21 struct {
  15. ID int64 `gorm:"primaryKey"`
  16. UserID int64
  17. OpType int
  18. ActUserID int64
  19. ActUserName string
  20. RepoID int64 `gorm:"index"`
  21. RepoUserName string
  22. RepoName string
  23. RefName string
  24. IsPrivate bool `gorm:"not null;default:FALSE"`
  25. Content string
  26. CreatedUnix int64
  27. }
  28. func (*actionPreV21) TableName() string {
  29. return "action"
  30. }
  31. type actionV21 struct {
  32. ID int64 `gorm:"primaryKey"`
  33. UserID int64 `gorm:"index"`
  34. OpType int
  35. ActUserID int64
  36. ActUserName string
  37. RepoID int64 `gorm:"index"`
  38. RepoUserName string
  39. RepoName string
  40. RefName string
  41. IsPrivate bool `gorm:"not null;default:FALSE"`
  42. Content string
  43. CreatedUnix int64
  44. }
  45. func (*actionV21) TableName() string {
  46. return "action"
  47. }
  48. func TestAddIndexToActionUserID(t *testing.T) {
  49. if testing.Short() {
  50. t.Skip()
  51. }
  52. t.Parallel()
  53. db := dbtest.NewDB(t, "addIndexToActionUserID", new(actionPreV21))
  54. err := db.Create(
  55. &actionPreV21{
  56. ID: 1,
  57. UserID: 1,
  58. OpType: 1,
  59. ActUserID: 1,
  60. ActUserName: "alice",
  61. RepoID: 1,
  62. RepoUserName: "alice",
  63. RepoName: "example",
  64. RefName: "main",
  65. IsPrivate: false,
  66. CreatedUnix: db.NowFunc().Unix(),
  67. },
  68. ).Error
  69. require.NoError(t, err)
  70. assert.False(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
  71. err = addIndexToActionUserID(db)
  72. require.NoError(t, err)
  73. assert.True(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
  74. // Re-run should be skipped
  75. err = addIndexToActionUserID(db)
  76. require.Equal(t, errMigrationSkipped, err)
  77. }