aboutsummaryrefslogtreecommitdiff
path: root/internal/db/migrations/v21_test.go
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-06-25 18:07:39 +0800
committerGitHub <noreply@github.com>2022-06-25 18:07:39 +0800
commit083c3ee659c6c5542687f3bafae68cbc24dbc90f (patch)
tree0103bf3b5c5ebfccd368a7cb6a425a521fd669d9 /internal/db/migrations/v21_test.go
parent9df4e3ae3c555a86f691f0d78a43834842e77d8b (diff)
db: refactor "action" table to use GORM (#7054)
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Diffstat (limited to 'internal/db/migrations/v21_test.go')
-rw-r--r--internal/db/migrations/v21_test.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/internal/db/migrations/v21_test.go b/internal/db/migrations/v21_test.go
new file mode 100644
index 00000000..d11c47c5
--- /dev/null
+++ b/internal/db/migrations/v21_test.go
@@ -0,0 +1,82 @@
+// Copyright 2022 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "gogs.io/gogs/internal/dbtest"
+)
+
+type actionPreV21 struct {
+ ID int64 `gorm:"primaryKey"`
+ UserID int64
+ OpType int
+ ActUserID int64
+ ActUserName string
+ RepoID int64 `gorm:"index"`
+ RepoUserName string
+ RepoName string
+ RefName string
+ IsPrivate bool `gorm:"not null;default:FALSE"`
+ Content string
+ CreatedUnix int64
+}
+
+func (*actionPreV21) TableName() string {
+ return "action"
+}
+
+type actionV21 struct {
+ ID int64 `gorm:"primaryKey"`
+ UserID int64 `gorm:"index"`
+ OpType int
+ ActUserID int64
+ ActUserName string
+ RepoID int64 `gorm:"index"`
+ RepoUserName string
+ RepoName string
+ RefName string
+ IsPrivate bool `gorm:"not null;default:FALSE"`
+ Content string
+ CreatedUnix int64
+}
+
+func (*actionV21) TableName() string {
+ return "action"
+}
+
+func TestAddIndexToActionUserID(t *testing.T) {
+ if testing.Short() {
+ t.Skip()
+ }
+ t.Parallel()
+
+ db := dbtest.NewDB(t, "addIndexToActionUserID", new(actionPreV21))
+ err := db.Create(
+ &actionPreV21{
+ ID: 1,
+ UserID: 1,
+ OpType: 1,
+ ActUserID: 1,
+ ActUserName: "alice",
+ RepoID: 1,
+ RepoUserName: "alice",
+ RepoName: "example",
+ RefName: "main",
+ IsPrivate: false,
+ CreatedUnix: db.NowFunc().Unix(),
+ },
+ ).Error
+ require.NoError(t, err)
+ assert.False(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
+
+ err = addIndexToActionUserID(db)
+ require.NoError(t, err)
+ assert.True(t, db.Migrator().HasIndex(&actionV21{}, "UserID"))
+}