diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-06-08 11:52:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-08 11:52:51 +0800 |
commit | b9d611e917d9bd10e0d8be8fc61e057d5936993c (patch) | |
tree | f70159ff5731da4b34312acfff8443dc3be61337 /models/migrations | |
parent | 21cde5c439676d4aaa15dfc79505f364cc849ec0 (diff) | |
download | gitea-b9d611e917d9bd10e0d8be8fc61e057d5936993c.tar.gz gitea-b9d611e917d9bd10e0d8be8fc61e057d5936993c.zip |
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 test
Diffstat (limited to 'models/migrations')
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v181.go | 92 | ||||
-rw-r--r-- | models/migrations/v181_test.go | 54 |
3 files changed, 148 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index d440722c96..df1bac4a13 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -311,6 +311,8 @@ var migrations = []Migration{ NewMigration("Convert avatar url to text", convertAvatarURLToText), // v180 -> v181 NewMigration("Delete credentials from past migrations", deleteMigrationCredentials), + // v181 -> v182 + NewMigration("Always save primary email on email address table", addPrimaryEmail2EmailAddress), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v181.go b/models/migrations/v181.go new file mode 100644 index 0000000000..6ba4edc155 --- /dev/null +++ b/models/migrations/v181.go @@ -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 +} diff --git a/models/migrations/v181_test.go b/models/migrations/v181_test.go new file mode 100644 index 0000000000..b392a9b71d --- /dev/null +++ b/models/migrations/v181_test.go @@ -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) + } +} |