diff options
| author | 2022-11-16 11:27:08 +0100 | |
|---|---|---|
| committer | 2022-11-16 11:27:08 +0100 | |
| commit | 940abc279cb84e6bdb62326565b04c279bfb596b (patch) | |
| tree | 2a8c03afcc57a70491d486391c0e638864b92017 /internal/db | |
| parent | [chore] update database caching library (#1040) (diff) | |
| download | gotosocial-940abc279cb84e6bdb62326565b04c279bfb596b.tar.xz | |
[chore] reversion: use specific columns for updating user again (#1059)
Diffstat (limited to 'internal/db')
| -rw-r--r-- | internal/db/bundb/user.go | 27 | ||||
| -rw-r--r-- | internal/db/user.go | 4 | 
2 files changed, 21 insertions, 10 deletions
| diff --git a/internal/db/bundb/user.go b/internal/db/bundb/user.go index d9b281a6f..ab3cc0f67 100644 --- a/internal/db/bundb/user.go +++ b/internal/db/bundb/user.go @@ -133,18 +133,29 @@ func (u *userDB) PutUser(ctx context.Context, user *gtsmodel.User) db.Error {  	})  } -func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User) db.Error { +func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ...string) db.Error {  	// Update the user's last-updated  	user.UpdatedAt = time.Now() -	return u.cache.Store(user, func() error { -		_, err := u.conn. -			NewUpdate(). -			Model(user). -			Where("? = ?", bun.Ident("user.id"), user.ID). -			Exec(ctx) +	if len(columns) > 0 { +		// If we're updating by column, ensure "updated_at" is included +		columns = append(columns, "updated_at") +	} + +	// Update the user in DB +	_, err := u.conn. +		NewUpdate(). +		Model(user). +		Where("? = ?", bun.Ident("user.id"), user.ID). +		Column(columns...). +		Exec(ctx) +	if err != nil {  		return u.conn.ProcessError(err) -	}) +	} + +	// Invalidate in cache +	u.cache.Invalidate("ID", user.ID) +	return nil  }  func (u *userDB) DeleteUserByID(ctx context.Context, userID string) db.Error { diff --git a/internal/db/user.go b/internal/db/user.go index d01a8862a..1c6118fce 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -36,8 +36,8 @@ type User interface {  	GetUserByConfirmationToken(ctx context.Context, confirmationToken string) (*gtsmodel.User, Error)  	// PutUser will attempt to place user in the database  	PutUser(ctx context.Context, user *gtsmodel.User) Error -	// UpdateUser updates one user by its primary key. -	UpdateUser(ctx context.Context, user *gtsmodel.User) Error +	// UpdateUser updates one user by its primary key, updating either only the specified columns, or all of them. +	UpdateUser(ctx context.Context, user *gtsmodel.User, columns ...string) Error  	// DeleteUserByID deletes one user by its ID.  	DeleteUserByID(ctx context.Context, userID string) Error  } | 
