Skip to content

Commit

Permalink
Set the name Mapper in migrations (#14526)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
zeripath authored Jan 30, 2021
1 parent eea4197 commit fcfbab9
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/setting"

"xorm.io/xorm"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)

Expand Down Expand Up @@ -333,6 +334,8 @@ func EnsureUpToDate(x *xorm.Engine) error {

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

0 comments on commit fcfbab9

Please sign in to comment.