db: only use `AutoMigrate` to create new tables (#6092)

* Only use AutoMigrate to create new tables

* Revert models.go
pull/6094/head
ᴜɴᴋɴᴡᴏɴ 2020-04-11 23:45:06 +08:00 committed by GitHub
parent 41f56ad05d
commit ae107b2e6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 3 deletions

View File

@ -158,9 +158,19 @@ func Init() error {
conf.UseSQLite3 = true
}
err = db.AutoMigrate(tables...).Error
if err != nil {
return errors.Wrap(err, "migrate schemes")
// NOTE: GORM has problem detecting existing columns, see https://github.com/gogs/gogs/issues/6091.
// Therefore only use it to create new tables, and do customized migration with future changes.
for _, table := range tables {
if db.HasTable(table) {
continue
}
name := strings.TrimPrefix(fmt.Sprintf("%T", table), "*db.")
err = db.AutoMigrate(table).Error
if err != nil {
return errors.Wrapf(err, "auto migrate %q", name)
}
log.Trace("Auto migrated %q", name)
}
gorm.NowFunc = func() time.Time {