mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-23 11:28:12 +02:00
Migrations should never use model structs directly, because the model structs can be different in different releases. e.g. if one migration uses "User" model, it works in the early releases, then one day, when the User model changes, the migration breaks because it will use the new (incorrect) User model, it should only use the old User model. The same to "modules/structs". --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: delvh <dev.lh@web.de>
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_26
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gitea.dev/modelmigration/base"
|
|
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
type UserBadge struct { //revive:disable-line:exported
|
|
ID int64 `xorm:"pk autoincr"`
|
|
BadgeID int64
|
|
UserID int64
|
|
}
|
|
|
|
// TableIndices implements xorm's TableIndices interface
|
|
func (n *UserBadge) TableIndices() []*schemas.Index {
|
|
indices := make([]*schemas.Index, 0, 1)
|
|
ubUnique := schemas.NewIndex("unique_user_badge", schemas.UniqueType)
|
|
ubUnique.AddColumn("user_id", "badge_id")
|
|
indices = append(indices, ubUnique)
|
|
return indices
|
|
}
|
|
|
|
// AddUniqueIndexForUserBadge adds a compound unique indexes for user badge table
|
|
// and it replaces an old index on user_id
|
|
func AddUniqueIndexForUserBadge(x base.EngineMigration) error {
|
|
// remove possible duplicated records in table user_badge
|
|
type result struct {
|
|
UserID int64
|
|
BadgeID int64
|
|
Cnt int
|
|
}
|
|
var results []result
|
|
if err := x.Select("user_id, badge_id, count(*) as cnt").
|
|
Table("user_badge").
|
|
GroupBy("user_id, badge_id").
|
|
Having("count(*) > 1").
|
|
Find(&results); err != nil {
|
|
return err
|
|
}
|
|
for _, r := range results {
|
|
if x.Dialect().URI().DBType == schemas.MSSQL {
|
|
if _, err := x.Exec(fmt.Sprintf("delete from user_badge where id in (SELECT top %d id FROM user_badge WHERE user_id = ? and badge_id = ?)", r.Cnt-1), r.UserID, r.BadgeID); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
var ids []int64
|
|
if err := x.SQL("SELECT id FROM user_badge WHERE user_id = ? and badge_id = ? limit ?", r.UserID, r.BadgeID, r.Cnt-1).Find(&ids); err != nil {
|
|
return err
|
|
}
|
|
if _, err := x.Table("user_badge").In("id", ids).Delete(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return x.Sync(new(UserBadge))
|
|
}
|