瀏覽代碼

更新用户邮箱字段并改进错误处理

在数据库迁移中添加了PublicEmail和LocalEmail字段,并改进了错误处理方式,使用fmt.Errorf来格式化错误信息。同时,更新了版权声明中的许可证文件名。

备注:无需提交为上游补丁
SongZihuan 1 月之前
父節點
當前提交
54ea68eb6a
共有 4 個文件被更改,包括 32 次插入12 次删除
  1. 12 2
      internal/database/migrations/v23.go
  2. 16 6
      internal/database/migrations/v24.go
  3. 3 3
      internal/database/migrations/v25.go
  4. 1 1
      public/embed.go

+ 12 - 2
internal/database/migrations/v23.go

@@ -1,11 +1,16 @@
 package migrations
 
 import (
+	"fmt"
 	"gorm.io/gorm"
 )
 
 func addUserPublicEmail(db *gorm.DB) error {
 	type User struct {
+		PublicEmail string // 不能使用NOT NULL
+	}
+
+	type UserNotNull struct {
 		PublicEmail string `xorm:"NOT NULL" gorm:"not null"`
 	}
 
@@ -16,12 +21,17 @@ func addUserPublicEmail(db *gorm.DB) error {
 	return db.Transaction(func(tx *gorm.DB) error {
 		err := tx.Migrator().AddColumn(&User{}, "PublicEmail")
 		if err != nil {
-			return err
+			return fmt.Errorf("add column user.public_email error: %s", err.Error())
 		}
 
 		err = tx.Exec("UPDATE `user` SET `public_email` = `email` WHERE `public_email` = '' AND `type` = 0").Error
 		if err != nil {
-			return err
+			return fmt.Errorf("update public_email error: %s", err.Error())
+		}
+
+		err = tx.Debug().Migrator().AlterColumn(&UserNotNull{}, "PublicEmail")
+		if err != nil {
+			return fmt.Errorf("alter column user.public_email error: %s", err.Error())
 		}
 
 		return nil

+ 16 - 6
internal/database/migrations/v24.go

@@ -1,15 +1,20 @@
 package migrations
 
 import (
-	"github.com/pkg/errors"
+	"fmt"
 	gouuid "github.com/satori/go.uuid"
 	"gorm.io/gorm"
 )
 
 func addUserLocalEmail(db *gorm.DB) error {
 	type User struct {
-		ID         int64  `gorm:"primaryKey"`
-		LocalEmail string `xorm:"NOT NULL" gorm:"not null"`
+		ID         int64 `gorm:"primaryKey"`
+		LocalEmail string
+	}
+
+	type UserNotNULL struct {
+		ID         int64 `gorm:"primaryKey"`
+		LocalEmail string
 	}
 
 	if db.Migrator().HasColumn(&User{}, "LocalEmail") {
@@ -19,7 +24,7 @@ func addUserLocalEmail(db *gorm.DB) error {
 	return db.Transaction(func(tx *gorm.DB) error {
 		err := tx.Migrator().AddColumn(&User{}, "LocalEmail")
 		if err != nil {
-			return err
+			return fmt.Errorf("add column user.local_email error: %s", err.Error())
 		}
 
 		const limit = 100
@@ -27,14 +32,14 @@ func addUserLocalEmail(db *gorm.DB) error {
 			var res []User
 			err := tx.Table("user").Where("type = ?", 0).Where("local_email = ''").Limit(limit).Find(&res).Error
 			if err != nil {
-				return errors.Wrap(err, "query user")
+				return fmt.Errorf("query user error: %s", err.Error())
 			}
 
 			for _, r := range res {
 				r.LocalEmail = gouuid.NewV4().String() + "@fake.localhost"
 				err = tx.Save(&r).Error
 				if err != nil {
-					return errors.Wrap(err, "save user")
+					return fmt.Errorf("save column user.local_email error: %s", err)
 				}
 			}
 
@@ -43,6 +48,11 @@ func addUserLocalEmail(db *gorm.DB) error {
 			}
 		}
 
+		err = tx.Migrator().AlterColumn(&User{}, "LocalEmail")
+		if err != nil {
+			return fmt.Errorf("alter column user.local_email error: %s", err.Error())
+		}
+
 		return nil
 	})
 }

+ 3 - 3
internal/database/migrations/v25.go

@@ -1,7 +1,7 @@
 package migrations
 
 import (
-	"github.com/pkg/errors"
+	"fmt"
 	"gorm.io/gorm"
 )
 
@@ -26,7 +26,7 @@ func insertUserPrimaryEmail(db *gorm.DB) error {
 			var res []User
 			err := tx.Table("user").Where("type = ?", 0).Offset(offset).Limit(limit).Find(&res).Error
 			if err != nil {
-				return errors.Wrap(err, "query user")
+				return fmt.Errorf("query user error: %s", err.Error())
 			}
 
 			for _, r := range res {
@@ -37,7 +37,7 @@ func insertUserPrimaryEmail(db *gorm.DB) error {
 				}
 				err := tx.Table("email_address").Where("uid = ? AND email = ?", record.UserID, record.Email).FirstOrCreate(record).Error
 				if err != nil {
-					return errors.Wrap(err, "insert email")
+					return fmt.Errorf("insert email error: %s", err.Error())
 				}
 			}
 

+ 1 - 1
public/embed.go

@@ -1,6 +1,6 @@
 // Copyright 2022 The Gogs Authors. All rights reserved.
 // Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
+// license that can be found in the LICENSE.gogs file.
 
 package public