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

mendersoftware / inventory / 1401701972

05 Aug 2024 08:32PM UTC coverage: 91.217%. Remained the same
1401701972

push

gitlab-ci

web-flow
Merge pull request #460 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3095 of 3393 relevant lines covered (91.22%)

148.68 hits per line

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

85.62
/store/mongo/migrations.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/pkg/errors"
21

22
        "github.com/mendersoftware/go-lib-micro/identity"
23
        "github.com/mendersoftware/go-lib-micro/log"
24
        "github.com/mendersoftware/go-lib-micro/mongo/migrate"
25
        mstore "github.com/mendersoftware/go-lib-micro/store"
26

27
        "github.com/mendersoftware/inventory/store"
28
)
29

30
// WithAutomigrate enables automatic migration and returns a new datastore based
31
// on current one
32
func (db *DataStoreMongo) WithAutomigrate() store.DataStore {
13✔
33
        return &DataStoreMongo{
13✔
34
                client:      db.client,
13✔
35
                automigrate: true,
13✔
36
        }
13✔
37
}
13✔
38

39
func (db *DataStoreMongo) MigrateTenant(
40
        ctx context.Context,
41
        version string,
42
        tenantId string,
43
) error {
16✔
44
        l := log.FromContext(ctx)
16✔
45

16✔
46
        database := mstore.DbNameForTenant(tenantId, DbName)
16✔
47

16✔
48
        l.Infof("migrating %s", database)
16✔
49

16✔
50
        m := migrate.SimpleMigrator{
16✔
51
                Client:      db.client,
16✔
52
                Db:          database,
16✔
53
                Automigrate: db.automigrate,
16✔
54
        }
16✔
55

16✔
56
        ver, err := migrate.NewVersion(version)
16✔
57
        if err != nil {
16✔
58
                return errors.Wrap(err, "failed to parse service version")
×
59
        }
×
60

61
        ctx = identity.WithContext(ctx, &identity.Identity{
16✔
62
                Tenant: tenantId,
16✔
63
        })
16✔
64

16✔
65
        migrations := []migrate.Migration{
16✔
66
                &migration_0_2_0{
16✔
67
                        ms:  db,
16✔
68
                        ctx: ctx,
16✔
69
                },
16✔
70
                &migration_1_0_0{
16✔
71
                        ms:  db,
16✔
72
                        ctx: ctx,
16✔
73
                },
16✔
74
                &migration_1_0_1{
16✔
75
                        ms:  db,
16✔
76
                        ctx: ctx,
16✔
77
                },
16✔
78
                &migration_1_0_2{
16✔
79
                        ms:  db,
16✔
80
                        ctx: ctx,
16✔
81
                },
16✔
82
                &migration_1_1_0{
16✔
83
                        ms:  db,
16✔
84
                        ctx: ctx,
16✔
85
                },
16✔
86
        }
16✔
87

16✔
88
        err = m.Apply(ctx, *ver, migrations)
16✔
89
        if err != nil {
17✔
90
                return errors.Wrap(err, "failed to apply migrations")
1✔
91
        }
1✔
92

93
        return nil
15✔
94
}
95

96
func (db *DataStoreMongo) Migrate(ctx context.Context, version string) error {
11✔
97
        l := log.FromContext(ctx)
11✔
98

11✔
99
        dbs, err := migrate.GetTenantDbs(ctx, db.client, mstore.IsTenantDb(DbName))
11✔
100
        if err != nil {
11✔
101
                return errors.Wrap(err, "failed go retrieve tenant DBs")
×
102
        }
×
103

104
        if len(dbs) == 0 {
20✔
105
                dbs = []string{DbName}
9✔
106
        }
9✔
107

108
        if db.automigrate {
21✔
109
                l.Infof("automigrate is ON, will apply migrations")
10✔
110
        } else {
11✔
111
                l.Infof("automigrate is OFF, will check db version compatibility")
1✔
112
        }
1✔
113

114
        for _, d := range dbs {
22✔
115
                l.Infof("migrating %s", d)
11✔
116

11✔
117
                tenantId := mstore.TenantFromDbName(d, DbName)
11✔
118

11✔
119
                if err := db.MigrateTenant(ctx, version, tenantId); err != nil {
12✔
120
                        return err
1✔
121
                }
1✔
122
        }
123
        return nil
10✔
124
}
125

