Skip to content

Commit eb41075

Browse files
zeripath6543
authored andcommitted
Set the name Mapper in migrations (go-gitea#14526)
Migrations currently uses the default Xorm mapper which is not the same as the mapper Gitea actually uses. This means that there is a difference between the struct parsing and mapping to database tables in migrations as compared to normal Sync2. This was the cause for the catastrophic problem in v168 - untagged fields are not mapped in the same way in migrations as compared to outside of migrations. This is also likely the cause of some weird subtle failures in other migrations as any untagged field may not be being mapped exactly the same way. This PR suggests that we ensure that the mapper is set at the start of the migrations code - but also enforces a strict clean mapper between each migration. Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 9569607 commit eb41075

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

models/migrations/migrations.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/setting"
1717

1818
"xorm.io/xorm"
19+
"xorm.io/xorm/names"
1920
)
2021

2122
const minDBVersion = 70 // Gitea 1.5.3
@@ -296,6 +297,8 @@ func EnsureUpToDate(x *xorm.Engine) error {
296297

297298
// Migrate database to current version
298299
func Migrate(x *xorm.Engine) error {
300+
// Set a new clean the default mapper to GonicMapper as that is the default for Gitea.
301+
x.SetMapper(names.GonicMapper{})
299302
if err := x.Sync(new(Version)); err != nil {
300303
return fmt.Errorf("sync: %v", err)
301304
}
@@ -334,6 +337,8 @@ Please try upgrading to a lower version first (suggested v1.6.4), then upgrade t
334337
// Migrate
335338
for i, m := range migrations[v-minDBVersion:] {
336339
log.Info("Migration[%d]: %s", v+int64(i), m.Description())
340+
// Reset the mapper between each migration - migrations are not supposed to depend on each other
341+
x.SetMapper(names.GonicMapper{})
337342
if err = m.Migrate(x); err != nil {
338343
return fmt.Errorf("do migrate: %v", err)
339344
}

0 commit comments

Comments
 (0)