diff --git a/main.go b/main.go index d64c912..6b5037a 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,19 @@ import ( "clamp-core/listeners" "clamp-core/migrations" "clamp-core/models" + "clamp-core/repository" + "log" "os" ) func main() { + log.Println("Pinging DB...") + err := repository.GetDB().Ping() + if err != nil { + log.Fatalf("DB ping failed: %s", err) + } + var cliArgs models.CLIArguments = os.Args[1:] os.Setenv("PORT", config.ENV.PORT) migrations.Migrate() diff --git a/repository/db.go b/repository/db.go index 5328cd0..8958ebf 100644 --- a/repository/db.go +++ b/repository/db.go @@ -21,6 +21,7 @@ type DBInterface interface { GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]models.Workflow, int, error) FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]models.ServiceRequest, error) DeleteWorkflowByName(string) error + Ping() error } var db DBInterface diff --git a/repository/postgres.go b/repository/postgres.go index 1307b0e..3b3208f 100644 --- a/repository/postgres.go +++ b/repository/postgres.go @@ -200,6 +200,11 @@ func (p *postgres) GetWorkflows(pageNumber int, pageSize int, sortFields models. return workflows, totalWorkflowsCount, err } +func (p *postgres) Ping() error { + _, err := p.getDb().Exec("SELECT 1") + return err +} + func (p *postgres) getDb() *pg.DB { singletonOnce.Do(func() { log.Println("Connecting to DB") diff --git a/services/abstract_test.go b/services/abstract_test.go index 15b4b2a..26b4480 100644 --- a/services/abstract_test.go +++ b/services/abstract_test.go @@ -75,6 +75,10 @@ func (m mockDB) FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uu return findAllStepStatusByServiceRequestIDAndStepIDMock(serviceRequestID, stepID) } +func (m mockDB) Ping() error { + return nil +} + func init() { repository.SetDb(&mockDB{}) }