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

mendersoftware / deployments / 890874106

pending completion
890874106

Pull #867

gitlab-ci

kjaskiewiczz
feat: make releases persistent in the database

Changelog: Title
Ticket: MEN-5180

Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #867: feat: make releases persistent in the database

81 of 239 new or added lines in 5 files covered. (33.89%)

1 existing line in 1 file now uncovered.

7174 of 9174 relevant lines covered (78.2%)

67.85 hits per line

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

33.85
/store/mongo/migration_1_2_15.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
        "fmt"
20
        "time"
21

22
        "github.com/mendersoftware/go-lib-micro/mongo/migrate"
23
        "github.com/pkg/errors"
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
        "github.com/mendersoftware/deployments/model"
29
)
30

31
type migration_1_2_15 struct {
32
        client *mongo.Client
33
        db     string
34
}
35

36
// Up intrduces special representation of artifact provides and index them.
37
func (m *migration_1_2_15) Up(from migrate.Version) error {
9✔
38
        ctx := context.Background()
9✔
39
        c := m.client.Database(m.db).Collection(CollectionImages)
9✔
40
        cr := m.client.Database(m.db).Collection(CollectionReleases)
9✔
41

9✔
42
        // create index for release name
9✔
43
        storage := NewDataStoreMongoWithClient(m.client)
9✔
44
        if err := storage.EnsureIndexes(
9✔
45
                m.db,
9✔
46
                CollectionReleases,
9✔
47
                IndexReleaseName,
9✔
48
        ); err != nil {
9✔
NEW
49
                return err
×
NEW
50
        }
×
51

52
        cursor, err := c.Find(ctx, bson.M{})
9✔
53
        if err != nil {
9✔
NEW
54
                return err
×
NEW
55
        }
×
56
        defer cursor.Close(ctx)
9✔
57
        var a model.Image
9✔
58
        for cursor.Next(ctx) {
9✔
NEW
59
                err = cursor.Decode(&a)
×
NEW
60
                if err != nil {
×
NEW
61
                        break
×
62
                }
63

NEW
64
                opt := &mopts.UpdateOptions{}
×
NEW
65
                upsert := true
×
NEW
66
                opt.Upsert = &upsert
×
NEW
67
                update := bson.M{
×
NEW
68
                        "$set": bson.M{
×
NEW
69
                                StorageKeyReleaseName:     a.ArtifactMeta.Name,
×
NEW
70
                                StorageKeyReleaseModified: time.Now(),
×
NEW
71
                        },
×
NEW
72
                        "$push": bson.M{StorageKeyReleaseArtifacts: a},
×
NEW
73
                }
×
NEW
74

×
NEW
75
                res, err := cr.UpdateOne(
×
NEW
76
                        ctx,
×
NEW
77
                        bson.M{
×
NEW
78
                                "$and": []bson.M{
×
NEW
79
                                        {
×
NEW
80
                                                StorageKeyReleaseName: a.ArtifactMeta.Name,
×
NEW
81
                                        },
×
NEW
82
                                        {
×
NEW
83
                                                StorageKeyReleaseArtifactsId: bson.M{
×
NEW
84
                                                        "$ne": a.Id,
×
NEW
85
                                                },
×
NEW
86
                                        },
×
NEW
87
                                }},
×
NEW
88
                        update,
×
NEW
89
                        opt,
×
NEW
90
                )
×
NEW
91
                if err != nil {
×
NEW
92
                        if mongo.IsDuplicateKeyError(err) {
×
NEW
93
                                continue
×
94
                        }
NEW
95
                        return err
×
96
                }
97

NEW
98
                if res.ModifiedCount != 1 && res.UpsertedCount != 1 {
×
NEW
99
                        return errors.New(fmt.Sprintf("failed to update release %s", a.ArtifactMeta.Name))
×
NEW
100
                }
×
101
        }
102
        if err = cursor.Err(); err != nil {
9✔
NEW
103
                return errors.WithMessage(err, "failed to decode artifact")
×
NEW
104
        }
×
105

106
        return nil
9✔
107
}
108

109
func (m *migration_1_2_15) Version() migrate.Version {
31✔
110
        return migrate.MakeVersion(1, 2, 15)
31✔
111
}
31✔
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