forked from dailydotdev/daily-monetization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
104 lines (88 loc) · 2.81 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
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/golang-migrate/migrate/v4/source/github"
log "github.com/sirupsen/logrus"
"os"
)
var dbConnString = os.Getenv("DB_CONNECTION_STRING")
const migrationVer uint = 9
var db *sql.DB
var hystrixDb = "db"
var campStmt *sql.Stmt
var addCampStmt *sql.Stmt
var getUserTagsStmt *sql.Stmt
func openDatabaseConnection() (*sql.DB, error) {
return sql.Open("mysql", dbConnString+"?charset=utf8mb4,utf8")
}
func newMigrate() (*migrate.Migrate, error) {
con, err := openDatabaseConnection()
if err != nil {
log.Fatal("failed to open sql ", err)
}
driver, err := mysql.WithInstance(con, &mysql.Config{})
if err != nil {
log.Fatal("failed to get driver ", err)
}
return migrate.NewWithDatabaseInstance(
getEnv("MIGRATIONS_SOURCE", "file://migrations"),
"mysql", driver)
}
func migrateDatabase() {
log.Info("migrating database")
m, err := newMigrate()
if err != nil {
log.Fatal("failed to connect ", err)
}
defer m.Close()
err = m.Migrate(migrationVer)
if err != nil && err.Error() != "no change" {
log.Fatal("failed to migrate ", err)
}
}
func dropDatabase() {
log.Info("dropping database")
m, err := newMigrate()
if err != nil {
log.Fatal("failed to connect ", err)
}
defer m.Close()
err = m.Drop()
if err != nil && err.Error() != "no change" {
log.Fatal("failed to drop ", err)
}
}
func initializeDatabase() {
var err error
db, err = openDatabaseConnection()
if err != nil {
log.Fatal("failed to open sql ", err)
}
campStmt, err = db.Prepare("select id, title, url, image, ratio, placeholder, source, company, probability, fallback, geo from ads left join (select ad_id, max(relevant) as relevant from (select ad_id, exists (select user_id from user_tags where user_tags.tag = ad_tags.tag and user_tags.user_id = ?) as relevant from ad_tags) as res group by ad_id) relevant_ads on ads.id = relevant_ads.ad_id where `start` <= ? and end > ? and (relevant_ads.relevant = 1 or relevant_ads.ad_id is null)")
if err != nil {
log.Fatal("failed to prepare query ", err)
}
addCampStmt, err = db.Prepare(
"insert into `ads` " +
"(`id`, `title`, `url`, `image`, `ratio`, `placeholder`, `source`, " +
"`company`, `probability`, `fallback`, `geo`, `start`, `end`) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal("failed to prepare query ", err)
}
getUserTagsStmt, err = db.Prepare("select tag from user_tags where user_id = ?")
if err != nil {
log.Fatal("failed to prepare query ", err)
}
}
func tearDatabase() {
addCampStmt.Close()
campStmt.Close()
getUserTagsStmt.Close()
db.Close()
}