• 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

89.92
/store/mongo/migration_2_0_0.go
1
// Copyright 2023 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/identity"
21
        "github.com/mendersoftware/go-lib-micro/log"
22
        "github.com/mendersoftware/go-lib-micro/mongo/migrate"
23
        mstore "github.com/mendersoftware/go-lib-micro/store/v2"
24
        "go.mongodb.org/mongo-driver/bson"
25
        "go.mongodb.org/mongo-driver/mongo"
26
        mopts "go.mongodb.org/mongo-driver/mongo/options"
27
)
28

29
const (
30
        findBatchSize = 100
31
)
32

33
type migration_2_0_0 struct {
34
        ds     *DataStoreMongo
35
        dbName string
36
        ctx    context.Context
37
}
38

39
func (m *migration_2_0_0) Up(from migrate.Version) error {
119✔
40
        logger := log.FromContext(m.ctx)
119✔
41
        ctx := context.Background()
119✔
42

119✔
43
        collectionsIndexes := map[string]struct {
119✔
44
                Indexes []mongo.IndexModel
119✔
45
        }{
119✔
46
                DbUsersColl: {
119✔
47
                        Indexes: []mongo.IndexModel{
119✔
48
                                {
119✔
49
                                        Keys: bson.D{{Key: DbUserEmail, Value: 1}},
119✔
50
                                        Options: mopts.Index().
119✔
51
                                                SetUnique(true).
119✔
52
                                                SetName(DbUniqueEmailIndexName),
119✔
53
                                },
119✔
54
                        },
119✔
55
                },
119✔
56
                DbTokensColl: {
119✔
57
                        Indexes: []mongo.IndexModel{
119✔
58
                                {
119✔
59
                                        Keys: bson.D{
119✔
60
                                                {Key: mstore.FieldTenantID, Value: 1},
119✔
61
                                                {Key: DbTokenSubject, Value: 1},
119✔
62
                                                {Key: DbTokenName, Value: 1},
119✔
63
                                        },
119✔
64
                                        Options: mopts.Index().
119✔
65
                                                SetUnique(true).
119✔
66
                                                SetPartialFilterExpression(
119✔
67
                                                        bson.M{
119✔
68
                                                                DbTokenName: bson.M{"$exists": true},
119✔
69
                                                        }).
119✔
70
                                                SetName(DbTenantUniqueTokenNameIndexName),
119✔
71
                                },
119✔
72
                                {
119✔
73
                                        Keys: bson.D{
119✔
74
                                                {Key: mstore.FieldTenantID, Value: 1},
119✔
75
                                                {Key: DbTokenSubject, Value: 1},
119✔
76
                                        },
119✔
77
                                        Options: mopts.Index().
119✔
78
                                                SetName(DbTenantTokenSubjectIndexName),
119✔
79
                                },
119✔
80
                        },
119✔
81
                },
119✔
82
                DbSettingsColl: {
119✔
83
                        Indexes: []mongo.IndexModel{
119✔
84
                                {
119✔
85
                                        Keys: bson.D{
119✔
86
                                                {Key: mstore.FieldTenantID, Value: 1},
119✔
87
                                        },
119✔
88
                                        Options: mopts.Index().
119✔
89
                                                SetUnique(true).
119✔
90
                                                SetName(DbSettingsTenantIndexName),
119✔
91
                                },
119✔
92
                        },
119✔
93
                },
119✔
94
        }
119✔
95

119✔
96
        // for each collection in main useradm database
119✔
97
        if m.dbName == DbName {
176✔
98
                for collection, indexModel := range collectionsIndexes {
228✔
99
                        coll := m.ds.client.Database(m.dbName).Collection(collection)
171✔
100
                        // drop all the existing indexes, ignoring the errors
171✔
101
                        _, _ = coll.Indexes().DropAll(ctx)
171✔
102

171✔
103
                        // create the new indexes
171✔
104
                        if len(indexModel.Indexes) != 0 {
342✔
105
                                _, err := coll.Indexes().CreateMany(ctx, indexModel.Indexes)
171✔
106
                                if err != nil {
171✔
107
                                        return err
×
108
                                }
×
109
                        }
110
                }
111
        }
112

113
        tenantID := mstore.TenantFromDbName(m.dbName, DbName)
119✔
114
        ctx = identity.WithContext(ctx, &identity.Identity{
119✔
115
                Tenant: tenantID,
119✔
116
        })
119✔
117

119✔
118
        // for each collection
119✔
119
        for collection := range collectionsIndexes {
474✔
120
                coll := m.ds.client.Database(m.dbName).Collection(collection)
355✔
121
                collOut := m.ds.client.Database(DbName).Collection(collection)
355✔
122
                writes := make([]mongo.WriteModel, 0, findBatchSize)
355✔
123

355✔
124
                if m.dbName == DbName {
526✔
125
                        // if any documents already exist in "useradm" ds,
171✔
126
                        // add empty "tenant_id": "" key-value pair
171✔
127
                        tenantIdFilter := bson.D{
171✔
128
                                {Key: mstore.FieldTenantID, Value: bson.D{{Key: "$exists", Value: false}}}}
171✔
129
                        update := bson.M{"$set": bson.M{mstore.FieldTenantID: ""}}
171✔
130
                        result, err := collOut.UpdateMany(ctx, tenantIdFilter, update)
171✔
131
                        logger.Debugf("Modified documents in main useradm database count: %d",
171✔
132
                                result.ModifiedCount)
171✔
133
                        if err != nil {
171✔
134
                                return err
×
135
                        }
×
136
                } else {
184✔
137
                        // get all the documents in the collection
184✔
138
                        findOptions := mopts.Find().
184✔
139
                                SetBatchSize(findBatchSize).
184✔
140
                                SetSort(bson.D{{Key: "_id", Value: 1}})
184✔
141
                        cur, err := coll.Find(ctx, bson.D{}, findOptions)
184✔
142
                        if err != nil {
184✔
143
                                return err
×
144
                        }
×
145
                        defer cur.Close(ctx)
184✔
146

184✔
147
                        // migrate the documents
184✔
148
                        for cur.Next(ctx) {
188✔
149
                                item := bson.D{}
4✔
150
                                err := cur.Decode(&item)
4✔
151
                                if err != nil {
4✔
152
                                        return err
×
153
                                }
×
154

155
                                item = mstore.WithTenantID(ctx, item)
4✔
156
                                writes = append(writes, mongo.NewInsertOneModel().SetDocument(item))
4✔
157

4✔
158
                                if len(writes) == findBatchSize {
4✔
159
                                        _, err := collOut.BulkWrite(ctx, writes)
×
160
                                        if err != nil {
×
161
                                                return err
×
162
                                        }
×
163
                                        writes = writes[:0]
×
164
                                }
165
                        }
166
                        if len(writes) > 0 {
188✔
167
                                _, err := collOut.BulkWrite(ctx, writes)
4✔
168
                                if err != nil {
5✔
169
                                        return err
1✔
170
                                }
1✔
171
                        }
172
                }
173
        }
174
        return nil
118✔
175
}
176

177
func (m *migration_2_0_0) Version() migrate.Version {
368✔
178
        return migrate.MakeVersion(2, 0, 0)
368✔
179
}
368✔
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