Skip to content

Commit 79ec6ef

Browse files
authored
feat(database): add optional build support for SQLite
Miniflux can be build with `go build -tags=sqlite` to test this. Note that while it builds, it will fail at runtime, as some of the SQL used in miniflux is postgresql-specific.
1 parent 8d4954e commit 79ec6ef

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-webauthn/webauthn v0.11.2
1010
github.com/gorilla/mux v1.8.1
1111
github.com/lib/pq v1.10.9
12+
github.com/mattn/go-sqlite3 v1.14.24
1213
github.com/prometheus/client_golang v1.20.5
1314
github.com/tdewolff/minify/v2 v2.21.2
1415
golang.org/x/crypto v0.31.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
3636
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
3737
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
3838
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
39+
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
40+
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
3941
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4042
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
4143
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=

internal/database/database.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,18 @@ import (
77
"database/sql"
88
"fmt"
99
"log/slog"
10-
"time"
11-
12-
// Postgresql driver import
13-
pq "github.com/lib/pq"
1410
)
1511

16-
// NewConnectionPool configures the database connection pool.
17-
func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
18-
db, err := sql.Open("postgres", dsn)
19-
if err != nil {
20-
return nil, err
21-
}
22-
23-
db.SetMaxOpenConns(maxConnections)
24-
db.SetMaxIdleConns(minConnections)
25-
db.SetConnMaxLifetime(connectionLifetime)
26-
27-
return db, nil
28-
}
29-
3012
// Migrate executes database migrations.
3113
func Migrate(db *sql.DB) error {
3214
var currentVersion int
3315
db.QueryRow(`SELECT version FROM schema_version`).Scan(&currentVersion)
3416

35-
driver := ""
36-
switch db.Driver().(type) {
37-
case *pq.Driver:
38-
driver = "postgresql"
39-
default:
40-
panic(fmt.Sprintf("the driver %s isn't supported", db.Driver()))
41-
}
42-
17+
driver := getDriverStr()
4318
slog.Info("Running database migrations",
4419
slog.Int("current_version", currentVersion),
4520
slog.Int("latest_version", schemaVersion),
21+
slog.String("driver", driver),
4622
)
4723

4824
for version := currentVersion; version < schemaVersion; version++ {

internal/database/postgresql.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build !sqlite
2+
3+
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package database // import "miniflux.app/v2/internal/database"
7+
8+
import (
9+
"database/sql"
10+
"time"
11+
12+
_ "github.com/lib/pq"
13+
)
14+
15+
// NewConnectionPool configures the database connection pool.
16+
func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
17+
db, err := sql.Open("postgres", dsn)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
db.SetMaxOpenConns(maxConnections)
23+
db.SetMaxIdleConns(minConnections)
24+
db.SetConnMaxLifetime(connectionLifetime)
25+
26+
return db, nil
27+
}
28+
29+
func getDriverStr() string {
30+
return "postgresql"
31+
}

internal/database/sqlite.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build sqlite
2+
3+
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package database // import "miniflux.app/v2/internal/database"
7+
8+
import (
9+
"database/sql"
10+
"time"
11+
12+
_ "github.com/mattn/go-sqlite3"
13+
)
14+
15+
// NewConnectionPool configures the database connection pool.
16+
func NewConnectionPool(dsn string, _, _ int, _ time.Duration) (*sql.DB, error) {
17+
db, err := sql.Open("sqlite3", dsn)
18+
if err != nil {
19+
return nil, err
20+
}
21+
return db, nil
22+
}
23+
24+
func getDriverStr() string {
25+
return "sqlite3"
26+
}

0 commit comments

Comments
 (0)