瀏覽代碼

Fix legacy mall user lookup on prod schema

abiao 1 周之前
父節點
當前提交
678ec66a3d
共有 2 個文件被更改,包括 13 次插入5 次删除
  1. 1 1
      go/gopath/src/fohow.com/apps/init.go
  2. 12 4
      go/gopath/src/fohow.com/apps/models/user_model/user.go

+ 1 - 1
go/gopath/src/fohow.com/apps/init.go

@@ -254,7 +254,7 @@ func (self *BaseController) GetCurrentUser(useCache bool) *user_model.User {
 	}
 	u := user_model.GetUserById(id, useCache)
 	if u == nil {
-		beego.BeeLogger.Error("User Not Found, uid=%d", id)
+		beego.BeeLogger.Error("User Not Found, wx_uid=%d, uid=%d", wxUser.Id, id)
 	}
 	return u
 }

+ 12 - 4
go/gopath/src/fohow.com/apps/models/user_model/user.go

@@ -37,8 +37,8 @@ type User struct {
 	Province    string    `orm:"column(province);null"                          json:"-"`        // varchar(20)
 	City        string    `orm:"column(city);null"                              json:"-"`        // varchar(20)
 	Sex         int64     `orm:"column(sex);null"                               json:"sex"`      // int(11)
-	Race        string    `orm:"column(race);null"                              json:"race"`
-	Occupation  string    `orm:"column(occupation);null"                        json:"occupation"`
+	Race        string    `orm:"-"                                              json:"race"`
+	Occupation  string    `orm:"-"                                              json:"occupation"`
 	Head        string    `orm:"column(head)"     json:"head"`
 	Birthday    time.Time `orm:"column(birthday);null;type(datetime)"          json:"birthday"`
 	InviteId    int64     `orm:"column(invite_id);null"                         json:"invite_id"` // int(11)
@@ -149,8 +149,16 @@ func GetUserById(id int64, useCache bool) *User {
 	o := orm.NewOrm()
 	err := o.QueryTable(usr).Filter("id", id).Limit(1).One(usr)
 	if err != nil {
-		beego.Debug("GetByUid is not found err=[%s], id=%d", err, id)
-		return nil
+		// Fall back to raw SQL when ORM metadata/query building drifts from the runtime schema.
+		rawUser := new(User)
+		rawErr := o.Raw("SELECT * FROM users WHERE id = ? LIMIT 1", id).QueryRow(rawUser)
+		if rawErr != nil {
+			beego.BeeLogger.Error("GetByUid failed, id=%d, query_err=[%s], raw_err=[%s]", id, err, rawErr)
+			return nil
+		}
+		cache.Cache.Put(k, rawUser, 5*time.Minute)
+		beego.BeeLogger.Warn("GetByUid recovered via raw SQL, id=%d, query_err=[%s]", id, err)
+		return rawUser
 	} else {
 		cache.Cache.Put(k, usr, 5*time.Minute)
 		return usr