-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlite.go
86 lines (71 loc) · 1.6 KB
/
sqlite.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
package db
import (
"github.com/cloud-barista/cm-grasshopper/common"
"github.com/cloud-barista/cm-grasshopper/pkg/api/rest/model"
"github.com/glebarez/sqlite"
"github.com/jollaman999/utils/fileutil"
"github.com/jollaman999/utils/logger"
"gorm.io/gorm"
"io"
"os"
)
var SoftwaresDB *gorm.DB
var DB *gorm.DB
func copyFile(src string, dst string) (err error) {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer func() {
_ = sourceFile.Close()
}()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
_ = destFile.Close()
}()
_, err = io.Copy(destFile, sourceFile)
return err
}
func Open() error {
var err error
sourceDB := "softwares.db"
targetPath := common.RootPath + "/softwares.db"
if !fileutil.IsExist(targetPath) {
if fileutil.IsExist(sourceDB) {
err := copyFile(sourceDB, targetPath)
if err != nil {
return err
}
}
}
SoftwaresDB, err = gorm.Open(sqlite.Open(common.RootPath+"/softwares.db"), &gorm.Config{})
if err != nil {
logger.Panicln(logger.ERROR, true, err)
}
err = SoftwaresDB.AutoMigrate(&model.Software{})
if err != nil {
logger.Panicln(logger.ERROR, true, err)
}
DB, err = gorm.Open(sqlite.Open(common.RootPath+"/"+common.ModuleName+".db"), &gorm.Config{})
if err != nil {
logger.Panicln(logger.ERROR, true, err)
}
err = DB.AutoMigrate(&model.SoftwareInstallStatus{})
if err != nil {
logger.Panicln(logger.ERROR, true, err)
}
return err
}
func Close() {
if SoftwaresDB != nil {
sqlDB, _ := SoftwaresDB.DB()
_ = sqlDB.Close()
}
if DB != nil {
sqlDB, _ := DB.DB()
_ = sqlDB.Close()
}
}