[go: nahoru, domu]

Skip to content

Commit

Permalink
api: add migration to convert usernames and emails to lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoveiga authored and gustavosbarreto committed Jun 11, 2021
1 parent 768463c commit 0eecce7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/store/mongo/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func GenerateMigrations() []migrate.Migration {
migration_21,
migration_22,
migration_23,
migration_24,
}
}

Expand Down
38 changes: 38 additions & 0 deletions api/store/mongo/migrations/migration_24.go
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
},
}

0 comments on commit 0eecce7

Please sign in to comment.