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

mendersoftware / deployments / 1197570064

01 Mar 2024 06:24PM UTC coverage: 52.222% (-28.4%) from 80.645%
1197570064

Pull #998

gitlab-ci

web-flow
chore: bump github.com/Azure/azure-sdk-for-go/sdk/azcore

Bumps [github.com/Azure/azure-sdk-for-go/sdk/azcore](https://github.com/Azure/azure-sdk-for-go) from 1.9.1 to 1.10.0.
- [Release notes](https://github.com/Azure/azure-sdk-for-go/releases)
- [Changelog](https://github.com/Azure/azure-sdk-for-go/blob/main/documentation/release.md)
- [Commits](https://github.com/Azure/azure-sdk-for-go/compare/sdk/azcore/v1.9.1...sdk/azcore/v1.10.0)

---
updated-dependencies:
- dependency-name: github.com/Azure/azure-sdk-for-go/sdk/azcore
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #998: chore: bump github.com/Azure/azure-sdk-for-go/sdk/azcore from 1.9.1 to 1.10.0

5218 of 9992 relevant lines covered (52.22%)

0.55 hits per line

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

70.29
/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
func (m *migration_1_2_15) Up(from migrate.Version) error {
1✔
37
        err := m.createCollectionReleases()
1✔
38
        if err == nil {
2✔
39
                err = m.indexReleaseTags()
1✔
40
        }
1✔
41
        if err == nil {
2✔
42
                err = m.indexReleaseUpdateType()
1✔
43
        }
1✔
44
        if err == nil {
2✔
45
                err = m.indexUpdateTypes()
1✔
46
        }
1✔
47
        if err == nil {
2✔
48
                err = m.indexReleaseArtifactsCount()
1✔
49
        }
1✔
50
        return err
1✔
51
}
52

53
func (m *migration_1_2_15) createCollectionReleases() error {
1✔
54
        ctx := context.Background()
1✔
55
        c := m.client.Database(m.db).Collection(CollectionImages)
1✔
56
        cr := m.client.Database(m.db).Collection(CollectionReleases)
1✔
57

1✔
58
        cursor, err := c.Find(ctx, bson.M{})
1✔
59
        if err != nil {
1✔
60
                return err
×
61
        }
×
62
        defer cursor.Close(ctx)
1✔
63
        for cursor.Next(ctx) {
1✔
64
                var a model.Image
×
65
                err = cursor.Decode(&a)
×
66
                if err != nil {
×
67
                        break
×
68
                }
69

70
                opt := &mopts.UpdateOptions{}
×
71
                upsert := true
×
72
                opt.Upsert = &upsert
×
73
                update := bson.M{
×
74
                        "$set": bson.M{
×
75
                                StorageKeyReleaseName:     a.ArtifactMeta.Name,
×
76
                                StorageKeyReleaseModified: time.Now(),
×
77
                        },
×
78
                        "$push": bson.M{StorageKeyReleaseArtifacts: a},
×
79
                }
×
80

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

104
                if res.ModifiedCount != 1 && res.UpsertedCount != 1 {
×
105
                        return errors.New(fmt.Sprintf("failed to update release %s", a.ArtifactMeta.Name))
×
106
                }
×
107
        }
108
        if err = cursor.Err(); err != nil {
1✔
109
                return errors.WithMessage(err, "failed to decode artifact")
×
110
        }
×
111
        return nil
1✔
112
}
113

114
func (m *migration_1_2_15) indexReleaseArtifactsCount() error {
1✔
115
        ctx := context.Background()
1✔
116
        idxReleases := m.client.
1✔
117
                Database(m.db).
1✔
118
                Collection(CollectionReleases).
1✔
119
                Indexes()
1✔
120

1✔
121
        _, err := idxReleases.CreateOne(ctx, mongo.IndexModel{
1✔
122
                Keys: bson.D{
1✔
123
                        {
1✔
124
                                Key:   StorageKeyReleaseArtifactsCount,
1✔
125
                                Value: 1,
1✔
126
                        },
1✔
127
                },
1✔
128
                Options: mopts.Index().SetName(IndexNameReleaseArtifactsCount),
1✔
129
        })
1✔
130
        if err != nil {
1✔
131
                return fmt.Errorf("mongo(1.2.15): failed to create index: %w", err)
×
132
        }
×
133

134
        collectionReleases := m.client.
1✔
135
                Database(m.db).
1✔
136
                Collection(CollectionReleases)
1✔
137
        _, err = collectionReleases.UpdateMany(
1✔
138
                ctx,
1✔
139
                bson.M{},
1✔
140
                []bson.M{
1✔
141
                        {
1✔
142
                                "$set": bson.M{
1✔
143
                                        "artifacts_count": bson.M{
1✔
144
                                                "$size": "$artifacts",
1✔
145
                                        },
1✔
146
                                },
1✔
147
                        },
1✔
148
                },
1✔
149
        )
1✔
150
        if err != nil {
1✔
151
                return fmt.Errorf("mongo(1.2.15): failed to update adrtifact counts: %w", err)
×
152
        }
×
153

154
        return nil
1✔
155
}
156

157
func (m *migration_1_2_15) indexReleaseTags() error {
1✔
158
        ctx := context.Background()
1✔
159
        idxReleases := m.client.
1✔
160
                Database(m.db).
1✔
161
                Collection(CollectionReleases).
1✔
162
                Indexes()
1✔
163

1✔
164
        _, err := idxReleases.CreateOne(ctx, mongo.IndexModel{
1✔
165
                Keys: bson.D{{
1✔
166
                        Key:   StorageKeyReleaseTags,
1✔
167
                        Value: 1,
1✔
168
                }, {
1✔
169
                        // Sort by modified date by default when querying by tags
1✔
170
                        Key:   StorageKeyReleaseModified,
1✔
171
                        Value: -1,
1✔
172
                }},
1✔
173
                Options: mopts.Index().
1✔
174
                        SetName(IndexNameReleaseTags),
1✔
175
        })
1✔
176
        if err != nil {
1✔
177
                return fmt.Errorf("mongo(1.2.15): failed to create index: %w", err)
×
178
        }
×
179
        return nil
1✔
180
}
181

182
func (m *migration_1_2_15) indexReleaseUpdateType() error {
1✔
183
        ctx := context.Background()
1✔
184
        idxReleases := m.client.
1✔
185
                Database(m.db).
1✔
186
                Collection(CollectionReleases).
1✔
187
                Indexes()
1✔
188

1✔
189
        _, err := idxReleases.CreateOne(ctx, mongo.IndexModel{
1✔
190
                Keys: bson.D{
1✔
191
                        {
1✔
192
                                Key:   StorageKeyReleaseArtifactsUpdateTypes,
1✔
193
                                Value: 1,
1✔
194
                        },
1✔
195
                },
1✔
196
                Options: mopts.Index().
1✔
197
                        SetName(IndexNameReleaseUpdateTypes).
1✔
198
                        SetSparse(true),
1✔
199
        })
1✔
200
        if err != nil {
1✔
201
                return fmt.Errorf("mongo(1.2.17): failed to create index: %w", err)
×
202
        }
×
203
        return nil
1✔
204
}
205

206
func (m *migration_1_2_15) indexUpdateTypes() error {
1✔
207
        ctx := context.Background()
1✔
208
        idxReleases := m.client.
1✔
209
                Database(m.db).
1✔
210
                Collection(CollectionUpdateTypes).
1✔
211
                Indexes()
1✔
212

1✔
213
        _, err := idxReleases.CreateOne(ctx, mongo.IndexModel{
1✔
214
                Keys: bson.D{
1✔
215
                        {
1✔
216
                                Key:   StorageKeyTenantId,
1✔
217
                                Value: 1,
1✔
218
                        },
1✔
219
                },
1✔
220
                Options: mopts.Index().SetName(IndexNameAggregatedUpdateTypes).SetUnique(true),
1✔
221
        })
1✔
222
        if err != nil {
1✔
223
                return fmt.Errorf("mongo(1.2.18): failed to create index: %w", err)
×
224
        }
×
225
        return nil
1✔
226
}
227

228
func (m *migration_1_2_15) Version() migrate.Version {
1✔
229
        return migrate.MakeVersion(1, 2, 15)
1✔
230
}
1✔
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