126
func (db *DataStoreMongo) upgradeTenant(
127
        ctx context.Context,
128
        version string,
129
) error {
3✔
130
        var migration migrate.Migration
3✔
131
        _, err := migrate.NewVersion(version)
3✔
132
        if err != nil {
3✔
133
                return err
×
134
        }
×
135

136
        switch version {
3✔
137
        case "1.0.0":
3✔
138
                migration = &migration_1_0_0{
3✔
139
                        ms:          db,
3✔
140
                        ctx:         ctx,
3✔
141
                        maintenance: true,
3✔
142
                }
3✔
143
        default:
×
144
                return errors.Errorf(
×
145
                        "migration version %s does not provide "+
×
146
                                "a maintenance interface",
×
147
                        version,
×
148
                )
×
149
        }
150
        tenantDB := mstore.DbFromContext(ctx, DbName)
3✔
151

3✔
152
        migrationInfo, err := migrate.GetMigrationInfo(
3✔
153
                ctx, db.client, tenantDB,
3✔
154
        )
3✔
155
        if err != nil {
3✔
156
                return errors.Wrap(err, "failed to fetch migration info")
×
157
        }
×
158
        migrationVersion := migrate.Version{Major: 0, Minor: 0, Patch: 0}
3✔
159
        if len(migrationInfo) > 0 {
6✔
160
                migrationVersion = migrationInfo[0].Version
3✔
161
        }
3✔
162
        err = migration.Up(migrationVersion)
3✔
163
        if err != nil {
3✔
164
                return err
×
165
        }
×
166
        return nil
3✔
167

168
}
169

170
func (db *DataStoreMongo) Maintenance(
171
        ctx context.Context,
172
        version string,
173
        tenantIDs ...string,
174
) error {
3✔
175
        l := log.FromContext(ctx)
3✔
176

3✔
177
        if len(tenantIDs) > 0 {
4✔
178
                for _, tid := range tenantIDs {
2✔
179
                        tenantCTX := identity.WithContext(ctx,
1✔
180
                                &identity.Identity{
1✔
181
                                        Tenant: tid,
1✔
182
                                },
1✔
183
                        )
1✔
184
                        err := db.upgradeTenant(tenantCTX, version)
1✔
185
                        if err != nil {
1✔
186
                                return err
×
187
                        }
×
188
                }
189
        } else {
2✔
190
                dbs, err := migrate.GetTenantDbs(
2✔
191
                        ctx, db.client, mstore.IsTenantDb(DbName),
2✔
192
                )
2✔
193
                if err != nil {
2✔
194
                        return errors.Wrap(err, "failed to retrieve tenant DBs")
×
195
                }
×
196
                if len(dbs) == 0 {
4✔
197
                        dbs = []string{DbName}
2✔
198
                }
2✔
199
                for _, d := range dbs {
4✔
200
                        tenantID := mstore.TenantFromDbName(d, DbName)
2✔
201
                        l.Infof("Updating DB: %s", d)
2✔
202
                        tenantCTX := identity.WithContext(
2✔
203
                                ctx,
2✔
204
                                &identity.Identity{
2✔
205
                                        Tenant: tenantID,
2✔
206
                                },
2✔
207
                        )
2✔
208
                        err := db.upgradeTenant(tenantCTX, version)
2✔
209
                        if err != nil {
2✔
210
                                return err
×
211
                        }
×
212
                }
213
        }
214
        return nil
3✔
215
}
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