|
1 | 1 | package sql |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "time" |
6 | 5 |
|
7 | 6 | "github.com/authorizerdev/authorizer/server/constants" |
@@ -71,35 +70,43 @@ func NewProvider() (*provider, error) { |
71 | 70 | return nil, err |
72 | 71 | } |
73 | 72 |
|
| 73 | + // For sqlserver, handle uniqueness of phone_number manually via extra db call |
| 74 | + // during create and update mutation. |
| 75 | + if sqlDB.Migrator().HasConstraint(&models.User{}, "authorizer_users_phone_number_key") { |
| 76 | + err = sqlDB.Migrator().DropConstraint(&models.User{}, "authorizer_users_phone_number_key") |
| 77 | + logrus.Debug("Failed to drop phone number constraint:", err) |
| 78 | + } |
| 79 | + |
74 | 80 | err = sqlDB.AutoMigrate(&models.User{}, &models.VerificationRequest{}, &models.Session{}, &models.Env{}, &models.Webhook{}, models.WebhookLog{}, models.EmailTemplate{}, &models.OTP{}) |
75 | 81 | if err != nil { |
76 | 82 | return nil, err |
77 | 83 | } |
78 | 84 |
|
| 85 | + // IMPACT: Request user to manually delete: UQ_phone_number constraint |
79 | 86 | // unique constraint on phone number does not work with multiple null values for sqlserver |
80 | 87 | // for more information check https://stackoverflow.com/a/767702 |
81 | | - if dbType == constants.DbTypeSqlserver { |
82 | | - var indexInfos []indexInfo |
83 | | - // remove index on phone number if present with different name |
84 | | - res := sqlDB.Raw("SELECT i.name AS index_name, i.type_desc AS index_algorithm, CASE i.is_unique WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS is_unique, ac.Name AS column_name FROM sys.tables AS t INNER JOIN sys.indexes AS i ON t.object_id = i.object_id INNER JOIN sys.index_columns AS ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id INNER JOIN sys.all_columns AS ac ON ic.object_id = ac.object_id AND ic.column_id = ac.column_id WHERE t.name = 'authorizer_users' AND SCHEMA_NAME(t.schema_id) = 'dbo';").Scan(&indexInfos) |
85 | | - if res.Error != nil { |
86 | | - return nil, res.Error |
87 | | - } |
88 | | - |
89 | | - for _, val := range indexInfos { |
90 | | - if val.ColumnName == phoneNumberColumnName && val.IndexName != phoneNumberIndexName { |
91 | | - // drop index & create new |
92 | | - if res := sqlDB.Exec(fmt.Sprintf(`ALTER TABLE authorizer_users DROP CONSTRAINT "%s";`, val.IndexName)); res.Error != nil { |
93 | | - return nil, res.Error |
94 | | - } |
95 | | - |
96 | | - // create index |
97 | | - if res := sqlDB.Exec(fmt.Sprintf("CREATE UNIQUE NONCLUSTERED INDEX %s ON authorizer_users(phone_number) WHERE phone_number IS NOT NULL;", phoneNumberIndexName)); res.Error != nil { |
98 | | - return nil, res.Error |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - } |
| 88 | + // if dbType == constants.DbTypeSqlserver { |
| 89 | + // var indexInfos []indexInfo |
| 90 | + // // remove index on phone number if present with different name |
| 91 | + // res := sqlDB.Raw("SELECT i.name AS index_name, i.type_desc AS index_algorithm, CASE i.is_unique WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS is_unique, ac.Name AS column_name FROM sys.tables AS t INNER JOIN sys.indexes AS i ON t.object_id = i.object_id INNER JOIN sys.index_columns AS ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id INNER JOIN sys.all_columns AS ac ON ic.object_id = ac.object_id AND ic.column_id = ac.column_id WHERE t.name = 'authorizer_users' AND SCHEMA_NAME(t.schema_id) = 'dbo';").Scan(&indexInfos) |
| 92 | + // if res.Error != nil { |
| 93 | + // return nil, res.Error |
| 94 | + // } |
| 95 | + |
| 96 | + // for _, val := range indexInfos { |
| 97 | + // if val.ColumnName == phoneNumberColumnName && val.IndexName != phoneNumberIndexName { |
| 98 | + // // drop index & create new |
| 99 | + // if res := sqlDB.Exec(fmt.Sprintf(`ALTER TABLE authorizer_users DROP CONSTRAINT "%s";`, val.IndexName)); res.Error != nil { |
| 100 | + // return nil, res.Error |
| 101 | + // } |
| 102 | + |
| 103 | + // // create index |
| 104 | + // if res := sqlDB.Exec(fmt.Sprintf("CREATE UNIQUE NONCLUSTERED INDEX %s ON authorizer_users(phone_number) WHERE phone_number IS NOT NULL;", phoneNumberIndexName)); res.Error != nil { |
| 105 | + // return nil, res.Error |
| 106 | + // } |
| 107 | + // } |
| 108 | + // } |
| 109 | + // } |
103 | 110 |
|
104 | 111 | return &provider{ |
105 | 112 | db: sqlDB, |
|
0 commit comments