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

mendersoftware / useradm / 1830670534

08 May 2025 10:54AM UTC coverage: 59.747% (-27.3%) from 87.019%
1830670534

push

gitlab-ci

alfrunes
Merge `alfrunes:1.22.x` into `mendersoftware:1.22.x`

2363 of 3955 relevant lines covered (59.75%)

12.76 hits per line

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

91.36
/store/mongo/migrations.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

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

26
        mstore_v1 "github.com/mendersoftware/go-lib-micro/store"
27
        mstore "github.com/mendersoftware/go-lib-micro/store/v2"
28
)
29

30
const (
31
        DbVersion = "2.0.3"
32
        DbName    = "useradm"
33
)
34

35
// MigrateTenant migrates a single tenant database.
36
func (db *DataStoreMongo) MigrateTenant(ctx context.Context, version string, tenant string) error {
27✔
37
        ver, err := migrate.NewVersion(version)
27✔
38
        if err != nil {
28✔
39
                return errors.Wrap(err, "failed to parse service version")
1✔
40
        }
1✔
41

42
        tenantCtx := identity.WithContext(ctx, &identity.Identity{
26✔
43
                Tenant: tenant,
26✔
44
        })
26✔
45

26✔
46
        m := migrate.SimpleMigrator{
26✔
47
                Client:      db.client,
26✔
48
                Db:          mstore_v1.DbFromContext(tenantCtx, DbName),
26✔
49
                Automigrate: db.automigrate,
26✔
50
        }
26✔
51
        migrations := []migrate.Migration{
26✔
52
                &migration_1_0_0{
26✔
53
                        ds:  db,
26✔
54
                        ctx: tenantCtx,
26✔
55
                },
26✔
56
                &migration_1_1_4{
26✔
57
                        ds:  db,
26✔
58
                        ctx: tenantCtx,
26✔
59
                },
26✔
60
                &migration_1_3_2{
26✔
61
                        ds:  db,
26✔
62
                        ctx: tenantCtx,
26✔
63
                },
26✔
64
                &migration_2_0_0{
26✔
65
                        ds:     db,
26✔
66
                        dbName: mstore_v1.DbFromContext(tenantCtx, DbName),
26✔
67
                        ctx:    tenantCtx,
26✔
68
                },
26✔
69
                &migration_2_0_1{
26✔
70
                        ds:     db,
26✔
71
                        dbName: mstore_v1.DbFromContext(tenantCtx, DbName),
26✔
72
                        ctx:    tenantCtx,
26✔
73
                },
26✔
74
                &migration_2_0_3{
26✔
75
                        ds:     db,
26✔
76
                        dbName: mstore_v1.DbFromContext(tenantCtx, DbName),
26✔
77
                        ctx:    tenantCtx,
26✔
78
                },
26✔
79
        }
26✔
80

26✔
81
        err = m.Apply(tenantCtx, *ver, migrations)
26✔
82
        if err != nil {
27✔
83
                return errors.Wrap(err, "failed to apply migrations")
1✔
84
        }
1✔
85
        return nil
25✔
86
}
87

88
func (db *DataStoreMongo) Migrate(ctx context.Context, version string) error {
27✔
89
        l := log.FromContext(ctx)
27✔
90

27✔
91
        dbs := []string{DbName}
27✔
92

27✔
93
        if db.multitenant {
27✔
94
                l.Infof("running migrations in multitenant mode")
×
95

×
96
                tdbs, err := migrate.GetTenantDbs(ctx, db.client, mstore.IsTenantDb(DbName))
×
97
                if err != nil {
×
98
                        return errors.Wrap(err, "failed go retrieve tenant DBs")
×
99
                }
×
100
                dbs = append(tdbs, DbName)
×
101
        } else {
27✔
102
                l.Infof("running migrations in single tenant mode")
27✔
103
        }
27✔
104

105
        if db.automigrate {
53✔
106
                l.Infof("automigrate is ON, will apply migrations")
26✔
107
        } else {
27✔
108
                l.Infof("automigrate is OFF, will check db version compatibility")
1✔
109
        }
1✔
110

111
        for _, d := range dbs {
54✔
112
                l.Infof("migrating %s", d)
27✔
113

27✔
114
                // if not in multi tenant, then tenant will be "" and identity
27✔
115
                // will be the same as default
27✔
116
                tenant := mstore.TenantFromDbName(d, DbName)
27✔
117

27✔
118
                if err := db.MigrateTenant(ctx, version, tenant); err != nil {
29✔
119
                        return err
2✔
120
                }
2✔
121
        }
122

123
        return nil
25✔
124
}
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