refactor: prepare to decouple the "model migration" package and "models" package (#38533)

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>
This commit is contained in:
wxiaoguang
2026-07-20 03:42:02 +00:00
committed by GitHub
co-authored by delvh
parent 775e3bdb34
commit fff32e9469
378 changed files with 708 additions and 721 deletions
+57
View File
@@ -0,0 +1,57 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_22
import (
"strconv"
"testing"
"gitea.dev/modelmigration/migrationtest"
"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 := migrationtest.PrepareTestEnv(t, 0, new(Badge))
defer deferable()
if x == nil || t.Failed() {
return
}
oldBadges := []*Badge{
{Description: "Test Badge 1", ImageURL: "https://example.com/badge1.png"},
{Description: "Test Badge 2", ImageURL: "https://example.com/badge2.png"},
{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+1] // 1 is in the badge.yml
assert.Equal(t, e.ID, got.ID)
assert.Equal(t, strconv.FormatInt(e.ID, 10), got.Slug)
}
// TODO: check if badges have been updated
}