forked from soundscapecloud/soundscape
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.go
More file actions
39 lines (35 loc) · 928 Bytes
/
db.go
File metadata and controls
39 lines (35 loc) · 928 Bytes
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
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"os"
// _ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
// _ "github.com/jinzhu/gorm/dialects/mssql"
//"fmt"
)
// User ...
/*type User struct {
ID uint
Username string
Password string
}*/
var db *gorm.DB
func dbInit() {
var err error
// Switch database
if os.Getenv("MYSQL_DB") != "" {
db, err = gorm.Open("mysql", os.Getenv("MYSQL_USER")+":"+os.Getenv("MYSQL_PASSWORD")+"@tcp("+os.Getenv("MYSQL_HOST")+":"+os.Getenv("MYSQL_PORT")+")/"+os.Getenv("MYSQL_DB")+"?charset=utf8&parseTime=True&loc=Local")
} else {
db, err = gorm.Open("sqlite3", "./streamlist.db")
}
if err != nil {
panic("failed to connect database")
}
//defer db.Close()
db.SingularTable(true)
//db.AutoMigrate(&User{})
db.AutoMigrate(&Media{})
db.AutoMigrate(&List{})
db.AutoMigrate(&User{})
}