From 7abe7674f3f056563f403b0396a62221dc6c211e Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Thu, 7 Jul 2022 16:00:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=90=AF=E7=94=A8=E4=B8=8D=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E9=80=A0=E6=88=90=E7=9A=84=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E6=80=A7=E7=B4=A2=E5=BC=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/custom_migrations.go | 18 +++++++++++++++--- models/user.go | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) 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