fix repository count of user is messed up

This commit is contained in:
Unknwon 2015-08-30 01:13:24 +08:00
parent 9b9e5f0290
commit d01f688257
5 changed files with 39 additions and 22 deletions

View file

@ -649,8 +649,13 @@ func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
if _, err = e.Insert(repo); err != nil { if _, err = e.Insert(repo); err != nil {
return err return err
} else if _, err = e.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", u.Id); err != nil { }
return err
u.NumRepos++
// Remember visibility preference.
u.LastRepoVisibility = repo.IsPrivate
if err = updateUser(e, u); err != nil {
return fmt.Errorf("updateUser: %v", err)
} }
// Give access to all members in owner team. // Give access to all members in owner team.
@ -1279,7 +1284,7 @@ func CheckRepoStats() {
log.Trace("Doing: CheckRepoStats") log.Trace("Doing: CheckRepoStats")
// ***** START: Watch ***** // ***** START: Repository.NumWatches *****
results, err := x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id)") results, err := x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id)")
if err != nil { if err != nil {
log.Error(4, "Select repository check 'watch': %v", err) log.Error(4, "Select repository check 'watch': %v", err)
@ -1293,9 +1298,9 @@ func CheckRepoStats() {
log.Error(4, "Update repository check 'watch'[%d]: %v", repoID, err) log.Error(4, "Update repository check 'watch'[%d]: %v", repoID, err)
} }
} }
// ***** END: Watch ***** // ***** END: Repository.NumWatches *****
// ***** START: Star ***** // ***** START: Repository.NumStars *****
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)") results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)")
if err != nil { if err != nil {
log.Error(4, "Select repository check 'star': %v", err) log.Error(4, "Select repository check 'star': %v", err)
@ -1309,9 +1314,9 @@ func CheckRepoStats() {
log.Error(4, "Update repository check 'star'[%d]: %v", repoID, err) log.Error(4, "Update repository check 'star'[%d]: %v", repoID, err)
} }
} }
// ***** END: Star ***** // ***** END: Repository.NumStars *****
// ***** START: Label ***** // ***** START: Label.NumIssues *****
results, err = x.Query("SELECT label.id FROM `label` WHERE label.num_issues!=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=label.id)") results, err = x.Query("SELECT label.id FROM `label` WHERE label.num_issues!=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=label.id)")
if err != nil { if err != nil {
log.Error(4, "Select label check 'num_issues': %v", err) log.Error(4, "Select label check 'num_issues': %v", err)
@ -1325,7 +1330,23 @@ func CheckRepoStats() {
log.Error(4, "Update label check 'num_issues'[%d]: %v", labelID, err) log.Error(4, "Update label check 'num_issues'[%d]: %v", labelID, err)
} }
} }
// ***** END: Label ***** // ***** END: Label.NumIssues *****
// ***** START: User.NumRepos *****
results, err = x.Query("SELECT `user`.id FROM `user` WHERE `user`.num_repos!=(SELECT COUNT(*) FROM `repository` WHERE owner_id=`user`.id)")
if err != nil {
log.Error(4, "Select user check 'num_repos': %v", err)
return
}
for _, user := range results {
userID := com.StrTo(user["id"]).MustInt64()
log.Trace("Updating user count 'num_repos': %d", userID)
_, err = x.Exec("UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?", userID, userID)
if err != nil {
log.Error(4, "Update user check 'num_repos'[%d]: %v", userID, err)
}
}
// ***** END: User.NumRepos *****
} }
// _________ .__ .__ ___. __ .__ // _________ .__ .__ ___. __ .__

View file

@ -479,10 +479,9 @@ func ChangeUserName(u *User, newUserName string) (err error) {
return os.Rename(UserPath(u.LowerName), UserPath(newUserName)) return os.Rename(UserPath(u.LowerName), UserPath(newUserName))
} }
// UpdateUser updates user's information. func updateUser(e Engine, u *User) error {
func UpdateUser(u *User) error {
u.Email = strings.ToLower(u.Email) u.Email = strings.ToLower(u.Email)
has, err := x.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) has, err := e.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -507,10 +506,15 @@ func UpdateUser(u *User) error {
u.Avatar = avatar.HashEmail(u.AvatarEmail) u.Avatar = avatar.HashEmail(u.AvatarEmail)
u.FullName = base.Sanitizer.Sanitize(u.FullName) u.FullName = base.Sanitizer.Sanitize(u.FullName)
_, err = x.Id(u.Id).AllCols().Update(u) _, err = e.Id(u.Id).AllCols().Update(u)
return err return err
} }
// UpdateUser updates user's information.
func UpdateUser(u *User) error {
return updateUser(x, u)
}
// DeleteBeans deletes all given beans, beans should contain delete conditions. // DeleteBeans deletes all given beans, beans should contain delete conditions.
func DeleteBeans(e Engine, beans ...interface{}) (err error) { func DeleteBeans(e Engine, beans ...interface{}) (err error) {
for i := range beans { for i := range beans {

File diff suppressed because one or more lines are too long

View file

@ -232,7 +232,7 @@
color: #bd2c00; color: #bd2c00;
} }
&.octicon-primitive-dot { &.octicon-primitive-dot {
font-size: 35px; font-size: 30px;
color: #6cc644; color: #6cc644;
} }
&.octicon-bookmark { &.octicon-bookmark {

View file

@ -120,10 +120,6 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
AutoInit: form.AutoInit, AutoInit: form.AutoInit,
}) })
if err == nil { if err == nil {
// Remember visibility preference.
ctx.User.LastRepoVisibility = repo.IsPrivate
models.UpdateUser(ctx.User)
log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name) log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
return return
@ -190,10 +186,6 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr) repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
if err == nil { if err == nil {
// Remember visibility preference.
ctx.User.LastRepoVisibility = repo.IsPrivate
models.UpdateUser(ctx.User)
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
return return