summaryrefslogtreecommitdiff
path: root/database.go
diff options
context:
space:
mode:
Diffstat (limited to 'database.go')
-rw-r--r--database.go79
1 files changed, 27 insertions, 52 deletions
diff --git a/database.go b/database.go
index d19572a..1efd22e 100644
--- a/database.go
+++ b/database.go
@@ -20,17 +20,26 @@ import (
const databaseSchemaVersion int = 2 // this defines the needed version for the
// executable
-type Database struct {
- target string
- Backend *sql.DB
-}
+func setupDatabase() *sql.DB {
+
+ u,err := user.Current()
+ if err != nil {
+ log.Fatal(err)
+ }
+ target := fmt.Sprintf("%s@unix(%s)/%s", u.Username, config.Database.Socket,
+ config.Database.Database)
+
+ db,err := sql.Open("mysql", target)
+ if err != nil {
+ log.Fatal(err)
+ }
-func InitDatabase() Database {
+ err = db.Ping()
+ if err != nil {
+ log.Fatal(err)
+ }
- db := NewDatabase()
- db.Connect()
- db.Ping()
- db.Migrate()
+ migrate(db)
// allow graceful shutdown
var listener = make(chan os.Signal)
@@ -39,53 +48,19 @@ func InitDatabase() Database {
go func() {
signal := <-listener
log.Printf("\nGot signal '%+v'. Shutting down ...\n", signal)
- db.Cleanup()
+ dbCleanup(db)
os.Exit(0) // TODO this does not belong to a database - write utils file 'shutdown.go'
}()
return db
}
-func NewDatabase() Database {
-
- db := Database{}
-
- var username string
- user_ptr,err := user.Current()
- if err != nil {
- log.Fatal(err)
- }
- username = user_ptr.Username
- db.target = fmt.Sprintf("%s@unix(%s)/%s", username, config.Database.Socket,
- config.Database.Database)
-
- return db
-}
-
-func (db *Database) Connect() {
- var err error
- db.Backend,err = sql.Open("mysql", db.target)
- if err != nil {
- log.Fatal(err)
- }
- log.Printf("Connected to database '%s'\n", db.target)
-}
-
-func (db *Database) Ping() {
- err := db.Backend.Ping()
- if err != nil {
- log.Fatal(err)
- } else {
- log.Println("Database is responding")
- }
-}
-
-func (db *Database) Migrate() {
+func migrate(db *sql.DB) {
const t = databaseSchemaVersion // targeted database schema version
for {
- v := db.SchemaVersion() // read schema version from DB table
+ v := schemaVersion(db) // read schema version from DB table
// handle current database schema which is newer than targeted one
if v > t {
@@ -102,12 +77,12 @@ func (db *Database) Migrate() {
log.Printf("Starting database schema migration to version %d.\n", v+1)
path := filepath.Join(config.Database.Migrations,
fmt.Sprintf("%04d_migration.sql", v+1))
- RunSql(path)
+ RunSqlScript(path)
log.Printf("Finished database schema migration to version %d.\n", v+1)
}
}
-func RunSql(path string) {
+func RunSqlScript(path string) {
script, err := os.Open(path)
if err != nil {
@@ -133,11 +108,11 @@ func RunSql(path string) {
}
}
-func (db *Database) SchemaVersion() int {
+func schemaVersion(db *sql.DB) int {
// ask database for schema version
cmd := "SELECT value FROM meta WHERE (identifier='version');"
- rows, err := db.Backend.Query(cmd)
+ rows, err := db.Query(cmd)
// handle missing meta table
if err != nil {
@@ -165,8 +140,8 @@ func (db *Database) SchemaVersion() int {
return v
}
-func (db *Database) Cleanup() {
- err := db.Backend.Close()
+func dbCleanup(db *sql.DB) {
+ err := db.Close()
if err != nil {
log.Println("Could not close database connection")
} else {