From 8e166f100697c5a497aa68349c20a6cb88b5503f Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 11 Feb 2023 12:50:08 +0100 Subject: Migrate database to global config struct --- database.go | 18 +++++++++--------- main.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/database.go b/database.go index 20ec5a6..d19572a 100644 --- a/database.go +++ b/database.go @@ -21,17 +21,16 @@ const databaseSchemaVersion int = 2 // this defines the needed version for the // executable type Database struct { - config DatabaseConfig target string Backend *sql.DB } -func InitDatabase(config DatabaseConfig) Database { +func InitDatabase() Database { - db := NewDatabase(config) + db := NewDatabase() db.Connect() db.Ping() - db.Migrate(config.Migrations) + db.Migrate() // allow graceful shutdown var listener = make(chan os.Signal) @@ -47,18 +46,18 @@ func InitDatabase(config DatabaseConfig) Database { return db } -func NewDatabase(config DatabaseConfig) Database { +func NewDatabase() Database { db := Database{} - db.config = config 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.Socket, config.Database) + db.target = fmt.Sprintf("%s@unix(%s)/%s", username, config.Database.Socket, + config.Database.Database) return db } @@ -81,7 +80,7 @@ func (db *Database) Ping() { } } -func (db *Database) Migrate(dir string) { +func (db *Database) Migrate() { const t = databaseSchemaVersion // targeted database schema version @@ -101,7 +100,8 @@ func (db *Database) Migrate(dir string) { // execute migration log.Printf("Starting database schema migration to version %d.\n", v+1) - path := filepath.Join(dir, fmt.Sprintf("%04d_migration.sql", v+1)) + path := filepath.Join(config.Database.Migrations, + fmt.Sprintf("%04d_migration.sql", v+1)) RunSql(path) log.Printf("Finished database schema migration to version %d.\n", v+1) } diff --git a/main.go b/main.go index 28e6554..cd32eb0 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,6 @@ var config RuntimeConfig func main() { config = GetRuntimeConfig() log.Printf("Starting ceres with config file '%s'\n", config.Path) - db := InitDatabase(config.Database) + db := InitDatabase() runServer(config.Http, &db) } -- cgit v1.2.3-70-g09d2