diff --git a/models/custom_migrations.go b/models/custom_migrations.go index 65b53f0f4..041c4fb93 100755 --- a/models/custom_migrations.go +++ b/models/custom_migrations.go @@ -15,10 +15,15 @@ type CustomMigrationStatic struct { Migrate func(*xorm.Engine, *xorm.Engine) error } -var customMigrations []CustomMigration +var customMigrations = []CustomMigration{ + //手机号功能可以不启用,不启用时手机号为空串,为了避免启用时唯一性约束报错,定制唯一性约束(对null和空字符串不做唯一性检查) + {"set phone number unique index", setPhoneNumberUniqueIndex}, +} var customMigrationsStatic []CustomMigrationStatic + + func MigrateCustom(x *xorm.Engine) { for _, m := range customMigrations { @@ -35,13 +40,20 @@ func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) { for _, m := range customMigrationsStatic { log.Info("Migration: %s", m.Description) if err := m.Migrate(x, static); err != nil { - log.Error("Migration: %v", err) - } } } +func setPhoneNumberUniqueIndex(x *xorm.Engine) error { + + query := "CREATE UNIQUE INDEX IF NOT EXISTS \"UQE_user_phone_number\" ON \"user\" (phone_number) WHERE phone_number IS NOT NULL and phone_number !='';" + + _, err := x.Exec(query) + return err +} + + func syncTopicStruct(x *xorm.Engine) error { query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);" diff --git a/models/user.go b/models/user.go index 9602c6e6e..a423a843b 100755 --- a/models/user.go +++ b/models/user.go @@ -185,7 +185,7 @@ type User struct { WechatOpenId string `xorm:"INDEX"` WechatBindUnix timeutil.TimeStamp //Mobile phone - PhoneNumber string `xorm:"UNIQUE"` + PhoneNumber string `xorm:"VARCHAR(255)"` } // SearchOrganizationsOptions options to filter organizations