Always store primary email address into email_address table and also the state (#15956)
* Always store primary email address into email_address table and also the state * Add lower_email to not convert email to lower as what's added * Fix fixture * Fix tests * Use BeforeInsert to save lower email * Fix v180 migration * fix tests * Fix test * Remove wrong submited codes * Fix test * Fix test * Fix test * Add test for v181 migration * remove change user's email to lower * Revert change on user's email column * Fix lower email * Fix test * Fix testtokarchuk/v1.17
parent
21cde5c439
commit
b9d611e917
@ -1,35 +1,279 @@ |
||||
- |
||||
id: 1 |
||||
uid: 1 |
||||
uid: 11 |
||||
email: user11@example.com |
||||
lower_email: user11@example.com |
||||
is_activated: false |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 2 |
||||
uid: 1 |
||||
uid: 12 |
||||
email: user12@example.com |
||||
is_activated: false |
||||
lower_email: user12@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 3 |
||||
uid: 2 |
||||
email: user2@example.com |
||||
lower_email: user2@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 4 |
||||
uid: 2 |
||||
uid: 21 |
||||
email: user21@example.com |
||||
is_activated: false |
||||
lower_email: user21@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 5 |
||||
uid: 9999999 |
||||
email: user9999999@example.com |
||||
lower_email: user9999999@example.com |
||||
is_activated: true |
||||
is_primary: false |
||||
|
||||
- |
||||
id: 6 |
||||
uid: 10 |
||||
email: user10@example.com |
||||
lower_email: user10@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 7 |
||||
uid: 10 |
||||
email: user101@example.com |
||||
lower_email: user101@example.com |
||||
is_activated: true |
||||
is_primary: false |
||||
|
||||
- |
||||
id: 8 |
||||
uid: 9 |
||||
email: user9@example.com |
||||
lower_email: user9@example.com |
||||
is_activated: false |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 9 |
||||
uid: 1 |
||||
email: user1@example.com |
||||
lower_email: user1@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 10 |
||||
uid: 3 |
||||
email: user3@example.com |
||||
lower_email: user3@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 11 |
||||
uid: 4 |
||||
email: user4@example.com |
||||
lower_email: user4@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 12 |
||||
uid: 5 |
||||
email: user5@example.com |
||||
lower_email: user5@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 13 |
||||
uid: 6 |
||||
email: user6@example.com |
||||
lower_email: user6@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 14 |
||||
uid: 7 |
||||
email: user7@example.com |
||||
lower_email: user7@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 15 |
||||
uid: 8 |
||||
email: user8@example.com |
||||
lower_email: user8@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 16 |
||||
uid: 13 |
||||
email: user13@example.com |
||||
lower_email: user13@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 17 |
||||
uid: 14 |
||||
email: user14@example.com |
||||
lower_email: user14@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 18 |
||||
uid: 15 |
||||
email: user15@example.com |
||||
lower_email: user15@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 19 |
||||
uid: 16 |
||||
email: user16@example.com |
||||
lower_email: user16@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 20 |
||||
uid: 17 |
||||
email: user17@example.com |
||||
lower_email: user17@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 21 |
||||
uid: 18 |
||||
email: user18@example.com |
||||
lower_email: user18@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 22 |
||||
uid: 19 |
||||
email: user19@example.com |
||||
lower_email: user19@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 23 |
||||
uid: 20 |
||||
email: user20@example.com |
||||
lower_email: user20@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 24 |
||||
uid: 22 |
||||
email: limited_org@example.com |
||||
lower_email: limited_org@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 25 |
||||
uid: 23 |
||||
email: privated_org@example.com |
||||
lower_email: privated_org@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 26 |
||||
uid: 24 |
||||
email: user24@example.com |
||||
lower_email: user24@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 27 |
||||
uid: 25 |
||||
email: org25@example.com |
||||
lower_email: org25@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 28 |
||||
uid: 26 |
||||
email: org26@example.com |
||||
lower_email: org26@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 29 |
||||
uid: 27 |
||||
email: user27@example.com |
||||
lower_email: user27@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 30 |
||||
uid: 28 |
||||
email: user28@example.com |
||||
lower_email: user28@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 31 |
||||
uid: 29 |
||||
email: user29@example.com |
||||
lower_email: user29@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 32 |
||||
uid: 30 |
||||
email: user30@example.com |
||||
lower_email: user30@example.com |
||||
is_activated: true |
||||
is_primary: true |
||||
|
||||
- |
||||
id: 33 |
||||
uid: 1 |
||||
email: user1-2@example.com |
||||
lower_email: user1-2@example.com |
||||
is_activated: true |
||||
is_primary: false |
||||
|
||||
- |
||||
id: 34 |
||||
uid: 1 |
||||
email: user1-3@example.com |
||||
lower_email: user1-3@example.com |
||||
is_activated: true |
||||
is_primary: false |
||||
|
||||
- |
||||
id: 35 |
||||
uid: 2 |
||||
email: user2-2@example.com |
||||
lower_email: user2-2@example.com |
||||
is_activated: false |
||||
is_primary: false |
@ -0,0 +1,92 @@ |
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations |
||||
|
||||
import ( |
||||
"strings" |
||||
|
||||
"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 EmailAddress1 struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
UID int64 `xorm:"INDEX NOT NULL"` |
||||
Email string `xorm:"UNIQUE NOT NULL"` |
||||
LowerEmail string |
||||
IsActivated bool |
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"` |
||||
} |
||||
|
||||
// Add lower_email and is_primary columns
|
||||
if err = x.Table("email_address").Sync2(new(EmailAddress1)); err != nil { |
||||
return |
||||
} |
||||
|
||||
if _, err = x.Exec("UPDATE email_address SET lower_email=LOWER(email), is_primary=?", false); err != nil { |
||||
return |
||||
} |
||||
|
||||
type EmailAddress struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
UID int64 `xorm:"INDEX NOT NULL"` |
||||
Email string `xorm:"UNIQUE NOT NULL"` |
||||
LowerEmail string `xorm:"UNIQUE NOT NULL"` |
||||
IsActivated bool |
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"` |
||||
} |
||||
|
||||
// change lower_email as unique
|
||||
if err = x.Sync2(new(EmailAddress)); 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, |
||||
LowerEmail: strings.ToLower(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 |
||||
} |
@ -0,0 +1,54 @@ |
||||
// 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 ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func Test_addPrimaryEmail2EmailAddress(t *testing.T) { |
||||
type User struct { |
||||
ID int64 |
||||
Email string |
||||
IsActive bool |
||||
} |
||||
|
||||
// Prepare and load the testing database
|
||||
x, deferable := prepareTestEnv(t, 0, new(User)) |
||||
if x == nil || t.Failed() { |
||||
defer deferable() |
||||
return |
||||
} |
||||
defer deferable() |
||||
|
||||
err := addPrimaryEmail2EmailAddress(x) |
||||
assert.NoError(t, err) |
||||
|
||||
type EmailAddress struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
UID int64 `xorm:"INDEX NOT NULL"` |
||||
Email string `xorm:"UNIQUE NOT NULL"` |
||||
LowerEmail string `xorm:"UNIQUE NOT NULL"` |
||||
IsActivated bool |
||||
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"` |
||||
} |
||||
|
||||
var users = make([]User, 0, 20) |
||||
err = x.Find(&users) |
||||
assert.NoError(t, err) |
||||
|
||||
for _, user := range users { |
||||
var emailAddress EmailAddress |
||||
has, err := x.Where("lower_email=?", strings.ToLower(user.Email)).Get(&emailAddress) |
||||
assert.NoError(t, err) |
||||
assert.True(t, has) |
||||
assert.True(t, emailAddress.IsPrimary) |
||||
assert.EqualValues(t, user.IsActive, emailAddress.IsActivated) |
||||
assert.EqualValues(t, user.ID, emailAddress.UID) |
||||
} |
||||
} |
Loading…
Reference in new issue