-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
48 lines (42 loc) · 1.02 KB
/
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
package database
import (
"fmt"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"github.com/bsanzhiev/tsurhai/models"
)
var Instance *gorm.DB
var dbError error
func Connect() *gorm.DB {
Instance, dbError = gorm.Open(postgres.Open(fmt.Sprintf(
"host=%s port=%d user=%s dbname=%s password=%s sslmode=disable TimeZone=%s",
viper.GetString("DB_HOST"),
viper.GetInt("DB_PORT"),
viper.GetString("DB_USER"),
viper.GetString("DB_NAME"),
viper.GetString("DB_PASSWORD"),
viper.GetString("DB_TIMEZONE"),
)), &gorm.Config{
PrepareStmt: true,
SkipDefaultTransaction: true,
DisableForeignKeyConstraintWhenMigrating: true,
})
if dbError != nil {
panic(dbError.Error())
}
log.Println("Connected to database!")
return Instance
}
func Migrate() error {
err := Instance.AutoMigrate(
&models.User{},
)
if err != nil {
log.Println("Database Migration Failed:", err)
return err
}
log.Println("Database Migration Completed!")
return nil
}