• 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

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 {
252✔
40
        logger := log.FromContext(m.ctx)
252✔
41
        ctx := context.Background()
252✔
42

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

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

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

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

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

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

453✔
147
                        // migrate the documents
453✔
148
                        for cur.Next(ctx) {
457✔
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 {
457✔
167
                                _, err := collOut.BulkWrite(ctx, writes)
4✔
168
                                if err != nil {
5✔
169
                                        return err
1✔
170
                                }
1✔
171
                        }
172
                }
173
        }
174
        return nil
251✔
175
}
176

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