Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check DB at startup #86

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -196,6 +196,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{})
}