-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
293 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/aradwann/eenergy/util" | ||
) | ||
|
||
// InitDatabase initializes the database connection and performs migrations. | ||
func InitDatabase(config util.Config) Store { | ||
// Initialize database connection. | ||
dbConn, err := initDatabaseConn(config) | ||
if err != nil { | ||
handleError("Unable to connect to database", err) | ||
} | ||
defer dbConn.Close() | ||
|
||
// Run database migrations. | ||
RunDBMigrations(dbConn, config.MigrationsURL) | ||
|
||
// Create store instance. | ||
store := newStore(dbConn) | ||
return store | ||
} | ||
|
||
// initDatabaseConn initializes the database connection. | ||
func initDatabaseConn(config util.Config) (*sql.DB, error) { | ||
dbConn, err := sql.Open(config.DBDriver, config.DBSource) | ||
if err != nil { | ||
handleError("Unable to connect to database", err) | ||
} | ||
return dbConn, err | ||
} | ||
|
||
// handleError logs an error message and exits the program with status code 1. | ||
func handleError(message string, err error) { | ||
slog.Error(fmt.Sprintf("%s: %v", message, err)) | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.