forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
116 lines (98 loc) · 3.45 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
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
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Functions for manipulating the per-event SQLite datastore.
package main
import (
"bitbucket.org/liamstask/goose/lib/goose"
"database/sql"
"fmt"
"github.com/jmoiron/modl"
_ "github.com/mattn/go-sqlite3"
"io"
"os"
"strings"
"time"
)
const backupsDir = "db/backups"
const migrationsDir = "db/migrations"
type Database struct {
path string
db *sql.DB
eventSettingsMap *modl.DbMap
matchMap *modl.DbMap
matchResultMap *modl.DbMap
rankingMap *modl.DbMap
teamMap *modl.DbMap
allianceTeamMap *modl.DbMap
lowerThirdMap *modl.DbMap
sponsorSlideMap *modl.DbMap
}
// Opens the SQLite database at the given path, creating it if it doesn't exist, and runs any pending
// migrations.
func OpenDatabase(path string) (*Database, error) {
// Find and run the migrations using goose. This also auto-creates the DB.
dbDriver := goose.DBDriver{"sqlite3", path, "github.com/mattn/go-sqlite3", &goose.Sqlite3Dialect{}}
dbConf := goose.DBConf{MigrationsDir: migrationsDir, Env: "prod", Driver: dbDriver}
target, err := goose.GetMostRecentDBVersion(migrationsDir)
if err != nil {
return nil, err
}
err = goose.RunMigrations(&dbConf, migrationsDir, target)
if err != nil {
return nil, err
}
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
database := Database{path: path, db: db}
database.mapTables()
return &database, nil
}
func (database *Database) Close() {
database.db.Close()
}
// Creates a copy of the current database and saves it to the backups directory.
func (database *Database) Backup(reason string) error {
err := os.MkdirAll(backupsDir, 0755)
if err != nil {
return err
}
filename := fmt.Sprintf("%s/%s_%s_%s.db", backupsDir, strings.Replace(eventSettings.Name, " ", "_", -1),
time.Now().Format("20060102150405"), reason)
src, err := os.Open(database.path)
if err != nil {
return err
}
defer src.Close()
dest, err := os.Create(filename)
if err != nil {
return err
}
defer dest.Close()
if _, err := io.Copy(dest, src); err != nil {
return err
}
return nil
}
// Sets up table-object associations.
func (database *Database) mapTables() {
dialect := new(modl.SqliteDialect)
database.eventSettingsMap = modl.NewDbMap(database.db, dialect)
database.eventSettingsMap.AddTableWithName(EventSettings{}, "event_settings").SetKeys(false, "Id")
database.matchMap = modl.NewDbMap(database.db, dialect)
database.matchMap.AddTableWithName(Match{}, "matches").SetKeys(true, "Id")
database.matchResultMap = modl.NewDbMap(database.db, dialect)
database.matchResultMap.AddTableWithName(MatchResultDb{}, "match_results").SetKeys(true, "Id")
database.rankingMap = modl.NewDbMap(database.db, dialect)
database.rankingMap.AddTableWithName(Ranking{}, "rankings").SetKeys(false, "TeamId")
database.teamMap = modl.NewDbMap(database.db, dialect)
database.teamMap.AddTableWithName(Team{}, "teams").SetKeys(false, "Id")
database.allianceTeamMap = modl.NewDbMap(database.db, dialect)
database.allianceTeamMap.AddTableWithName(AllianceTeam{}, "alliance_teams").SetKeys(true, "Id")
database.lowerThirdMap = modl.NewDbMap(database.db, dialect)
database.lowerThirdMap.AddTableWithName(LowerThird{}, "lower_thirds").SetKeys(true, "Id")
database.sponsorSlideMap = modl.NewDbMap(database.db, dialect)
database.sponsorSlideMap.AddTableWithName(SponsorSlide{}, "sponsor_slides").SetKeys(true, "Id")
}