forked from uberswe/golang-base-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
66 lines (61 loc) · 2.34 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
package baseproject
import (
"fmt"
"github.com/uberswe/golang-base-project/config"
"github.com/uberswe/golang-base-project/models"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func connectToDatabase(c config.Config) (db *gorm.DB, err error) {
if c.Database == "sqlite" {
// In-memory sqlite if no database name is specified
dsn := "file::memory:?cache=shared"
if c.DatabaseName != "" {
dsn = fmt.Sprintf("%s.db", c.DatabaseName)
}
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{})
} else if c.Database == "mysql" {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", c.DatabaseUsername, c.DatabasePassword, c.DatabaseHost, c.DatabasePort, c.DatabaseName)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
} else if c.Database == "postgres" {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=UTC", c.DatabaseHost, c.DatabaseUsername, c.DatabasePassword, c.DatabaseName, c.DatabasePort)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
} else {
return db, fmt.Errorf("no database specified: %s", c.Database)
}
return db, err
}
func migrateDatabase(db *gorm.DB) error {
err := db.AutoMigrate(&models.User{}, &models.Token{}, &models.Session{}, &models.Website{})
seed(db)
return err
}
func seed(db *gorm.DB) {
// We seed some websites for our search results
websites := []models.Website{
{
Title: "A Tour of Go",
Description: "A Tour of Go has several interactive examples of how Go which you can learn from. There is a menu available if you would like to skip to different sections.",
URL: "https://go.dev/tour/welcome/1",
},
{
Title: "Go by Example",
Description: "As described on the website: Go by Example is a hands-on introduction to Go using annotated example programs. I have used this site many times as a reference when I need to look something up.",
URL: "https://gobyexample.com/",
},
{
Title: "Go.dev",
Description: "Learn how to install Go on your machine and read the documentation on the Go website.",
URL: "https://go.dev/learn/",
},
}
for _, w := range websites {
res := db.Where(&w).First(&w)
// If no record exists we insert
if res.Error != nil && res.Error == gorm.ErrRecordNotFound {
db.Save(&w)
}
}
}