diff options
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/database.go b/database.go index 5d9181c..6451599 100644 --- a/database.go +++ b/database.go @@ -1,33 +1,32 @@ - package main import ( - "log" + "database/sql" "fmt" - "path/filepath" "io" + "log" "os" - "os/user" "os/exec" + "os/user" + "path/filepath" "strconv" - "database/sql" _ "github.com/go-sql-driver/mysql" ) -const databaseSchemaVersion int = 2 // this defines the needed version for the - // executable +const databaseSchemaVersion int = 2 // this defines the needed version for the +// executable func setupDatabase() *sql.DB { - u,err := user.Current() + 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) + config.Database.Database) - db,err := sql.Open("mysql", target) + db, err := sql.Open("mysql", target) if err != nil { log.Fatal(err) } @@ -46,15 +45,15 @@ func setupDatabase() *sql.DB { func migrate(db *sql.DB) { - const t = databaseSchemaVersion // targeted database schema version + const t = databaseSchemaVersion // targeted database schema version for { - v := schemaVersion(db) // 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 { log.Fatalf( - "Current database schema version is %d but newest is %d!", v, t) + "Current database schema version is %d but newest is %d!", v, t) } // break if targeted version is already reached @@ -65,7 +64,7 @@ func migrate(db *sql.DB) { // execute migration 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)) + fmt.Sprintf("%04d_migration.sql", v+1)) RunSqlScript(path) log.Printf("Finished database schema migration to version %d.\n", v+1) } @@ -123,7 +122,7 @@ func schemaVersion(db *sql.DB) int { v, err := strconv.Atoi(version) if err != nil { log.Fatalf("Could not convert database schema version '%s' to int.\n", - version) + version) } return v |