Skip to content

Commit

Permalink
Merge pull request #202 from SkynetLabs/ivo/fix_dropping_indexes
Browse files Browse the repository at this point in the history
Ignore "IndexNotFound" errors and log the rest without panicking.
  • Loading branch information
MSevey committed Apr 22, 2022
2 parents aaaddb3 + 9be7072 commit e9e215a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,18 @@ func ensureDBSchema(ctx context.Context, db *mongo.Database, schema map[string][
}
// Drop indexes we no longer need.
_, err = db.Collection(collUsers).Indexes().DropOne(ctx, "email_unique")
// Ignore namespace not found errors.
if err != nil && !strings.Contains(err.Error(), "NamespaceNotFound") {
return err
// We want to ignore IndexNotFound errors - we'll have that each time we run
// this code after the initial run on which we drop the index.
// We also want to ignore NamespaceNotFound errors - we'll have that on the
// very first run of the service when users collection doesn't exist, yet.
// We don't want to worry new portal operators and waste their time.
// All other errors we want to log for informational purposes but we don't
// want to return an error and prevent the service from running - if there
// is any issue with the database that would affect the operation of the
// service, it will surface during the next step where we ensure collections
// indexes exist.
if err != nil && !strings.Contains(err.Error(), "IndexNotFound") && !strings.Contains(err.Error(), "NamespaceNotFound") {
log.Debugf("Error while dropping index '%s': %v", "email_unique", err)
}
// Ensure current schema.
for collName, models := range schema {
Expand Down

0 comments on commit e9e215a

Please sign in to comment.