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

Added support for sqlite3 check #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions checks/sqlite3/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package healthchecks

import (
"context"
"database/sql"
"fmt"

_ "github.com/mattn/go-sqlite3"
)

// Config is the SQLite3 checker configuration settings container.
type Config struct {
// DSN is the SQLite3 instance connection DSN. Required.
DSN string
}

// New creates new SQLite3 health check that verifies the following:
// - connection establishing
// - doing the ping command
// - selecting postgres version
func New(config Config) func(ctx context.Context) error {
return func(ctx context.Context) (checkErr error) {
db, err := sql.Open("sqlite3", config.DSN)
if err != nil {
checkErr = fmt.Errorf("sqlite health check failed on connect: %w", err)
return
}

defer func() {
// override checkErr only if there were no other errors
if err := db.Close(); err != nil && checkErr == nil {
checkErr = fmt.Errorf("sqlite health check failed on connection closing: %w", err)
}
}()

err = db.PingContext(ctx)
if err != nil {
checkErr = fmt.Errorf("sqlite health check failed on ping: %w", err)
return
}

rows, err := db.QueryContext(ctx, `select sqlite_version()`)
if err != nil {
checkErr = fmt.Errorf("sqlite health check failed on select: %w", err)
return
}
defer func() {
// override checkErr only if there were no other errors
if err = rows.Close(); err != nil && checkErr == nil {
checkErr = fmt.Errorf("sqlite health check failed on rows closing: %w", err)
}
}()

return
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/influxdata/influxdb-client-go/v2 v2.9.0
github.com/jackc/pgx/v4 v4.16.1
github.com/lib/pq v1.10.6
github.com/mattn/go-sqlite3 v1.14.16
github.com/rabbitmq/amqp091-go v1.3.4
github.com/stretchr/testify v1.8.0
github.com/vitorsalgado/mocha/v2 v2.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down