generated from PackagrIO/goweb-template
/
gorm_repository_migrations.go
226 lines (197 loc) · 8.12 KB
/
gorm_repository_migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package database
import (
"context"
"fmt"
_20231017112246 "github.com/fastenhealth/fasten-onprem/backend/pkg/database/migrations/20231017112246"
_20231201122541 "github.com/fastenhealth/fasten-onprem/backend/pkg/database/migrations/20231201122541"
_0240114092806 "github.com/fastenhealth/fasten-onprem/backend/pkg/database/migrations/20240114092806"
_20240114103850 "github.com/fastenhealth/fasten-onprem/backend/pkg/database/migrations/20240114103850"
_20240208112210 "github.com/fastenhealth/fasten-onprem/backend/pkg/database/migrations/20240208112210"
"github.com/fastenhealth/fasten-onprem/backend/pkg/models"
databaseModel "github.com/fastenhealth/fasten-onprem/backend/pkg/models/database"
sourceCatalog "github.com/fastenhealth/fasten-sources/catalog"
sourcePkg "github.com/fastenhealth/fasten-sources/pkg"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/google/uuid"
"gorm.io/gorm"
"log"
)
func (gr *GormRepository) Migrate() error {
gr.Logger.Infoln("Database migration starting. Please wait, this process may take a long time....")
gormMigrateOptions := gormigrate.DefaultOptions
gormMigrateOptions.UseTransaction = true
//use "echo $(date '+%Y%m%d%H%M%S')" to generate new ID's
m := gormigrate.New(gr.GormClient, gormMigrateOptions, []*gormigrate.Migration{
{
ID: "20231017112246",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&_20231017112246.BackgroundJob{},
&_20231017112246.Glossary{},
&_20231017112246.SourceCredential{},
&_20231017112246.UserSettingEntry{},
&_20231017112246.User{},
)
},
},
{
ID: "20231017113858", // FHIR Resource Database models.
Migrate: func(tx *gorm.DB) error {
//automigrate Fhir Resource Tables
return databaseModel.Migrate(tx)
},
},
{
ID: "20231201122541", // Adding Fasten Source Credential for each user
Migrate: func(tx *gorm.DB) error {
users := []_20231201122541.User{}
results := tx.Find(&users)
if results.Error != nil {
return results.Error
}
for _, user := range users {
tx.Logger.Info(context.Background(), fmt.Sprintf("Creating Fasten Source Credential for user: %s", user.ID))
fastenUserCred := _20231201122541.SourceCredential{
UserID: user.ID,
SourceType: string(sourcePkg.PlatformTypeFasten),
}
fastenUserCredCreateResp := tx.Create(&fastenUserCred)
if fastenUserCredCreateResp.Error != nil {
tx.Logger.Error(context.Background(), fmt.Sprintf("An error occurred creating Fasten Source Credential for user: %s", user.ID))
return fastenUserCredCreateResp.Error
}
}
return nil
},
},
{
ID: "20240114092806", // Adding additional fields to Source Credential
Migrate: func(tx *gorm.DB) error {
err := tx.AutoMigrate(
&_0240114092806.SourceCredential{},
)
if err != nil {
return err
}
//attempt to populate the endpoint id, portal id and brand id for each existing source credential
sourceCredentials := []_0240114092806.SourceCredential{}
results := tx.Find(&sourceCredentials)
if results.Error != nil {
return results.Error
}
for ndx, _ := range sourceCredentials {
sourceCredential := &sourceCredentials[ndx]
if sourceCredential.SourceType == string(sourcePkg.PlatformTypeFasten) || sourceCredential.SourceType == string(sourcePkg.PlatformTypeManual) {
tx.Logger.Info(context.Background(), fmt.Sprintf("Updating Legacy SourceType (%s) to PlatformType: %s", sourceCredential.SourceType, sourceCredential.ID))
sourceCredential.PlatformType = string(sourceCredential.SourceType)
fastenUpdateSourceCredential := tx.Save(sourceCredential)
if fastenUpdateSourceCredential.Error != nil {
tx.Logger.Error(context.Background(), fmt.Sprintf("An error occurred update Fasten Source Credential: %s", sourceCredential.ID))
return fastenUpdateSourceCredential.Error
}
continue
}
tx.Logger.Info(context.Background(), fmt.Sprintf("Mapping Legacy SourceType (%s) to Brand, Portal and Endpoint IDs: %s", sourceCredential.SourceType, sourceCredential.ID))
matchingBrand, matchingPortal, matchingEndpoint, endpointEnv, err := sourceCatalog.GetPatientAccessInfoForLegacySourceType(sourceCredential.SourceType, sourceCredential.ApiEndpointBaseUrl)
if err != nil {
log.Printf("An error occurred getting Patient Access Info for Legacy SourceType: %s", sourceCredential.SourceType)
tx.Logger.Error(context.Background(), err.Error())
return err
}
portalId := uuid.MustParse(matchingPortal.Id)
sourceCredential.PortalID = &portalId
brandId := uuid.MustParse(matchingBrand.Id)
sourceCredential.Display = matchingPortal.Name
sourceCredential.BrandID = &brandId
sourceCredential.EndpointID = uuid.MustParse(matchingEndpoint.Id)
sourceCredential.PlatformType = string(matchingEndpoint.GetPlatformType())
sourceCredential.LighthouseEnvType = endpointEnv
fastenUpdateSourceCredential := tx.Save(sourceCredential)
if fastenUpdateSourceCredential.Error != nil {
tx.Logger.Error(context.Background(), fmt.Sprintf("An error occurred update Fasten Source Credential: %s", sourceCredential.ID))
return fastenUpdateSourceCredential.Error
}
}
return nil
},
},
{
ID: "20240114103850", // cleanup unnecessary fields, now that we're using Brands, Portals and Endpoints.
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&_20240114103850.SourceCredential{},
)
},
},
{
ID: "20240208112210", // add system settings
Migrate: func(tx *gorm.DB) error {
err := tx.AutoMigrate(
&_20240208112210.SystemSettingEntry{},
)
if err != nil {
return err
}
//add the default system settings
defaultSystemSettings := []_20240208112210.SystemSettingEntry{
{
SettingKeyName: "installation_id",
SettingKeyDescription: "installation id is used to identify this installation when making external calls to Fasten Health, Inc. infrastructure. It does not contain any personally identifiable information",
SettingDataType: "string",
SettingValueString: "",
},
{
SettingKeyName: "installation_secret",
SettingKeyDescription: "installation secret is used to sign requests/updates to Fasten Health, Inc. infrastructure",
SettingDataType: "string",
SettingValueString: "",
},
}
for _, setting := range defaultSystemSettings {
tx.Logger.Info(context.Background(), fmt.Sprintf("Creating System Setting: %s", setting.SettingKeyName))
settingCreateResp := tx.Create(&setting)
if settingCreateResp.Error != nil {
tx.Logger.Error(context.Background(), fmt.Sprintf("An error occurred creating System Setting: %s", setting.SettingKeyName))
return settingCreateResp.Error
}
}
return nil
},
},
{
ID: "20240212142126", // remove unnecessary admin user if present.
Migrate: func(tx *gorm.DB) error {
deleteResp := tx.Delete(&models.User{}, "username = ?", "admin")
if deleteResp.Error != nil {
tx.Logger.Error(context.Background(), fmt.Sprintf("An error occurred while removing placeholder admin user: %v", deleteResp.Error))
return deleteResp.Error
}
return nil
},
},
})
// run when database is empty
//m.InitSchema(func(tx *gorm.DB) error {
// err := tx.AutoMigrate(
// &models.BackgroundJob{},
// &models.Glossary{},
// &models.SourceCredential{},
// &models.UserSettingEntry{},
// &models.User{},
// )
// if err != nil {
// return err
// }
// return nil
//})
if err := m.Migrate(); err != nil {
gr.Logger.Errorf("Database migration failed with error. \n Please open a github issue at https://github.com/fastenhealth/fasten-onprem. \n %v", err)
return err
}
//TODO: final migration step. This should not be necessary once we do true migrations for the databaseModels.
if err := databaseModel.Migrate(gr.GormClient); err != nil {
gr.Logger.Errorf("Final Database migration failed with error.\n Please open a github issue at https://github.com/fastenhealth/fasten-onprem. \n %v", err)
}
gr.Logger.Infoln("Database migration completed successfully")
return nil
}