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

mendersoftware / useradm / 1396224475

01 Aug 2024 02:01AM UTC coverage: 85.226%. Remained the same
1396224475

Pull #428

gitlab-ci

web-flow
chore: bump golang in the docker-dependencies group

Bumps the docker-dependencies group with 1 update: golang.


Updates `golang` from 1.22.4-alpine3.19 to 1.22.5-alpine3.19

---
updated-dependencies:
- dependency-name: golang
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: docker-dependencies
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #428: chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

2792 of 3276 relevant lines covered (85.23%)

49.04 hits per line

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

88.57
/store/mongo/migration_2_0_1.go
1
// Copyright 2022 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/google/uuid"
21
        "github.com/mendersoftware/go-lib-micro/mongo/migrate"
22
        mstore "github.com/mendersoftware/go-lib-micro/store/v2"
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
type migration_2_0_1 struct {
29
        ds     *DataStoreMongo
30
        dbName string
31
        ctx    context.Context
32
}
33

34
func isValidUUID(u string) bool {
4✔
35
        _, err := uuid.Parse(u)
4✔
36
        return err == nil
4✔
37
}
4✔
38

39
func (m *migration_2_0_1) Up(from migrate.Version) error {
114✔
40
        ctx := context.Background()
114✔
41

114✔
42
        collectionsIndexes := map[string]struct {
114✔
43
                Indexes []mongo.IndexModel
114✔
44
        }{
114✔
45
                DbUserSettingsColl: {
114✔
46
                        Indexes: []mongo.IndexModel{
114✔
47
                                {
114✔
48
                                        Keys: bson.D{
114✔
49
                                                {Key: mstore.FieldTenantID, Value: 1},
114✔
50
                                                {Key: DbSettingsUserID, Value: 1},
114✔
51
                                        },
114✔
52
                                        Options: mopts.Index().
114✔
53
                                                SetUnique(true).
114✔
54
                                                SetName(DbSettingsTenantIndexName),
114✔
55
                                },
114✔
56
                        },
114✔
57
                },
114✔
58
        }
114✔
59

114✔
60
        // for each collection in main useradm database
114✔
61
        if m.dbName == DbName {
170✔
62
                for collection, indexModel := range collectionsIndexes {
112✔
63
                        coll := m.ds.client.Database(m.dbName).Collection(collection)
56✔
64
                        // drop all the existing indexes, ignoring the errors
56✔
65
                        _, _ = coll.Indexes().DropAll(ctx)
56✔
66

56✔
67
                        // create the new indexes
56✔
68
                        if len(indexModel.Indexes) != 0 {
112✔
69
                                _, err := coll.Indexes().CreateMany(ctx, indexModel.Indexes)
56✔
70
                                if err != nil {
56✔
71
                                        return err
×
72
                                }
×
73
                        }
74
                }
75

76
                // migrate user settings to the dedicated collection
77
                coll := m.ds.client.Database(m.dbName).Collection(DbSettingsColl)
56✔
78
                opts := &mopts.FindOptions{}
56✔
79
                opts.SetSort(bson.D{{Key: "_id", Value: 1}})
56✔
80
                cur, err := coll.Find(ctx, bson.M{}, opts)
56✔
81
                if err != nil {
56✔
82
                        return err
×
83
                }
×
84

85
                usersColl := m.ds.client.Database(m.dbName).Collection(DbUserSettingsColl)
56✔
86

56✔
87
                defer cur.Close(ctx)
56✔
88

56✔
89
                // migrate the documents
56✔
90
                for cur.Next(ctx) {
57✔
91
                        item := map[string]interface{}{}
1✔
92
                        err := cur.Decode(&item)
1✔
93
                        if err != nil {
1✔
94
                                return err
×
95
                        }
×
96
                        for key, value := range item {
5✔
97
                                if isValidUUID(key) {
5✔
98
                                        valueMap, ok := value.(map[string]interface{})
1✔
99
                                        if ok {
2✔
100
                                                valueMap[DbSettingsUserID] = key
1✔
101
                                                valueMap[mstore.FieldTenantID] = item[mstore.FieldTenantID]
1✔
102
                                                _, err = usersColl.InsertOne(ctx, valueMap)
1✔
103
                                                if err != nil {
1✔
104
                                                        return err
×
105
                                                }
×
106
                                        }
107
                                }
108
                        }
109
                }
110
        }
111

112
        return nil
114✔
113
}
114

115
func (m *migration_2_0_1) Version() migrate.Version {
363✔
116
        return migrate.MakeVersion(2, 0, 1)
363✔
117
}
363✔
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