database/sqlite: make optional

SQLite requires cgo, for some use cases this is undesirable.
This commit is contained in:
Simon Ser 2022-09-11 13:57:00 +02:00
parent d27880e03e
commit a9949c2e95
6 changed files with 38 additions and 9 deletions

View File

@ -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

View File

@ -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 != "",
}
}

View File

@ -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()

17
database/sqlite_stub.go Normal file
View File

@ -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("")
}

View File

@ -1,3 +1,5 @@
//go:build !nosqlite
package database
import (

View File

@ -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)