-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Always store primary email address into email_address table and also the state #15956
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2180ed1
Always store primary email address into email_address table and also …
lunny 7ca9dfd
Add lower_email to not convert email to lower as what's added
lunny a4bab8e
Fix fixture
lunny 09c7ae7
Fix tests
lunny a1896e9
Use BeforeInsert to save lower email
lunny 06b9b4e
Fix v180 migration
lunny 1b39dc7
fix tests
lunny 1015f51
Fix test
lunny 01aae82
Remove wrong submited codes
lunny 3437a6f
Fix test
lunny 57227c3
Fix test
lunny c7e7bfb
Fix test
lunny 1ed31d1
Add test for v181 migration
lunny 1ab6eb2
remove change user's email to lower
lunny 5bb4124
Revert change on user's email column
lunny 7e511d2
Fix lower email
lunny eb652a9
Fix test
lunny ce66547
Fix test
lunny 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
Next
Next commit
Always store primary email address into email_address table and also …
…the state
- Loading branch information
commit 2180ed11f62db6dbfa7f95b27b0a7e4c5a54b564
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| // Copyright 2021 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 ( | ||
| "xorm.io/xorm" | ||
| ) | ||
|
|
||
| func addPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) { | ||
| type User struct { | ||
| ID int64 `xorm:"pk autoincr"` | ||
| Email string `xorm:"NOT NULL"` | ||
| IsActive bool `xorm:"INDEX"` // Activate primary email | ||
| } | ||
|
|
||
| type EmailAddress struct { | ||
| ID int64 `xorm:"pk autoincr"` | ||
| UID int64 `xorm:"INDEX NOT NULL"` | ||
| Email string `xorm:"UNIQUE NOT NULL"` | ||
| IsActivated bool | ||
| IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"` | ||
| } | ||
|
|
||
| // Add is_primary column | ||
| if err = x.Sync2(new(EmailAddress)); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if _, err = x.Exec("UPDATE email_address SET is_primary=? WHERE is_primary IS NULL", false); err != nil { | ||
| return | ||
| } | ||
|
|
||
| sess := x.NewSession() | ||
| defer sess.Close() | ||
|
|
||
| const batchSize = 100 | ||
|
|
||
| for start := 0; ; start += batchSize { | ||
| users := make([]*User, 0, batchSize) | ||
| if err = sess.Limit(batchSize, start).Find(&users); err != nil { | ||
| return | ||
| } | ||
| if len(users) == 0 { | ||
| break | ||
| } | ||
|
|
||
| for _, user := range users { | ||
| var exist bool | ||
| exist, err = sess.Where("email=?", user.Email).Table("email_address").Exist() | ||
| if err != nil { | ||
| return | ||
| } | ||
| if !exist { | ||
| if _, err = sess.Insert(&EmailAddress{ | ||
| UID: user.ID, | ||
| Email: user.Email, | ||
| IsActivated: user.IsActive, | ||
| IsPrimary: true, | ||
| }); err != nil { | ||
| return | ||
| } | ||
| } else { | ||
| if _, err = sess.Where("email=?", user.Email).Cols("is_primary").Update(&EmailAddress{ | ||
| IsPrimary: true, | ||
| }); err != nil { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.