-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
127 lines (110 loc) · 3.11 KB
/
db.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
package main
import (
"crypto/tls"
"errors"
"github.com/go-gormigrate/gormigrate/v2"
mysql2 "github.com/go-sql-driver/mysql"
"log"
"os"
"path/filepath"
"regexp"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var DB *gorm.DB
func init() {
var err error
if DB, err = OpenTestConnection(); err != nil {
log.Printf("failed to connect database, got error %v\n", err)
os.Exit(1)
} else {
sqlDB, err := DB.DB()
if err == nil {
err = sqlDB.Ping()
}
if err != nil {
log.Printf("failed to connect database, got error %v\n", err)
}
RunMigrations()
if DB.Dialector.Name() == "sqlite" {
DB.Exec("PRAGMA foreign_keys = ON")
}
DB.Logger = DB.Logger.LogMode(logger.Info)
}
}
func OpenTestConnection() (db *gorm.DB, err error) {
dbDSN := os.Getenv("GORM_DSN")
switch os.Getenv("GORM_DIALECT") {
case "tidb":
log.Println("testing tidb...")
if dbDSN == "" {
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
}
re := regexp.MustCompile(`(?m)tcp\((\S+):\d+\)`)
match := re.FindStringSubmatch(dbDSN)
if len(match) <= 1 {
return nil, errors.New("invaild dsn")
}
mysql2.RegisterTLSConfig("tidb", &tls.Config{
MinVersion: tls.VersionTLS12,
ServerName: match[1],
})
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
case "mysql":
log.Println("testing mysql...")
if dbDSN == "" {
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
}
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
case "postgres":
log.Println("testing postgres...")
if dbDSN == "" {
dbDSN = "user=gorm password=gorm host=localhost dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
}
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
case "sqlserver":
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
// CREATE DATABASE gorm;
// USE gorm;
// CREATE USER gorm FROM LOGIN gorm;
// sp_changedbowner 'gorm';
log.Println("testing sqlserver...")
if dbDSN == "" {
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
}
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
default:
log.Println("testing sqlite3...")
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
}
if debug := os.Getenv("DEBUG"); debug == "true" {
db.Logger = db.Logger.LogMode(logger.Info)
} else if debug == "false" {
db.Logger = db.Logger.LogMode(logger.Silent)
}
return
}
func RunMigrations() {
var err error
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
m := gormigrate.New(DB, gormigrate.DefaultOptions, []*gormigrate.Migration{{
// create tables
ID: "202307101821",
Migrate: func(tx *gorm.DB) error {
if err = DB.AutoMigrate(allModels...); err != nil {
log.Printf("Failed to auto migrate, but got error %v\n", err)
return err
}
return nil
},
}})
if err = m.Migrate(); err != nil {
log.Fatalf("Migration failed: %v", err)
os.Exit(1)
}
log.Println("Migration did run successfully")
}