Finish db update sync.

This commit is contained in:
Yang Luo 2021-05-09 15:44:12 +08:00
parent e2f6efd1ab
commit 6e5aa2bc40
4 changed files with 88 additions and 9 deletions

View File

@ -105,6 +105,15 @@ func UpdateUser(id string, user *User) bool {
return affected != 0
}
func UpdateUserForOriginal(user *User) bool {
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols("displayName", "password", "phone", "avatar", "isForbidden").Update(user)
if err != nil {
panic(err)
}
return affected != 0
}
func AddUser(user *User) bool {
user.Id = util.GenerateId()
user.UpdateUserHash()

View File

@ -47,29 +47,80 @@ func createUserFromOriginalUser(originalUser *User) *object.User {
return user
}
func createOriginalUserFromUser(user *object.User) *User {
deleted := 0
if user.IsForbidden {
deleted = 1
}
originalUser := &User{
Id: util.ParseInt(user.Id),
Name: user.DisplayName,
Password: user.Password,
Cellphone: user.Phone,
Avatar: user.Avatar,
Deleted: deleted,
}
return originalUser
}
func syncUsers() {
fmt.Printf("Running syncUsers()..\n")
users, userMap := getUserMap()
oUsers, _ := getUserMapOriginal()
oUsers, oUserMap := getUserMapOriginal()
fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers))
newUsers := []*object.User{}
for _, oUser := range oUsers {
id := strconv.Itoa(oUser.Id)
if _, ok := userMap[id]; !ok {
user := createUserFromOriginalUser(oUser)
fmt.Printf("New user: %v\n", user)
newUsers = append(newUsers, user)
newUser := createUserFromOriginalUser(oUser)
fmt.Printf("New user: %v\n", newUser)
newUsers = append(newUsers, newUser)
} else {
user := userMap[id]
hash := calculateHash(oUser)
if user.Hash != hash {
user := createUserFromOriginalUser(oUser)
object.UpdateUser(user.GetId(), user)
fmt.Printf("Update user: %v\n", user)
oHash := calculateHash(oUser)
if user.Hash == user.PreHash {
if user.Hash != oHash {
updatedUser := createUserFromOriginalUser(oUser)
updatedUser.Hash = oHash
updatedUser.PreHash = oHash
object.UpdateUserForOriginal(updatedUser)
fmt.Printf("Update from oUser to user: %v\n", updatedUser)
}
} else {
if user.PreHash == oHash {
updatedOUser := createOriginalUserFromUser(user)
updateUser(updatedOUser)
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
// update preHash
user.PreHash = user.Hash
object.SetUserField(user, "preHash", user.PreHash)
} else {
if user.Hash == oHash {
// update preHash
user.PreHash = user.Hash
object.SetUserField(user, "preHash", user.PreHash)
} else {
updatedUser := createUserFromOriginalUser(oUser)
updatedUser.Hash = oHash
updatedUser.PreHash = oHash
object.UpdateUserForOriginal(updatedUser)
fmt.Printf("Update from oUser to user (2nd condition): %v\n", updatedUser)
}
}
}
}
}
object.AddUsersSafe(newUsers)
for _, user := range users {
id := user.Id
if _, ok := oUserMap[id]; !ok {
panic(fmt.Sprintf("New original user: cannot create now, user = %v", user))
}
}
}

View File

@ -54,6 +54,15 @@ func getUserMapOriginal() ([]*User, map[string]*User) {
return users, m
}
func updateUser(user *User) bool {
affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "avatar", "deleted").Update(user)
if err != nil {
panic(err)
}
return affected != 0
}
func calculateHash(user *User) string {
s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone}, "|")
return util.GetMd5Hash(s)

View File

@ -19,11 +19,21 @@ import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func ParseInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
func GetOwnerAndNameFromId(id string) (string, string) {
tokens := strings.Split(id, "/")
if len(tokens) != 2 {