-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure user email is always in email address table #13296
Closed
ivanvc
wants to merge
13
commits into
go-gitea:master
from
ivanvc:ensure-user-email-is-stored-in-email-address-table
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e9d0455
Ensure user email is always in email address table
ivanvc 111e507
Fix format
ivanvc e259244
Merge remote-tracking branch 'origin/master' into ensure-user-email-i…
ivanvc 77cd657
Fix email db seeds
ivanvc d2d2479
Merge remote-tracking branch 'origin/master' into ensure-user-email-i…
ivanvc bdd4bc2
Update seeds, make it consistent with previous
ivanvc c220b5e
Warn if email address is taken rather than fail
ivanvc 38901cf
Merge remote-tracking branch 'origin/master' into ensure-user-email-i…
ivanvc 4de7015
Revert "Update seeds, make it consistent with ..."
ivanvc d2cc31c
Revert "Fix email db seeds"
ivanvc cf397c2
Fix user email fixtures
ivanvc 987a843
Merge remote-tracking branch 'origin/master' into ensure-user-email-i…
ivanvc daa11e6
Remove unnecessary sync
ivanvc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/log" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func addUserPrimaryEmailToUserMails(x *xorm.Engine) error { | ||
type User struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
Email string `xorm:"NOT NULL"` | ||
IsActive bool `xorm:"INDEX"` | ||
} | ||
type EmailAddress struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"INDEX NOT NULL"` | ||
Email string `xorm:"UNIQUE NOT NULL"` | ||
IsActivated bool | ||
} | ||
|
||
updateUsers := func(users []*User) error { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
for _, user := range users { | ||
email := new(EmailAddress) | ||
if isExist, err := sess.Where("email=?", user.Email).Get(email); err != nil { | ||
return err | ||
} else if isExist { | ||
if email.UID != user.ID { | ||
log.Warn("Email (%s) from user with ID %d is taken by user with ID %d", email.Email, user.ID, email.UID) | ||
log.Warn("Skipping to avoid conflicts, should be manually fixed") | ||
} | ||
continue | ||
} | ||
email = &EmailAddress{ | ||
Email: user.Email, | ||
UID: user.ID, | ||
IsActivated: user.IsActive, | ||
} | ||
if _, err := sess.Insert(email); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return sess.Commit() | ||
} | ||
|
||
var start = 0 | ||
var batchSize = 100 | ||
for { | ||
var users = make([]*User, 0, batchSize) | ||
if err := x.Limit(batchSize, start).Find(&users); err != nil { | ||
return err | ||
} | ||
|
||
if err := updateUsers(users); err != nil { | ||
return err | ||
} | ||
|
||
start += len(users) | ||
|
||
if len(users) < batchSize { | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in the
email_1ddress.yml
fixtures, theuser11@example.com
is registered touser1
, and changing that file rather than this, brings more issues and places where it should be updated (I initially went through that route with commits bdd4bc2 and 77cd657), and decided later that the minimal impact would be to update the email address here.