diff options
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/database.go b/database.go index 6451599..ffc44e3 100644 --- a/database.go +++ b/database.go @@ -14,8 +14,7 @@ import ( _ "github.com/go-sql-driver/mysql" ) -const databaseSchemaVersion int = 2 // this defines the needed version for the -// executable +const neededSchemaVersion int = 2 func setupDatabase() *sql.DB { @@ -45,23 +44,19 @@ func setupDatabase() *sql.DB { func migrate(db *sql.DB) { - const t = databaseSchemaVersion // targeted database schema version - for { - v := schemaVersion(db) // read schema version from DB table + v := schemaVersion(db) - // handle current database schema which is newer than targeted one - if v > t { + if v > neededSchemaVersion { 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, + neededSchemaVersion) } - // break if targeted version is already reached - if v == t { + if v == neededSchemaVersion { break } - // 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)) @@ -98,27 +93,20 @@ func RunSqlScript(path string) { func schemaVersion(db *sql.DB) int { - // ask database for schema version cmd := "SELECT value FROM meta WHERE (identifier='version');" rows, err := db.Query(cmd) - - // handle missing meta table if err != nil { log.Fatal(err) } - - // handle successful schema version query defer rows.Close() + rows.Next() var version string err = rows.Scan(&version) - - // handle missing version field in meta table if err != nil { log.Fatal(err) } - // convert to integer and handle error v, err := strconv.Atoi(version) if err != nil { log.Fatalf("Could not convert database schema version '%s' to int.\n", |