fix: unique constraint data

This commit is contained in:
Lakhan Samani
2021-12-22 15:31:45 +05:30
parent 508c714932
commit 3ee79c3937
21 changed files with 206 additions and 192 deletions

View File

@@ -36,16 +36,12 @@ func initArangodb() (arangoDriver.Database, error) {
if arangodb_exists {
log.Println(constants.DATABASE_NAME + " db exists already")
arangodb, err = arangoClient.Database(nil, constants.DATABASE_NAME)
if err != nil {
return nil, err
}
} else {
arangodb, err = arangoClient.CreateDatabase(nil, constants.DATABASE_NAME, nil)
if err != nil {
return nil, err
}

View File

@@ -34,12 +34,6 @@ func initMongodb() (*mongo.Database, error) {
mongodb.CreateCollection(ctx, Collections.User, options.CreateCollection())
userCollection := mongodb.Collection(Collections.User, options.Collection())
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"id": 1},
Options: options.Index().SetUnique(true).SetSparse(true),
},
}, options.CreateIndexes())
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"email": 1},
@@ -48,19 +42,15 @@ func initMongodb() (*mongo.Database, error) {
}, options.CreateIndexes())
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"phone_number": 1},
Options: options.Index().SetUnique(true).SetSparse(true),
Keys: bson.M{"phone_number": 1},
Options: options.Index().SetUnique(true).SetSparse(true).SetPartialFilterExpression(map[string]interface{}{
"phone_number": map[string]string{"$type": "string"},
}),
},
}, options.CreateIndexes())
mongodb.CreateCollection(ctx, Collections.VerificationRequest, options.CreateCollection())
verificationRequestCollection := mongodb.Collection(Collections.VerificationRequest, options.Collection())
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"id": 1},
Options: options.Index().SetUnique(true).SetSparse(true),
},
}, options.CreateIndexes())
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"email": 1, "identifier": 1},
@@ -75,13 +65,6 @@ func initMongodb() (*mongo.Database, error) {
}, options.CreateIndexes())
mongodb.CreateCollection(ctx, Collections.Session, options.CreateCollection())
sessionCollection := mongodb.Collection(Collections.Session, options.Collection())
sessionCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"id": 1},
Options: options.Index().SetUnique(true).SetSparse(true),
},
}, options.CreateIndexes())
return mongodb, nil
}

View File

@@ -10,8 +10,7 @@ import (
)
type Session struct {
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
UserID string `gorm:"type:char(36)" json:"user_id" bson:"user_id"`
User User `json:"-" bson:"-"`
@@ -29,7 +28,6 @@ func (mgr *manager) AddSession(session Session) error {
if IsORMSupported {
session.Key = session.ID
// session.ObjectID = session.ID
res := mgr.sqlDB.Clauses(
clause.OnConflict{
DoNothing: true,
@@ -53,7 +51,6 @@ func (mgr *manager) AddSession(session Session) error {
if IsMongoDB {
session.Key = session.ID
// session.ObjectID = session.ID
session.CreatedAt = time.Now().Unix()
session.UpdatedAt = time.Now().Unix()
sessionCollection := mgr.mongodb.Collection(Collections.Session, options.Collection())

View File

@@ -17,22 +17,22 @@ type User struct {
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
Email string `gorm:"unique" json:"email" bson:"email"`
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
Password string `gorm:"type:text" json:"password" bson:"password"`
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
GivenName string `json:"given_name" bson:"given_name"`
FamilyName string `json:"family_name" bson:"family_name"`
MiddleName string `json:"middle_name" bson:"middle_name"`
Nickname string `json:"nickname" bson:"nickname"`
Gender string `json:"gender" bson:"gender"`
Birthdate string `json:"birthdate" bson:"birthdate"`
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number"`
PhoneNumberVerifiedAt int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
Picture string `gorm:"type:text" json:"picture" bson:"picture"`
Roles string `json:"roles" bson:"roles"`
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
Email string `gorm:"unique" json:"email" bson:"email"`
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
Password *string `gorm:"type:text" json:"password" bson:"password"`
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
GivenName *string `json:"given_name" bson:"given_name"`
FamilyName *string `json:"family_name" bson:"family_name"`
MiddleName *string `json:"middle_name" bson:"middle_name"`
Nickname *string `json:"nickname" bson:"nickname"`
Gender *string `json:"gender" bson:"gender"`
Birthdate *string `json:"birthdate" bson:"birthdate"`
PhoneNumber *string `gorm:"unique" json:"phone_number" bson:"phone_number"`
PhoneNumberVerifiedAt *int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
Picture *string `gorm:"type:text" json:"picture" bson:"picture"`
Roles string `json:"roles" bson:"roles"`
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
}
// AddUser function to add user even with email conflict
@@ -44,7 +44,6 @@ func (mgr *manager) AddUser(user User) (User, error) {
if IsORMSupported {
// copy id as value for fields required for mongodb & arangodb
user.Key = user.ID
// user.ObjectID = user.ID
result := mgr.sqlDB.Clauses(
clause.OnConflict{
UpdateAll: true,
@@ -67,14 +66,13 @@ func (mgr *manager) AddUser(user User) (User, error) {
return user, err
}
user.Key = meta.Key
// user.ObjectID = meta.ID.String()
user.ID = meta.ID.String()
}
if IsMongoDB {
user.CreatedAt = time.Now().Unix()
user.UpdatedAt = time.Now().Unix()
user.Key = user.ID
// user.ObjectID = user.ID
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
_, err := userCollection.InsertOne(nil, user)
if err != nil {
@@ -108,7 +106,7 @@ func (mgr *manager) UpdateUser(user User) (User, error) {
}
user.Key = meta.Key
// user.ObjectID = meta.ID.String()
user.ID = meta.ID.String()
}
if IsMongoDB {

View File

@@ -13,8 +13,7 @@ import (
)
type VerificationRequest struct {
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
Token string `gorm:"type:text" json:"token" bson:"token"`
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier" bson:"identifier"`
@@ -32,7 +31,6 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
if IsORMSupported {
// copy id as value for fields required for mongodb & arangodb
verification.Key = verification.ID
// verification.ObjectID = verification.ID
result := mgr.sqlDB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
@@ -54,14 +52,13 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
return verification, err
}
verification.Key = meta.Key
// verification.ObjectID = meta.ID.String()
verification.ID = meta.ID.String()
}
if IsMongoDB {
verification.CreatedAt = time.Now().Unix()
verification.UpdatedAt = time.Now().Unix()
verification.Key = verification.ID
// verification.ObjectID = verification.ID
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
_, err := verificationRequestCollection.InsertOne(nil, verification)
if err != nil {