Skip to content

Commit

Permalink
Merge pull request #86 from sivachandran/startup-db-check
Browse files Browse the repository at this point in the history
Check DB at startup
  • Loading branch information
priyaaank authored Jan 29, 2021
2 parents 0e04c10 + b2677a4 commit 91cb312
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions repository/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions repository/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions services/abstract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}

0 comments on commit 91cb312

Please sign in to comment.