1
0

v21_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package migrations
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "gogs.io/gogs/internal/dbtest"
  7. )
  8. type actionPreV21 struct {
  9. ID int64 `gorm:"primaryKey"`
  10. UserID int64
  11. OpType int
  12. ActUserID int64
  13. ActUserName string
  14. RepoID int64 `gorm:"index"`
  15. RepoUserName string
  16. RepoName string
  17. RefName string
  18. IsPrivate bool `gorm:"not null;default:FALSE"`
  19. Content string
  20. CreatedUnix int64
  21. }
  22. func (*actionPreV21) TableName() string {
  23. return "action"
  24. }
  25. type actionV21 struct {
  26. ID int64 `gorm:"primaryKey"`
  27. UserID int64 `gorm:"index"`
  28. OpType int
  29. ActUserID int64
  30. ActUserName string
  31. RepoID int64 `gorm:"index"`
  32. RepoUserName string
  33. RepoName string
  34. RefName string
  35. IsPrivate bool `gorm:"not null;default:FALSE"`
  36. Content string
  37. CreatedUnix int64
  38. }
  39. func (*actionV21) TableName() string {
  40. return "action"
  41. }
  42. func TestAddIndexToActionUserID(t *testing.T) {
  43. if testing.Short() {
  44. t.Skip()
  45. }
  46. t.Parallel()
  47. db := dbtest.NewDB(t, "addIndexToActionUserID", new(actionPreV21))
  48. err := db.Create(
  49. &actionPreV21{
  50. ID: 1,
  51. UserID: 1,
  52. OpType: 1,
  53. ActUserID: 1,
  54. ActUserName: "alice",
  55. RepoID: 1,
  56. RepoUserName: "alice",
  57. RepoName: "example",
  58. RefName: "main",
  59. IsPrivate: false,
  60. CreatedUnix: db.NowFunc().Unix(),
  61. },
  62. ).Error
  63. require.NoError(t, err)
  64. assert.False(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
  65. err = addIndexToActionUserID(db)
  66. require.NoError(t, err)
  67. assert.True(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
  68. // Re-run should be skipped
  69. err = addIndexToActionUserID(db)
  70. require.Equal(t, errMigrationSkipped, err)
  71. }