diff options
author | Joe Chen <jc@unknwon.io> | 2022-11-25 23:01:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-25 23:01:41 +0800 |
commit | a7dbc970dfaac9f04addf05da97bb0aa29083e37 (patch) | |
tree | 6cc1a8b479c7be9e42a4d9fadaf8505f68455e85 /internal | |
parent | 644a3a9d78a691af8f0f5ab04d5d44207b5d03dc (diff) |
fix(db): update `user.updated_unix` upon changing username (#7262)
Diffstat (limited to 'internal')
-rw-r--r-- | internal/db/user.go | 30 | ||||
-rw-r--r-- | internal/db/users.go | 5 | ||||
-rw-r--r-- | internal/db/users_test.go | 18 |
3 files changed, 34 insertions, 19 deletions
diff --git a/internal/db/user.go b/internal/db/user.go index 5d75bfca..24af0ec1 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -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) { diff --git a/internal/db/users.go b/internal/db/users.go index 12fa90a2..01688dab 100644 --- a/internal/db/users.go +++ b/internal/db/users.go @@ -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") diff --git a/internal/db/users_test.go b/internal/db/users_test.go index 697bad8f..26d5ec7c 100644 --- a/internal/db/users_test.go +++ b/internal/db/users_test.go @@ -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) { |