Skip to content

Commit 0288ae8

Browse files
committed
Add Primary Key to Topic and RepoTopic
Add a primary key to Topic and RepoTopic tables Fix #8920 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 1b9d507 commit 0288ae8

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ var migrations = []Migration{
233233
NewMigration("remove issue dependency comments who refer to non existing issues", purgeInvalidDependenciesComments),
234234
// v149 -> v150
235235
NewMigration("Add Created and Updated to Milestone table", addCreatedAndUpdatedToMilestones),
236+
// v150 -> v151
237+
NewMigration("add primary key to repo_topic", addPrimaryKeyToRepoTopic),
236238
}
237239

238240
// GetCurrentDBVersion returns the current db version

models/migrations/v150.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 The Gitea 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+
"code.gitea.io/gitea/modules/timeutil"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addPrimaryKeyToRepoTopic(x *xorm.Engine) error {
14+
// Topic represents a topic of repositories
15+
type Topic struct {
16+
ID int64 `xorm:"pk autoincr"`
17+
Name string `xorm:"UNIQUE VARCHAR(25)"`
18+
RepoCount int
19+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
20+
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
21+
}
22+
23+
// RepoTopic represents associated repositories and topics
24+
type RepoTopic struct {
25+
RepoID int64 `xorm:"pk"`
26+
TopicID int64 `xorm:"pk"`
27+
}
28+
29+
sess := x.NewSession()
30+
defer sess.Close()
31+
if err := sess.Begin(); err != nil {
32+
return err
33+
}
34+
35+
recreateTable(sess, &Topic{})
36+
recreateTable(sess, &RepoTopic{})
37+
38+
return sess.Commit()
39+
}

models/topic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var topicPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`)
2525

2626
// Topic represents a topic of repositories
2727
type Topic struct {
28-
ID int64
28+
ID int64 `xorm:"pk autoincr"`
2929
Name string `xorm:"UNIQUE VARCHAR(25)"`
3030
RepoCount int
3131
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
@@ -34,8 +34,8 @@ type Topic struct {
3434

3535
// RepoTopic represents associated repositories and topics
3636
type RepoTopic struct {
37-
RepoID int64 `xorm:"UNIQUE(s)"`
38-
TopicID int64 `xorm:"UNIQUE(s)"`
37+
RepoID int64 `xorm:"pk"`
38+
TopicID int64 `xorm:"pk"`
3939
}
4040

4141
// ErrTopicNotExist represents an error that a topic is not exist

0 commit comments

Comments
 (0)