William Petit
e756a60373
All checks were successful
arcad/emissary/pipeline/head This commit looks good
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/command/common"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
"github.com/pkg/errors"
|
|
"github.com/urfave/cli/v2"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
func PingCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "ping",
|
|
Usage: "Test database connectivity",
|
|
Action: func(ctx *cli.Context) error {
|
|
conf, err := common.LoadConfig(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "Could not load configuration")
|
|
}
|
|
|
|
logger.Info(ctx.Context, "connecting to database", logger.F("dsn", conf.Server.Database.DSN))
|
|
|
|
driver := string(conf.Server.Database.Driver)
|
|
dsn := string(conf.Server.Database.DSN)
|
|
|
|
db, err := sql.Open(driver, dsn)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
defer func() {
|
|
if err := db.Close(); err != nil {
|
|
err = errors.WithStack(err)
|
|
logger.Error(ctx.Context, "error while closing database connection", logger.E(err))
|
|
sentry.CaptureException(err)
|
|
}
|
|
}()
|
|
|
|
if err := db.PingContext(ctx.Context); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
logger.Info(ctx.Context, "connection succeeded", logger.F("dsn", conf.Server.Database.DSN))
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|