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

mendersoftware / mender-server / 1491991335

11 Oct 2024 01:37PM UTC coverage: 74.12% (+0.005%) from 74.115%
1491991335

push

gitlab-ci

web-flow
Merge pull request #98 from mzedel/fix/readme

fix: fixed an issue that prevented enterprise demo tenant creation

4399 of 6347 branches covered (69.31%)

Branch coverage included in aggregate %.

41997 of 56249 relevant lines covered (74.66%)

29.24 hits per line

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

84.09
/backend/services/deviceauth/store/mongo/migration_1_9_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
package mongo
15

16
import (
17
        "context"
18

19
        "github.com/pkg/errors"
20
        "go.mongodb.org/mongo-driver/bson"
21
        "go.mongodb.org/mongo-driver/mongo"
22
        mopts "go.mongodb.org/mongo-driver/mongo/options"
23

24
        "github.com/mendersoftware/mender-server/pkg/mongo/migrate"
25
        ctxstore "github.com/mendersoftware/mender-server/pkg/store"
26

27
        "github.com/mendersoftware/mender-server/services/deviceauth/model"
28
)
29

30
type migration_1_9_0 struct {
31
        ds  *DataStoreMongo
32
        ctx context.Context
33
}
34

35
// Up removes device and authset indexes which include raw id data
36
// and creates device index on id data sha256
37
func (m *migration_1_9_0) Up(from migrate.Version) error {
3✔
38
        _false := false
3✔
39
        _true := true
3✔
40

3✔
41
        // create device index on id data sha
3✔
42
        devIdDataSha256UniqueIndex := mongo.IndexModel{
3✔
43
                Keys: bson.D{
3✔
44
                        {Key: model.DevKeyIdDataSha256, Value: 1},
3✔
45
                },
3✔
46
                Options: &mopts.IndexOptions{
3✔
47
                        Background: &_false,
3✔
48
                        Name:       &indexDevices_IdentityDataSha256,
3✔
49
                        Unique:     &_true,
3✔
50
                },
3✔
51
        }
3✔
52

3✔
53
        cDevs := m.ds.client.Database(ctxstore.DbFromContext(m.ctx, DbName)).Collection(DbDevicesColl)
3✔
54
        devIndexes := cDevs.Indexes()
3✔
55
        _, err := devIndexes.CreateOne(m.ctx, devIdDataSha256UniqueIndex)
3✔
56
        if err != nil {
3✔
57
                return errors.Wrap(err, "failed to create unique index containing IdDataSha256 on devices")
×
58
        }
×
59

60
        // drop device index on raw identity data, if exists
61
        _, err = devIndexes.DropOne(m.ctx, indexDevices_IdentityData)
3✔
62
        if err != nil && !isIndexNotFound(err) {
3✔
63
                return errors.Wrap(err, "failed to drop index devices:IdentityData")
×
64
        }
×
65

66
        // drop authset index on raw identity data, if exists
67
        cAuthsets := m.ds.client.Database(ctxstore.DbFromContext(m.ctx, DbName)).
3✔
68
                Collection(DbAuthSetColl)
3✔
69
        asetIndexes := cAuthsets.Indexes()
3✔
70
        _, err = asetIndexes.DropOne(m.ctx, indexAuthSet_DeviceId_IdentityData_PubKey)
3✔
71
        if err != nil && !isIndexNotFound(err) {
3✔
72
                return errors.Wrap(err, "failed to drop index auth_sets:DeviceId:IdData:PubKey")
×
73
        }
×
74

75
        return nil
3✔
76
}
77

78
func (m *migration_1_9_0) Version() migrate.Version {
3✔
79
        return migrate.MakeVersion(1, 9, 0)
3✔
80
}
3✔
81

82
// ref: https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml
83
func isIndexNotFound(e error) bool {
1✔
84
        if mgoErr, ok := e.(mongo.CommandError); ok {
2✔
85
                if mgoErr.Code == 27 || // IndexNotFound - index does not exist
1✔
86
                        mgoErr.Code == 26 { // NamespaceNotFound - collection does not exist
2✔
87
                        return true
1✔
88
                }
1✔
89
        }
90
        return false
×
91
}
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