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

mendersoftware / mender-server / 1833359013

23 May 2025 01:25PM UTC coverage: 66.318% (+0.5%) from 65.861%
1833359013

Pull #674

gitlab-ci

mzedel
fix(gui): prevented device tag editor to be shown when no tags exist

- this is to reduce confusion about tags defined async to the current session not being visible

Ticket: ME-528
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #674: ME-529, ME-528 - adjustments to device tag editing

29554 of 44564 relevant lines covered (66.32%)

1.45 hits per line

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

80.85
/backend/pkg/mongo/migrate/db.go
1
// Copyright 2024 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 migrate
15

16
import (
17
        "context"
18
        "time"
19

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

25
        "github.com/mendersoftware/mender-server/pkg/store"
26
)
27

28
// this is a small internal data layer for the migration utils, may be shared by diff migrators
29
const (
30
        DbMigrationsColl = "migration_info"
31
)
32

33
type MigrationEntry struct {
34
        Version   Version   `bson:"version"`
35
        Timestamp time.Time `bson:"timestamp"`
36
}
37

38
// GetMigrationInfo retrieves a list of migrations applied to the db.
39
// On success the function returns the MigrationEntries present in the db
40
// sorted by the version in decending order.
41
func GetMigrationInfo(
42
        ctx context.Context,
43
        sess *mongo.Client,
44
        db string,
45
) ([]MigrationEntry, error) {
9✔
46
        c := sess.Database(db).Collection(DbMigrationsColl)
9✔
47
        findOpts := mopts.Find()
9✔
48
        findOpts.SetSort(bson.D{{
9✔
49
                Key: "version.major", Value: -1,
9✔
50
        }, {
9✔
51
                Key: "version.minor", Value: -1,
9✔
52
        }, {
9✔
53
                Key: "version.patch", Value: -1,
9✔
54
        }})
9✔
55

9✔
56
        cursor, err := c.Find(ctx, bson.M{
9✔
57
                "version": bson.M{"$exists": true},
9✔
58
        }, findOpts)
9✔
59
        if cursor == nil || err != nil {
9✔
60
                return nil, errors.Wrap(err, "db: failed to get migration info")
×
61
        }
×
62

63
        var infoArray []MigrationEntry
9✔
64
        err = cursor.All(ctx, &infoArray)
9✔
65
        return infoArray, err
9✔
66
}
67

68
// UpdateMigrationInfo inserts a migration entry in the migration info collection.
69
func UpdateMigrationInfo(
70
        ctx context.Context,
71
        version Version,
72
        sess *mongo.Client,
73
        db string,
74
) error {
9✔
75
        c := sess.Database(db).Collection(DbMigrationsColl)
9✔
76

9✔
77
        entry := MigrationEntry{
9✔
78
                Version:   version,
9✔
79
                Timestamp: time.Now(),
9✔
80
        }
9✔
81
        _, err := c.InsertOne(ctx, entry)
9✔
82
        if err != nil {
9✔
83
                return errors.Wrap(err, "db: failed to insert migration info")
×
84
        }
×
85
        // result.InsertedID (InsertOneResult)
86

87
        return nil
9✔
88
}
89

90
func GetTenantDbs(
91
        ctx context.Context,
92
        client *mongo.Client,
93
        matcher store.TenantDbMatchFunc,
94
) ([]string, error) {
5✔
95
        result, err := client.ListDatabaseNames(ctx, bson.M{})
5✔
96
        if err != nil {
5✔
97
                return nil, err
×
98
        }
×
99

100
        tenantDbs := make([]string, len(result))
5✔
101
        j := 0
5✔
102
        for _, db := range result {
10✔
103
                if matcher(db) {
5✔
104
                        tenantDbs[j] = db
×
105
                        j++
×
106
                }
×
107
        }
108
        tenantDbs = tenantDbs[:j]
5✔
109

5✔
110
        return tenantDbs, nil
5✔
111
}
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