diff options
author | techknowlogick <techknowlogick@gitea.com> | 2024-03-01 03:23:28 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-01 03:23:28 -0500 |
commit | cb52b17f92e2d2293f7c003649743464492bca48 (patch) | |
tree | 50351d737a406f4e9c2850c9918525998d09a787 /models/migrations | |
parent | e71eb8930a5d0f60874b038c223498b41ad65592 (diff) | |
download | gitea-cb52b17f92e2d2293f7c003649743464492bca48.tar.gz gitea-cb52b17f92e2d2293f7c003649743464492bca48.zip |
Add admin API route for managing user's badges (#23106)
Fix #22785
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/migrations')
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v1_22/v287.go | 46 | ||||
-rw-r--r-- | models/migrations/v1_22/v287_test.go | 57 |
3 files changed, 105 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index beb1f3bb96..516eb53f62 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -558,6 +558,8 @@ var migrations = []Migration{ NewMigration("Add PreviousDuration to ActionRun", v1_22.AddPreviousDurationToActionRun), // v286 -> v287 NewMigration("Add support for SHA256 git repositories", v1_22.AdjustDBForSha256), + // v287 -> v288 + NewMigration("Use Slug instead of ID for Badges", v1_22.UseSlugInsteadOfIDForBadges), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_22/v287.go b/models/migrations/v1_22/v287.go new file mode 100644 index 0000000000..c8b1593286 --- /dev/null +++ b/models/migrations/v1_22/v287.go @@ -0,0 +1,46 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "xorm.io/xorm" +) + +type BadgeUnique struct { + ID int64 `xorm:"pk autoincr"` + Slug string `xorm:"UNIQUE"` +} + +func (BadgeUnique) TableName() string { + return "badge" +} + +func UseSlugInsteadOfIDForBadges(x *xorm.Engine) error { + type Badge struct { + Slug string + } + + err := x.Sync(new(Badge)) + if err != nil { + return err + } + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + _, err = sess.Exec("UPDATE `badge` SET `slug` = `id` Where `slug` IS NULL") + if err != nil { + return err + } + + err = sess.Sync(new(BadgeUnique)) + if err != nil { + return err + } + + return sess.Commit() +} diff --git a/models/migrations/v1_22/v287_test.go b/models/migrations/v1_22/v287_test.go new file mode 100644 index 0000000000..19c7ae3b91 --- /dev/null +++ b/models/migrations/v1_22/v287_test.go @@ -0,0 +1,57 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "fmt" + "testing" + + "code.gitea.io/gitea/models/migrations/base" + + "github.com/stretchr/testify/assert" +) + +func Test_UpdateBadgeColName(t *testing.T) { + type Badge struct { + ID int64 `xorm:"pk autoincr"` + Description string + ImageURL string + } + + // Prepare and load the testing database + x, deferable := base.PrepareTestEnv(t, 0, new(BadgeUnique), new(Badge)) + defer deferable() + if x == nil || t.Failed() { + return + } + + oldBadges := []Badge{ + {ID: 1, Description: "Test Badge 1", ImageURL: "https://example.com/badge1.png"}, + {ID: 2, Description: "Test Badge 2", ImageURL: "https://example.com/badge2.png"}, + {ID: 3, Description: "Test Badge 3", ImageURL: "https://example.com/badge3.png"}, + } + + for _, badge := range oldBadges { + _, err := x.Insert(&badge) + assert.NoError(t, err) + } + + if err := UseSlugInsteadOfIDForBadges(x); err != nil { + assert.NoError(t, err) + return + } + + got := []BadgeUnique{} + if err := x.Table("badge").Asc("id").Find(&got); !assert.NoError(t, err) { + return + } + + for i, e := range oldBadges { + got := got[i] + assert.Equal(t, e.ID, got.ID) + assert.Equal(t, fmt.Sprintf("%d", e.ID), got.Slug) + } + + // TODO: check if badges have been updated +} |