@@ -4,18 +4,32 @@ import (
44 "fmt"
55
66 "github.com/google/go-github/v50/github"
7- config "github.com/moosh3 /github-actions-aggregator/pkg/config"
8- "github.com/moosh3 /github-actions-aggregator/pkg/db/models"
7+ "github.com/mooshe3 /github-actions-aggregator/pkg/config"
8+ "github.com/mooshe3 /github-actions-aggregator/pkg/db/models"
99 "gorm.io/driver/postgres"
1010 "gorm.io/gorm"
1111 "gorm.io/gorm/clause"
1212)
1313
14+ // Database represents a wrapper around the GORM database connection.
1415type Database struct {
16+ // Conn is the underlying GORM database connection.
1517 Conn * gorm.DB
1618}
1719
18- func InitDB (cfg * config.Config ) (* gorm.DB , error ) {
20+ // InitDB initializes and returns a new Database instance.
21+ //
22+ // It takes a configuration object and uses it to establish a connection
23+ // to the PostgreSQL database. It also performs auto-migration of the
24+ // database schema for the Repository, WorkflowRun, and Statistics models.
25+ //
26+ // Parameters:
27+ // - cfg: A pointer to a config.Config struct containing database connection details.
28+ //
29+ // Returns:
30+ // - A pointer to a Database struct containing the initialized GORM DB connection.
31+ // - An error if the database connection or auto-migration fails.
32+ func InitDB (cfg * config.Config ) (* Database , error ) {
1933 // Database initialization logic
2034 dsn := fmt .Sprintf ("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=UTC" ,
2135 cfg .Database .Host , cfg .Database .User , cfg .Database .Password , cfg .Database .DBName , cfg .Database .Port )
@@ -31,7 +45,7 @@ func InitDB(cfg *config.Config) (*gorm.DB, error) {
3145 return nil , fmt .Errorf ("failed to auto-migrate database schema: %w" , err )
3246 }
3347
34- return db , nil
48+ return & Database { Conn : db } , nil
3549}
3650
3751func (db * Database ) GetMonitoredRepositories () ([]models.Repository , error ) {
@@ -42,13 +56,11 @@ func (db *Database) GetMonitoredRepositories() ([]models.Repository, error) {
4256
4357func (db * Database ) SaveWorkflowRun (run * github.WorkflowRun ) error {
4458 workflowRun := models.WorkflowRun {
45- ID : run .GetID (),
4659 WorkflowID : run .GetWorkflowID (),
4760 RepositoryID : run .GetRepository ().GetID (),
4861 Status : run .GetStatus (),
4962 Conclusion : run .GetConclusion (),
5063 RunNumber : run .GetRunNumber (),
51- Event : run .GetEvent (),
5264 CreatedAt : run .GetCreatedAt ().Time ,
5365 UpdatedAt : run .GetUpdatedAt ().Time ,
5466 // Add other fields as needed
@@ -65,3 +77,7 @@ func (db *Database) SaveStatistics(stats *models.Statistics) error {
6577 // Upsert operation
6678 return db .Conn .Save (stats ).Error
6779}
80+
81+ func (db * Database ) SaveUser (user * models.GitHubUser ) error {
82+ return db .Conn .Save (user ).Error
83+ }
0 commit comments