-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
72 lines (57 loc) · 2.02 KB
/
database.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
package database
import (
"os"
"time"
"github.com/Liphium/station/backend/entities/account"
"github.com/Liphium/station/backend/entities/account/properties"
"github.com/Liphium/station/backend/entities/app"
"github.com/Liphium/station/backend/entities/node"
"github.com/Liphium/station/backend/util"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var DBConn *gorm.DB
func Connect() {
url := "host=" + os.Getenv("DB_HOST") + " user=" + os.Getenv("DB_USER") + " password=" + os.Getenv("DB_PASSWORD") + " dbname=" + os.Getenv("DB_DATABASE") + " port=" + os.Getenv("DB_PORT")
db, err := gorm.Open(postgres.Open(url), &gorm.Config{
Logger: logger.Default.LogMode(logger.Warn),
})
if err != nil {
util.Log.Fatal("Something went wrong during the connection with the database.", err)
}
util.Log.Println("Successfully connected to the database.")
// Configure the database driver
driver, _ := db.DB()
driver.SetMaxIdleConns(10)
driver.SetMaxOpenConns(100)
driver.SetConnMaxLifetime(time.Hour)
// Migrate the schema
// Migrate account related tables
db.AutoMigrate(&account.Account{})
db.AutoMigrate(&account.Authentication{})
db.AutoMigrate(&account.Session{})
db.AutoMigrate(&account.Rank{})
db.AutoMigrate(&account.PublicKey{})
db.AutoMigrate(&account.ProfileKey{})
db.AutoMigrate(&account.SignatureKey{})
db.AutoMigrate(&account.StoredActionKey{})
db.AutoMigrate(&account.CloudFile{})
db.AutoMigrate(&account.Invite{})
db.AutoMigrate(&account.InviteCount{})
// Migrate account properties related tables
db.AutoMigrate(&properties.Friendship{})
db.AutoMigrate(&properties.Profile{})
db.AutoMigrate(&properties.StoredAction{})
db.AutoMigrate(&properties.AStoredAction{})
db.AutoMigrate(&properties.VaultEntry{})
// Migrate node related tables
db.AutoMigrate(&node.Cluster{})
db.AutoMigrate(&node.Node{})
db.AutoMigrate(&node.NodeCreation{})
// Migrate app related tables
db.AutoMigrate(&app.App{})
db.AutoMigrate(&app.AppSetting{})
// Assign the database to the global variable
DBConn = db
}