mirror of https://github.com/gogs/gogs.git
fix(db): update `user.updated_unix` upon changing username (#7262)
parent
644a3a9d78
commit
a7dbc970df
|
@ -31,14 +31,6 @@ func (u *User) BeforeInsert() {
|
|||
u.UpdatedUnix = u.CreatedUnix
|
||||
}
|
||||
|
||||
// TODO(unknwon): Refactoring together with methods that do updates.
|
||||
func (u *User) BeforeUpdate() {
|
||||
if u.MaxRepoCreation < -1 {
|
||||
u.MaxRepoCreation = -1
|
||||
}
|
||||
u.UpdatedUnix = time.Now().Unix()
|
||||
}
|
||||
|
||||
// TODO(unknwon): Delete me once refactoring is done.
|
||||
func (u *User) AfterSet(colName string, _ xorm.Cell) {
|
||||
switch colName {
|
||||
|
@ -49,13 +41,6 @@ func (u *User) AfterSet(colName string, _ xorm.Cell) {
|
|||
}
|
||||
}
|
||||
|
||||
// Deprecated: Use OrgsUsers.CountByUser instead.
|
||||
//
|
||||
// TODO(unknwon): Delete me once no more call sites in this file.
|
||||
func (u *User) getOrganizationCount(e Engine) (int64, error) {
|
||||
return e.Where("uid=?", u.ID).Count(new(OrgUser))
|
||||
}
|
||||
|
||||
func updateUser(e Engine, u *User) error {
|
||||
// Organization does not need email
|
||||
if !u.IsOrganization() {
|
||||
|
@ -82,6 +67,14 @@ func updateUser(e Engine, u *User) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// TODO(unknwon): Refactoring together with methods that do updates.
|
||||
func (u *User) BeforeUpdate() {
|
||||
if u.MaxRepoCreation < -1 {
|
||||
u.MaxRepoCreation = -1
|
||||
}
|
||||
u.UpdatedUnix = time.Now().Unix()
|
||||
}
|
||||
|
||||
// UpdateUser updates user's information.
|
||||
func UpdateUser(u *User) error {
|
||||
return updateUser(x, u)
|
||||
|
@ -202,6 +195,13 @@ func deleteUser(e *xorm.Session, u *User) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Deprecated: Use OrgsUsers.CountByUser instead.
|
||||
//
|
||||
// TODO(unknwon): Delete me once no more call sites in this file.
|
||||
func (u *User) getOrganizationCount(e Engine) (int64, error) {
|
||||
return e.Where("uid=?", u.ID).Count(new(OrgUser))
|
||||
}
|
||||
|
||||
// DeleteUser completely and permanently deletes everything of a user,
|
||||
// but issues/comments/pulls will be kept and shown as someone has been deleted.
|
||||
func DeleteUser(u *User) (err error) {
|
||||
|
|
|
@ -218,8 +218,9 @@ func (db *users) ChangeUsername(ctx context.Context, userID int64, newUsername s
|
|||
err := tx.Model(&User{}).
|
||||
Where("id = ?", user.ID).
|
||||
Updates(map[string]any{
|
||||
"lower_name": strings.ToLower(newUsername),
|
||||
"name": newUsername,
|
||||
"lower_name": strings.ToLower(newUsername),
|
||||
"name": newUsername,
|
||||
"updated_unix": tx.NowFunc().Unix(),
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "update user name")
|
||||
|
|
|
@ -285,6 +285,9 @@ func usersChangeUsername(t *testing.T, db *users) {
|
|||
err = db.Exec(`INSERT INTO pull_request (head_user_name) VALUES (?)`, alice.Name).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.Model(&User{}).Where("id = ?", alice.ID).Update("updated_unix", 0).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.MkdirAll(repoutil.UserPath(alice.Name), os.ModePerm)
|
||||
require.NoError(t, err)
|
||||
err = os.MkdirAll(repoutil.RepositoryLocalPath(repo.ID), os.ModePerm)
|
||||
|
@ -293,6 +296,17 @@ func usersChangeUsername(t *testing.T, db *users) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// Make sure mock data is set up correctly
|
||||
// TODO: Use PullRequests.GetByID to replace SQL hack when the method is available.
|
||||
var headUserName string
|
||||
err = db.Model(&PullRequest{}).Select("head_user_name").Row().Scan(&headUserName)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, headUserName, alice.Name)
|
||||
|
||||
var updatedUnix int64
|
||||
err = db.Model(&User{}).Select("updated_unix").Row().Scan(&updatedUnix)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(0), updatedUnix)
|
||||
|
||||
assert.True(t, osutil.IsExist(repoutil.UserPath(alice.Name)))
|
||||
assert.True(t, osutil.IsExist(repoutil.RepositoryLocalPath(repo.ID)))
|
||||
assert.True(t, osutil.IsExist(repoutil.RepositoryLocalWikiPath(repo.ID)))
|
||||
|
@ -302,8 +316,7 @@ func usersChangeUsername(t *testing.T, db *users) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// TODO: Use PullRequests.GetByID to replace SQL hack when the method is available.
|
||||
var headUserName string
|
||||
err = db.Select("head_user_name").Table("pull_request").Row().Scan(&headUserName)
|
||||
err = db.Model(&PullRequest{}).Select("head_user_name").Row().Scan(&headUserName)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, headUserName, newUsername)
|
||||
|
||||
|
@ -315,6 +328,7 @@ func usersChangeUsername(t *testing.T, db *users) {
|
|||
alice, err = db.GetByID(ctx, alice.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, newUsername, alice.Name)
|
||||
assert.Equal(t, db.NowFunc().Unix(), alice.UpdatedUnix)
|
||||
}
|
||||
|
||||
func usersCount(t *testing.T, db *users) {
|
||||
|
|
Loading…
Reference in New Issue