Skip to content

Commit

Permalink
Add Primary Key to Topic and RepoTopic
Browse files Browse the repository at this point in the history
Add a primary key to Topic and RepoTopic tables

Fix #8920

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Sep 6, 2020
1 parent 1b9d507 commit 0288ae8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ var migrations = []Migration{
NewMigration("remove issue dependency comments who refer to non existing issues", purgeInvalidDependenciesComments),
// v149 -> v150
NewMigration("Add Created and Updated to Milestone table", addCreatedAndUpdatedToMilestones),
// v150 -> v151
NewMigration("add primary key to repo_topic", addPrimaryKeyToRepoTopic),
}

// GetCurrentDBVersion returns the current db version
Expand Down
39 changes: 39 additions & 0 deletions models/migrations/v150.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 The Gitea 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 (
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
)

func addPrimaryKeyToRepoTopic(x *xorm.Engine) error {
// Topic represents a topic of repositories
type Topic struct {
ID int64 `xorm:"pk autoincr"`
Name string `xorm:"UNIQUE VARCHAR(25)"`
RepoCount int
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}

// RepoTopic represents associated repositories and topics
type RepoTopic struct {
RepoID int64 `xorm:"pk"`
TopicID int64 `xorm:"pk"`
}

sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

recreateTable(sess, &Topic{})
recreateTable(sess, &RepoTopic{})

return sess.Commit()
}
6 changes: 3 additions & 3 deletions models/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var topicPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`)

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

// RepoTopic represents associated repositories and topics
type RepoTopic struct {
RepoID int64 `xorm:"UNIQUE(s)"`
TopicID int64 `xorm:"UNIQUE(s)"`
RepoID int64 `xorm:"pk"`
TopicID int64 `xorm:"pk"`
}

// ErrTopicNotExist represents an error that a topic is not exist
Expand Down

0 comments on commit 0288ae8

Please sign in to comment.