Jump to conversation
Unresolved conversations (1)
@wxiaoguang wxiaoguang Oct 29, 2021
I used a trick to make upsert quickly: https://github.com/go-gitea/gitea/blob/e6e3b212b3ed01705ac6fb746f87df8204c8102c/models/appstate/appstate.go#L31 IIRC, `INSERT ON DUPLICATE` keeps increasing the next auto incr ID even if the insertion fails, for a busy and huge system, the ID will increase quickly ... (although the quickly increased ID should not be a problem, but I prefer to keep the IDs clear and compact)
Outdated
models/user_setting.go
wxiaoguang techknowlogick
Resolved conversations (26)
@lunny lunny Nov 22, 2021
No, this is not right. The next serval `err` have override the first one.
models/user/setting.go
wxiaoguang
@wxiaoguang wxiaoguang Nov 21, 2021
IIRC, the `NewSession` should not be used now (a recent refactoring), there is a `TxContext` instead.
Outdated
models/user/setting.go
@wxiaoguang wxiaoguang Nov 21, 2021
Here we need a `insert on duplicate ignore` or ignore some unique-duplicated-errors. Because the `update` before may return 0 if the record already exists but it isn't changed. And that's why I usually do `revision=revision+1` to make sure the update always returns 1 if there is already an existing row (then we can do a simple insert, no need to worry about an existing record.) ps: If no easy way to do `insert on duplicate ignore` or we do not want to use an extra `revision` column and we want a general method to do safe upsert, here we can do a `Get(SELECT)`, since the txn is locked, if this `SELECT` returns 0 rows, then it guarantee that there is no duplicated rows and it's safe to do a simple `INSERT`. eg: ```sql create table t ...; insert into t (c1, v2) values (1,2); update t set c2=2 where c1=1; // updated 0 rows update t set c2=c2+1 where c1=1; // updated 1 rows ```
Outdated
models/user/setting.go
@lunny lunny Nov 21, 2021
maybe `rows > 0` is better.
Outdated
models/user/setting.go
@lunny lunny Nov 21, 2021
maybe `GetSettings` is a better name.
Outdated
models/user/setting.go
@lunny lunny Nov 10, 2021
How about move this into `models/user/setting.go` so that we don't need move it again in future PRs.
Outdated
models/user_setting.go
techknowlogick wxiaoguang
lunny
@wxiaoguang wxiaoguang Nov 10, 2021
Here mismatches the real `UserSetting`. Otherwise L-G-T-M
Outdated
models/migrations/v202.go
techknowlogick
@wxiaoguang wxiaoguang Oct 29, 2021
Should we just return a map? It will be easier to be used for callers.
Outdated
models/user_setting.go
techknowlogick wxiaoguang
@wxiaoguang wxiaoguang Oct 29, 2021
The `order by` seems no affect.
Outdated
models/user_setting.go
techknowlogick
@wxiaoguang wxiaoguang Oct 29, 2021
Maybe `SettingKey` and `SettingValue` are better, since `Key` and `Value` all seem to be possible SQL keywords
Outdated
models/migrations/v199.go
techknowlogick
@wxiaoguang wxiaoguang Oct 29, 2021
Only one `sess.Delete`, maybe we do not need transaction here.
Outdated
models/user_setting.go
techknowlogick
@wxiaoguang wxiaoguang Oct 29, 2021
The name can be `GetUserAllSettings`. `GetAllUserSettings` sounds like it gets the settings of all users. ps: Should we just return a map? It will be easier to be used for callers.
Outdated
models/user_setting.go
techknowlogick
@wxiaoguang wxiaoguang Oct 29, 2021
Maybe it's caller's responsibility to make the keys identical. Changing the character cases seem strange. The caller will be confused that the value of an object was changed silently.
Outdated
models/user_setting.go
techknowlogick
@delvh delvh Oct 7, 2021
What about extracting the used values to variables so that they can be changed easily and to ensure that everything has the same value?
Outdated
models/user_setting_test.go
@delvh delvh Oct 7, 2021
Shouldn't it be ```suggestion settings := make([]*UserSetting, 0, len(keys)) ``` as we expect an entry for each provided key?
Outdated
models/user_setting.go
@delvh delvh Oct 7, 2021
`GetAllUserSettings`?
Outdated
models/user_setting.go
techknowlogick
@delvh delvh Oct 7, 2021
I think there is a `BeforeDelete` missing here.
Outdated
models/user_setting.go
techknowlogick
@delvh delvh Oct 7, 2021
Copy-paste error? ```suggestion // ErrUserSettingNotExists represents a "setting does not exist for user" error. ```
Outdated
models/error.go
@techknowlogick techknowlogick Oct 7, 2021
```suggestion ```
Outdated
models/migrations/v197.go
@techknowlogick techknowlogick Oct 7, 2021
```suggestion UserID int64 `xorm:"index unique(key_userid)"` // to load all of someone's settings Key string `xorm:"varchar(255) index unique(key_userid)"` // ensure key is always lowercase ```
Outdated
models/migrations/v197.go
@zeripath zeripath Sep 28, 2021
If the intention is that there is only one value per key+userID pair we should add a uniqueness constraint: ```suggestion type UserSetting struct { ID int64 `xorm:"pk autoincr"` UserID int64 `xorm:"index unique(key_userid)"` // to load all of someone's settings Key string `xorm:"varchar(255) index unique(key_userid)"` // ensure key is always lowercase Value string `xorm:"text"` } ``` Then you can use an UPSERT pattern to update the key below.
Outdated
models/user_setting.go
@silverwind silverwind Sep 10, 2021
Why not just overwrite on update?
Outdated
models/user_setting.go
techknowlogick zeripath
delvh
@lunny lunny Aug 29, 2021
We need a `insertOrUpdateUserSetting` method to do the thing.
Outdated
models/user_setting.go
techknowlogick lunny
@lunny lunny Aug 29, 2021
`Exist()` method has better performance than `Get()`
Outdated
models/user_setting.go
techknowlogick
@lunny lunny Aug 29, 2021
And we should also have `BeforeUpdate`
Outdated
models/user_setting.go
techknowlogick
@lunny lunny Aug 29, 2021
2021
Outdated
models/user_setting.go
techknowlogick