Skip to content

Commit b62e13a

Browse files
Open transaction when adding Avatar email-hash pairs to the DB (#12577) (#12940)
Backport #12577 When adding Avatar email-hash pairs we simply want the DB table to represent a Set. We don't care if the hash-pair is already present, so we just simply Insert and ignore the error. Unfortunately this seems to cause some DBs to log the duplicate insert to their logs - looking like a bug a in Gitea. Now, there is no standard way in SQL to say Insert but if there's an error ignore it. MySQL has INSERT IGNORE, PostgreSQL >= 9.5 has INSERT ... ON CONFLICT DO NOTHING, but I do not believe that SQLite or MSSQL have variants. This PR places the insert in a transaction which we are happy to fail if there is an error - hopefully this will stop the unnecessary logging. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 470b195 commit b62e13a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

models/avatar.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ func AvatarLink(email string) string {
4141
Email: lowerEmail,
4242
Hash: sum,
4343
}
44-
_, _ = x.Insert(emailHash)
44+
// OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
45+
sess := x.NewSession()
46+
defer sess.Close()
47+
if err := sess.Begin(); err != nil {
48+
// we don't care about any DB problem just return the lowerEmail
49+
return lowerEmail, nil
50+
}
51+
_, _ = sess.Insert(emailHash)
52+
if err := sess.Commit(); err != nil {
53+
// Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
54+
return lowerEmail, nil
55+
}
4556
return lowerEmail, nil
4657
})
4758
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum)

0 commit comments

Comments
 (0)