From 16dd360c5fb6404e6cbb8aa9cb8a4eb8d1ce9066 Mon Sep 17 00:00:00 2001 From: Andrii Kondratiuk Date: Thu, 5 Jan 2023 16:54:01 +0200 Subject: [PATCH] Added support for sqlite3 check --- checks/sqlite3/check.go | 56 +++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 59 insertions(+) create mode 100644 checks/sqlite3/check.go diff --git a/checks/sqlite3/check.go b/checks/sqlite3/check.go new file mode 100644 index 0000000..103a2a8 --- /dev/null +++ b/checks/sqlite3/check.go @@ -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 + } +} diff --git a/go.mod b/go.mod index d28bd49..c2fd55c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d8f96c0..d2d7707 100644 --- a/go.sum +++ b/go.sum @@ -188,6 +188,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=