Bläddra i källkod

db: skip auto migrate for existing "version" table (#7057)

Joe Chen 2 år sedan
förälder
incheckning
4a3dc6c774
2 ändrade filer med 11 tillägg och 6 borttagningar
  1. 3 2
      internal/db/db.go
  2. 8 4
      internal/db/migrations/migrations.go

+ 3 - 2
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

+ 8 - 4
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{