-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add migration to convert usernames and emails to lowercase
- Loading branch information
1 parent
768463c
commit 0eecce7
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
migrate "github.com/xakep666/mongo-migrate" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
var migration_24 = migrate.Migration{ | ||
Version: 24, | ||
Description: "convert names and emails to lowercase", | ||
Up: func(db *mongo.Database) error { | ||
logrus.Info("Applying migration 24 - Up") | ||
if _, err := db.Collection("users").UpdateMany(context.TODO(), bson.D{}, []bson.M{ | ||
{ | ||
"$set": bson.M{"username": bson.M{"$toLower": "$username"}, | ||
"email": bson.M{"$toLower": "$email"}}, | ||
}, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
_, err := db.Collection("namespaces").UpdateMany(context.TODO(), bson.D{}, []bson.M{ | ||
{ | ||
"$set": bson.M{"name": bson.M{"$toLower": "$name"}}, | ||
}, | ||
}) | ||
|
||
return err | ||
}, | ||
Down: func(db *mongo.Database) error { | ||
logrus.Info("Applying migration 24 - Down") | ||
return nil | ||
}, | ||
} |