• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / useradm / 1565757771

29 Nov 2024 07:56AM UTC coverage: 87.019%. Remained the same
1565757771

push

gitlab-ci

web-flow
Merge pull request #434 from alfrunes/1.22.x

chore(deps): Upgrade golang to latest

2869 of 3297 relevant lines covered (87.02%)

131.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/store/mongo/migration_1_1_4.go
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package mongo
16

17
import (
18
        "context"
19

20
        "github.com/mendersoftware/go-lib-micro/mongo/migrate"
21
        mstore "github.com/mendersoftware/go-lib-micro/store"
22
        "github.com/pkg/errors"
23
        "go.mongodb.org/mongo-driver/bson"
24
        "go.mongodb.org/mongo-driver/mongo"
25
        mopts "go.mongodb.org/mongo-driver/mongo/options"
26
)
27

28
const (
29
        OldDbUniqueEmailIndexName = "uniqueEmail"
30
)
31

32
type migration_1_1_4 struct {
33
        ds  *DataStoreMongo
34
        ctx context.Context
35
}
36

37
const ErrCodeIndexOptionsError = 85
38

39
func (m *migration_1_1_4) Up(from migrate.Version) error {
248✔
40
        c := m.ds.client.Database(mstore.DbFromContext(m.ctx, DbName)).
248✔
41
                Collection(DbUsersColl)
248✔
42

248✔
43
        idxUsers := c.Indexes()
248✔
44

248✔
45
        // create new index with predictable name
248✔
46
        indexOptions := mopts.Index()
248✔
47
        indexOptions.SetUnique(true)
248✔
48
        indexOptions.SetName(DbUniqueEmailIndexName)
248✔
49

248✔
50
        uniqueEmailIndex := mongo.IndexModel{
248✔
51
                Keys:    bson.D{{Key: DbUserEmail, Value: 1}},
248✔
52
                Options: indexOptions,
248✔
53
        }
248✔
54

248✔
55
        _, err := idxUsers.CreateOne(m.ctx, uniqueEmailIndex)
248✔
56
        if err != nil {
250✔
57
                if mgoErr, ok := err.(mongo.CommandError); ok {
4✔
58
                        if mgoErr.Code == ErrCodeIndexOptionsError {
4✔
59
                                _, e := idxUsers.DropOne(m.ctx, OldDbUniqueEmailIndexName)
2✔
60
                                if e != nil {
2✔
61
                                        return errors.Wrap(err, e.Error())
×
62
                                }
×
63
                                _, err = idxUsers.CreateOne(m.ctx, uniqueEmailIndex)
2✔
64
                        }
65
                }
66
        }
67

68
        return err
248✔
69
}
70

71
func (m *migration_1_1_4) Version() migrate.Version {
768✔
72
        return migrate.MakeVersion(1, 1, 4)
768✔
73
}
768✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc