Browse Source

Add primary key and index to external login user table (#1656)

Lauris BH 2 years ago
parent
commit
98548c83d3
3 changed files with 43 additions and 3 deletions
  1. 3 3
      models/external_login_user.go
  2. 2 0
      models/migrations/migrations.go
  3. 38 0
      models/migrations/v30.go

+ 3 - 3
models/external_login_user.go

@@ -8,9 +8,9 @@ import "github.com/markbates/goth"
8 8
 
9 9
 // ExternalLoginUser makes the connecting between some existing user and additional external login sources
10 10
 type ExternalLoginUser struct {
11
-	ExternalID    string `xorm:"NOT NULL"`
12
-	UserID        int64  `xorm:"NOT NULL"`
13
-	LoginSourceID int64  `xorm:"NOT NULL"`
11
+	ExternalID    string `xorm:"pk NOT NULL"`
12
+	UserID        int64  `xorm:"INDEX NOT NULL"`
13
+	LoginSourceID int64  `xorm:"pk NOT NULL"`
14 14
 }
15 15
 
16 16
 // GetExternalLogin checks if a externalID in loginSourceID scope already exists

+ 2 - 0
models/migrations/migrations.go

@@ -108,6 +108,8 @@ var migrations = []Migration{
108 108
 	NewMigration("add field for repo size", addRepoSize),
109 109
 	// v29 -> v30
110 110
 	NewMigration("add commit status table", addCommitStatus),
111
+	// v30 -> 31
112
+	NewMigration("add primary key to external login user", addExternalLoginUserPK),
111 113
 }
112 114
 
113 115
 // Migrate database to current version

+ 38 - 0
models/migrations/v30.go

@@ -0,0 +1,38 @@
1
+// Copyright 2017 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
+
5
+package migrations
6
+
7
+import (
8
+	"fmt"
9
+
10
+	"github.com/go-xorm/xorm"
11
+)
12
+
13
+func addExternalLoginUserPK(x *xorm.Engine) error {
14
+	// ExternalLoginUser see models/external_login_user.go
15
+	type ExternalLoginUser struct {
16
+		ExternalID    string `xorm:"pk NOT NULL"`
17
+		UserID        int64  `xorm:"INDEX NOT NULL"`
18
+		LoginSourceID int64  `xorm:"pk NOT NULL"`
19
+	}
20
+
21
+	extlogins := make([]*ExternalLoginUser, 0, 6)
22
+	if err := x.Find(&extlogins); err != nil {
23
+		return fmt.Errorf("Find: %v", err)
24
+	}
25
+
26
+	if err := x.DropTables(new(ExternalLoginUser)); err != nil {
27
+		return fmt.Errorf("DropTables: %v", err)
28
+	}
29
+
30
+	if err := x.Sync2(new(ExternalLoginUser)); err != nil {
31
+		return fmt.Errorf("Sync2: %v", err)
32
+	}
33
+
34
+	if _, err := x.Insert(extlogins); err != nil {
35
+		return fmt.Errorf("Insert: %v", err)
36
+	}
37
+	return nil
38
+}