aboutsummaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-04-11 23:45:06 +0800
committerGitHub <noreply@github.com>2020-04-11 23:45:06 +0800
commitae107b2e6eb10162257b380f2e23dd94d517ff2a (patch)
tree4281b0297a8f26db631b92607e049feea86ca2ee /internal/db
parent41f56ad05d12520bb90f91889fa979465d0b3d6b (diff)
db: only use `AutoMigrate` to create new tables (#6092)
* Only use AutoMigrate to create new tables * Revert models.go
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/db.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/internal/db/db.go b/internal/db/db.go
index 77d78f53..ec2dd6ec 100644
--- a/internal/db/db.go
+++ b/internal/db/db.go
@@ -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 {