-
Notifications
You must be signed in to change notification settings - Fork 137
/
sqlite_gorm_client.go
256 lines (198 loc) · 7.32 KB
/
sqlite_gorm_client.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package metadata
import (
"context"
"strings"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/pkg/errors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type DBAccess interface {
SchemaMigration(ctx context.Context) error
CreateTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error
UpdateTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error
DeleteTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error
ReadTenantConfigByTenantUUID(ctx context.Context, uid string) (*TenantConfig, error)
CreateCodeModule(ctx context.Context, codeModule *CodeModule) error
ReadCodeModuleByVersion(ctx context.Context, version string) (*CodeModule, error)
DeleteCodeModule(ctx context.Context, codeModule *CodeModule) error
CreateOSMount(ctx context.Context, osMount *OSMount) error
UpdateOSMount(ctx context.Context, osMount *OSMount) error
DeleteOSMount(ctx context.Context, osMount *OSMount) error
RestoreOSMount(ctx context.Context, osMount *OSMount) error
ReadOSMountByTenantUUID(ctx context.Context, tenantUUID string) (*OSMount, error)
CreateAppMount(ctx context.Context, appMount *AppMount) error
UpdateAppMount(ctx context.Context, appMount *AppMount) error
DeleteAppMount(ctx context.Context, appMount *AppMount) error
ReadAppMountByVolumeMetaID(ctx context.Context, volumeMetaID string) (*AppMount, error)
ReadAppMounts(ctx context.Context) ([]AppMount, error)
}
type DBConn struct {
db *gorm.DB
}
var _ DBAccess = &DBConn{}
// NewDBAccess creates a new gorm db connection to the database.
func NewDBAccess(path string) (*DBConn, error) {
// we need to explicitly enable foreign_keys for sqlite to have sqlite enforce this constraint
if strings.Contains(path, "?") {
path += "&_foreign_keys=on"
} else {
path += "?_foreign_keys=on"
}
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{Logger: logger.Default})
if err != nil {
return &DBConn{}, err
}
return &DBConn{db: db}, nil
}
// SchemaMigration runs gormigrate migrations to create tables
func (conn *DBConn) SchemaMigration(_ context.Context) error {
m := gormigrate.New(conn.db, gormigrate.DefaultOptions, []*gormigrate.Migration{})
m.InitSchema(func(tx *gorm.DB) error {
err := tx.AutoMigrate(
&TenantConfig{},
&CodeModule{},
&OSMount{},
&AppMount{},
&VolumeMeta{},
)
if err != nil {
return err
}
// all other constraints, indexes, etc...
return nil
})
_ = m.Migrate()
return gormigrate.New(conn.db, gormigrate.DefaultOptions, []*gormigrate.Migration{
{
ID: "202403041200",
Migrate: dataMigration,
Rollback: func(tx *gorm.DB) error {
return nil
},
},
}).Migrate()
}
func (conn *DBConn) CreateTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error {
return conn.db.WithContext(ctx).Create(tenantConfig).Error
}
func (conn *DBConn) UpdateTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error {
return conn.db.WithContext(ctx).Updates(tenantConfig).Error
}
func (conn *DBConn) DeleteTenantConfig(ctx context.Context, tenantConfig *TenantConfig) error {
return conn.db.WithContext(ctx).Delete(tenantConfig).Error
}
func (conn *DBConn) CreateCodeModule(ctx context.Context, codeModule *CodeModule) error {
return conn.db.WithContext(ctx).Create(codeModule).Error
}
func (conn *DBConn) ReadCodeModuleByVersion(ctx context.Context, version string) (*CodeModule, error) {
if version == "" {
return nil, errors.New("Key must not be empty string!")
} // TODO: check will be obsolete with move to sql.NullString
codeModule := &CodeModule{Version: version}
err := conn.db.WithContext(ctx).First(codeModule).Error
if err != nil {
return nil, err
}
return codeModule, nil
}
func (conn *DBConn) DeleteCodeModule(ctx context.Context, codeModule *CodeModule) error {
return conn.db.WithContext(ctx).Delete(codeModule).Error
}
func (conn *DBConn) ReadTenantConfigByTenantUUID(ctx context.Context, uid string) (*TenantConfig, error) {
if uid == "" {
return nil, errors.New("error")
} // TODO: check will be obsolete with move to sql.NullString
tenantConfig := &TenantConfig{TenantUUID: uid}
result := conn.db.WithContext(ctx).Where(tenantConfig).First(tenantConfig)
if result.Error != nil {
return nil, result.Error
}
return tenantConfig, nil
}
func (conn *DBConn) CreateOSMount(ctx context.Context, osMount *OSMount) error {
return conn.db.WithContext(ctx).Create(osMount).Error
}
func (conn *DBConn) UpdateOSMount(ctx context.Context, osMount *OSMount) error {
err := conn.RestoreOSMount(ctx, osMount)
if err != nil {
return err
}
return conn.db.WithContext(ctx).Updates(osMount).Error
}
func (conn *DBConn) RestoreOSMount(ctx context.Context, osMount *OSMount) error {
if osMount == nil {
return errors.New("Cannot restore nil OSMount")
}
if !osMount.DeletedAt.Valid {
return nil
}
osMount.DeletedAt.Valid = false
return conn.db.WithContext(ctx).Unscoped().Updates(osMount).Error
}
func (conn *DBConn) ReadOSMountByTenantUUID(ctx context.Context, tenantUUID string) (*OSMount, error) {
if tenantUUID == "" {
return nil, errors.New("Key must not be empty string!")
} // TODO: check will be obsolete with move to sql.NullString
osMount := &OSMount{TenantUUID: tenantUUID}
err := conn.db.WithContext(ctx).Unscoped().Preload("VolumeMeta").First(osMount).Error
if err != nil {
return nil, err
}
return osMount, nil
}
func (conn *DBConn) DeleteOSMount(ctx context.Context, osMount *OSMount) error {
if osMount != nil && osMount.VolumeMetaID != "" {
volumeMeta, err := conn.ReadVolumeMetaByID(ctx, osMount.VolumeMetaID)
if err == nil {
conn.db.WithContext(ctx).Delete(volumeMeta)
}
}
return conn.db.WithContext(ctx).Delete(osMount).Error
}
func (conn *DBConn) ReadVolumeMetaByID(ctx context.Context, id string) (*VolumeMeta, error) {
if id == "" {
return nil, errors.New("Key must not be empty string!")
} // TODO: check will be obsolete with move to sql.NullString
volumeMeta := &VolumeMeta{ID: id}
result := conn.db.WithContext(ctx).First(volumeMeta)
if result.Error != nil {
return nil, result.Error
}
return volumeMeta, nil
}
func (conn *DBConn) CreateAppMount(ctx context.Context, appMount *AppMount) error {
return conn.db.WithContext(ctx).Create(appMount).Error
}
func (conn *DBConn) UpdateAppMount(ctx context.Context, appMount *AppMount) error {
return conn.db.WithContext(ctx).Updates(appMount).Error
}
func (conn *DBConn) ReadAppMountByVolumeMetaID(ctx context.Context, volumeMetaID string) (*AppMount, error) {
if volumeMetaID == "" {
return nil, errors.New("Key must not be empty string!")
} // TODO: check will be obsolete with move to sql.NullString
appMount := &AppMount{VolumeMetaID: volumeMetaID}
result := conn.db.WithContext(ctx).Preload("VolumeMeta").First(&appMount)
if result.Error != nil {
return nil, result.Error
}
return appMount, nil
}
func (conn *DBConn) ReadAppMounts(ctx context.Context) ([]AppMount, error) {
var appMounts []AppMount
result := conn.db.WithContext(ctx).Preload("VolumeMeta").Find(&appMounts)
if result.Error != nil {
return nil, result.Error
}
return appMounts, nil
}
func (conn *DBConn) DeleteAppMount(ctx context.Context, appMount *AppMount) error {
if appMount != nil {
volumeMeta, err := conn.ReadVolumeMetaByID(ctx, appMount.VolumeMetaID)
if err == nil {
conn.db.WithContext(ctx).Delete(volumeMeta)
}
}
return conn.db.WithContext(ctx).Delete(appMount).Error
}