• 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

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 {
247✔
40
        ctx := context.Background()
247✔
41

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

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

100✔
67
                        // create the new indexes
100✔
68
                        if len(indexModel.Indexes) != 0 {
200✔
69
                                _, err := coll.Indexes().CreateMany(ctx, indexModel.Indexes)
100✔
70
                                if err != nil {
100✔
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)
100✔
78
                opts := &mopts.FindOptions{}
100✔
79
                opts.SetSort(bson.D{{Key: "_id", Value: 1}})
100✔
80
                cur, err := coll.Find(ctx, bson.M{}, opts)
100✔
81
                if err != nil {
100✔
82
                        return err
×
83
                }
×
84

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

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

100✔
89
                // migrate the documents
100✔
90
                for cur.Next(ctx) {
101✔
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
247✔
113
}
114

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