From a9949c2e956eff5232fe6bd5bab4b7db9db67948 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 11 Sep 2022 13:57:00 +0200 Subject: [PATCH] database/sqlite: make optional SQLite requires cgo, for some use cases this is undesirable. --- README.md | 5 +++-- database/database.go | 8 ++++++++ database/sqlite.go | 11 ++++------- database/sqlite_stub.go | 17 +++++++++++++++++ database/sqlite_test.go | 2 ++ server_test.go | 4 ++++ 6 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 database/sqlite_stub.go diff --git a/README.md b/README.md index d7c2fdf..b86edea 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Dependencies: - Go - BSD or GNU make -- a C89 compiler (for SQLite) +- a C89 compiler (optional, for SQLite) - scdoc (optional, for man pages) For end users, a `Makefile` is provided: @@ -28,7 +28,8 @@ For end users, a `Makefile` is provided: For development, you can use `go run ./cmd/soju` as usual. -To link with the system libsqlite3, set `GOFLAGS="-tags=libsqlite3"`. +To link with the system libsqlite3, set `GOFLAGS="-tags=libsqlite3"`. To disable +SQLite support, set `GOFLAGS="-tags=nosqlite"`. ## Contributing diff --git a/database/database.go b/database/database.go index eb5240e..be60fd8 100644 --- a/database/database.go +++ b/database/database.go @@ -2,6 +2,7 @@ package database import ( "context" + "database/sql" "fmt" "net/url" "strings" @@ -235,3 +236,10 @@ type WebPushSubscription struct { VAPID string } } + +func toNullString(s string) sql.NullString { + return sql.NullString{ + String: s, + Valid: s != "", + } +} diff --git a/database/sqlite.go b/database/sqlite.go index fc70f11..feb1050 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -1,3 +1,5 @@ +//go:build !nosqlite + package database import ( @@ -13,6 +15,8 @@ import ( promcollectors "github.com/prometheus/client_golang/prometheus/collectors" ) +const SqliteEnabled = true + const sqliteQueryTimeout = 5 * time.Second const sqliteTimeLayout = "2006-01-02T15:04:05.000Z" @@ -339,13 +343,6 @@ func (db *SqliteDB) Stats(ctx context.Context) (*DatabaseStats, error) { return &stats, nil } -func toNullString(s string) sql.NullString { - return sql.NullString{ - String: s, - Valid: s != "", - } -} - func (db *SqliteDB) ListUsers(ctx context.Context) ([]User, error) { ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout) defer cancel() diff --git a/database/sqlite_stub.go b/database/sqlite_stub.go new file mode 100644 index 0000000..168c88f --- /dev/null +++ b/database/sqlite_stub.go @@ -0,0 +1,17 @@ +//go:build nosqlite + +package database + +import ( + "errors" +) + +const SqliteEnabled = false + +func OpenSqliteDB(source string) (Database, error) { + return nil, errors.New("SQLite support is disabled") +} + +func OpenTempSqliteDB() (Database, error) { + return OpenSqliteDB("") +} diff --git a/database/sqlite_test.go b/database/sqlite_test.go index eb2c7ad..3a6e72b 100644 --- a/database/sqlite_test.go +++ b/database/sqlite_test.go @@ -1,3 +1,5 @@ +//go:build !nosqlite + package database import ( diff --git a/server_test.go b/server_test.go index 3dbb934..7e70bd0 100644 --- a/server_test.go +++ b/server_test.go @@ -20,6 +20,10 @@ const ( ) func createTempSqliteDB(t *testing.T) database.Database { + if !database.SqliteEnabled { + t.Skip("SQLite support is disabled") + } + db, err := database.OpenTempSqliteDB() if err != nil { t.Fatalf("failed to create temporary SQLite database: %v", err)