Browse Source

解决手机号功能启用不启用切换造成的唯一性索引问题

pull/2447/head
ychao_1983 2 years ago
parent
commit
7abe7674f3
2 changed files with 16 additions and 4 deletions
  1. +15
    -3
      models/custom_migrations.go
  2. +1
    -1
      models/user.go

+ 15
- 3
models/custom_migrations.go View File

@@ -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);"


+ 1
- 1
models/user.go View File

@@ -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


Loading…
Cancel
Save