Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update needed columns when update user #2296

Merged
merged 2 commits into from
Aug 12, 2017

Conversation

lunny
Copy link
Member

@lunny lunny commented Aug 11, 2017

Also fix #1882. After this merged, I will back ported to branch release/v1.1.

@lunny lunny added type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality. labels Aug 11, 2017
@lunny lunny added this to the 1.2.0 milestone Aug 11, 2017
// UpdateUserCols update user according special columns
func UpdateUserCols(u *User, cols ...string) error {
// Organization does not need email
u.Email = strings.ToLower(u.Email)
Copy link
Member

@sapk sapk Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we iterate over cols array and with a switch only do needed operation when matching col ?

Example : https://play.golang.org/p/4pRMeP89Re

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't get what you mean.

Copy link
Member

@sapk sapk Aug 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is overkill

// UpdateUserCols update user according special columns
func UpdateUserCols(u *User, cols ...string) error {
	//Format only when needed
	for _, c := range cols {
		switch c {
		case "email":
			// Organization does not need email
			u.Email = strings.ToLower(u.Email)
			if !u.IsOrganization() {
				if len(u.AvatarEmail) == 0 {
					u.AvatarEmail = u.Email
				}
			}
		case "name":
			u.LowerName = strings.ToLower(u.Name)
		case "location":
			u.Location = base.TruncateString(u.Location, 255)
		case "website":
			u.Website = base.TruncateString(u.Website, 255)
		case "description":
			u.Description = base.TruncateString(u.Description, 255)
		}
	}
	if !u.IsOrganization() {
		if len(u.AvatarEmail) > 0 {
			u.Avatar = base.HashEmail(u.AvatarEmail)
		}
	}
	_, err := x.Id(u.ID).Cols(cols...).Update(u)
	return err
}

models/user.go Outdated
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)

_, err := x.Id(u.ID).Cols(cols...).Update(u)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated_unix column won't be updated. I think we need to do something similar to #2204

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@ethantkoenig
Copy link
Member

Adding a new function seems like overkill to me, is there a reason we can't just fix the updateUser(..) function to not write to u.Avatar (and require the caller of updateUser() to update u.Avatar if they actually need it)?

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Aug 11, 2017
@sapk
Copy link
Member

sapk commented Aug 11, 2017

@ethantkoenig We could add a new func generateAvatar that is call before updateUser only when email is change or needed ?

@lunny
Copy link
Member Author

lunny commented Aug 12, 2017

@ethantkoenig I think updateUser is not corrected in many place where only need to update serval columns but update all the columns. So I added a new function updateUserCols to do that. And there are still some place where we should use updateUser to update all the columns.
We cannot finish the work only improve updateUser function, so I create the new one.
For fix of #1882, it's not the PR's main idea. #1882 's problem is when user login, the avatar will be updated, so the avatar changed. In fact, the only needed change column on login is last_login_time but it update all the columns. When I change to updateUserCols, the bug is also fixed.

@lunny
Copy link
Member Author

lunny commented Aug 12, 2017

@sapk that could be another PR.

@ethantkoenig
Copy link
Member

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Aug 12, 2017
@lafriks
Copy link
Member

lafriks commented Aug 12, 2017

LGTM

@lafriks
Copy link
Member

lafriks commented Aug 12, 2017

Make LG-TM work

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Aug 12, 2017
@lunny lunny merged commit f960e19 into go-gitea:master Aug 12, 2017
@lunny lunny deleted the lunny/update_user_cols branch August 12, 2017 14:18
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User avatars seem to change color often
5 participants