aboutsummaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-06-14 15:47:11 +0800
committerGitHub <noreply@github.com>2022-06-14 15:47:11 +0800
commit4a3dc6c774c96cba6f1e5236b805bffe18f04124 (patch)
tree7f8569c51bb09d56690c2824f539e6cf7a505bd3 /internal/db
parentc0db4a7f1b3d363afd5f763f7e448dd35e3eec98 (diff)
db: skip auto migrate for existing "version" table (#7057)
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/db.go5
-rw-r--r--internal/db/migrations/migrations.go12
2 files changed, 11 insertions, 6 deletions
diff --git a/internal/db/db.go b/internal/db/db.go
index 9845bda7..80e2dfc5 100644
--- a/internal/db/db.go
+++ b/internal/db/db.go
@@ -96,8 +96,9 @@ func Init(w logger.Writer) (*gorm.DB, error) {
panic("unreachable")
}
- // 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.
+ // 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.Migrator().HasTable(table) {
continue
diff --git a/internal/db/migrations/migrations.go b/internal/db/migrations/migrations.go
index 4f93b9c5..9d22681d 100644
--- a/internal/db/migrations/migrations.go
+++ b/internal/db/migrations/migrations.go
@@ -58,13 +58,17 @@ var migrations = []Migration{
// Migrate migrates the database schema and/or data to the current version.
func Migrate(db *gorm.DB) error {
- err := db.AutoMigrate(new(Version))
- if err != nil {
- return errors.Wrap(err, `auto migrate "version" table`)
+ // NOTE: GORM has problem migrating tables that happen to have columns with the
+ // same name, see https://github.com/gogs/gogs/issues/7056.
+ if !db.Migrator().HasTable(new(Version)) {
+ err := db.AutoMigrate(new(Version))
+ if err != nil {
+ return errors.Wrap(err, `auto migrate "version" table`)
+ }
}
var current Version
- err = db.Where("id = ?", 1).First(&current).Error
+ err := db.Where("id = ?", 1).First(&current).Error
if err == gorm.ErrRecordNotFound {
err = db.Create(
&Version{