v21_test.go 1.9 KB

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