Forráskód Böngészése

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.
Julien Voisin 1 éve
szülő
commit
79ec6ef81f
5 módosított fájl, 62 hozzáadás és 26 törlés
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 2 26
      internal/database/database.go
  4. 31 0
      internal/database/postgresql.go
  5. 26 0
      internal/database/sqlite.go

+ 1 - 0
go.mod

@@ -9,6 +9,7 @@ require (
 	github.com/go-webauthn/webauthn v0.11.2
 	github.com/gorilla/mux v1.8.1
 	github.com/lib/pq v1.10.9
+	github.com/mattn/go-sqlite3 v1.14.24
 	github.com/prometheus/client_golang v1.20.5
 	github.com/tdewolff/minify/v2 v2.21.2
 	golang.org/x/crypto v0.31.0

+ 2 - 0
go.sum

@@ -36,6 +36,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
 github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
 github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
+github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
+github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
 github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
 github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
 github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=

+ 2 - 26
internal/database/database.go

@@ -7,42 +7,18 @@ import (
 	"database/sql"
 	"fmt"
 	"log/slog"
-	"time"
-
-	// Postgresql driver import
-	pq "github.com/lib/pq"
 )
 
-// NewConnectionPool configures the database connection pool.
-func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
-	db, err := sql.Open("postgres", dsn)
-	if err != nil {
-		return nil, err
-	}
-
-	db.SetMaxOpenConns(maxConnections)
-	db.SetMaxIdleConns(minConnections)
-	db.SetConnMaxLifetime(connectionLifetime)
-
-	return db, nil
-}
-
 // Migrate executes database migrations.
 func Migrate(db *sql.DB) error {
 	var currentVersion int
 	db.QueryRow(`SELECT version FROM schema_version`).Scan(&currentVersion)
 
-	driver := ""
-	switch db.Driver().(type) {
-	case *pq.Driver:
-		driver = "postgresql"
-	default:
-		panic(fmt.Sprintf("the driver %s isn't supported", db.Driver()))
-	}
-
+	driver := getDriverStr()
 	slog.Info("Running database migrations",
 		slog.Int("current_version", currentVersion),
 		slog.Int("latest_version", schemaVersion),
+		slog.String("driver", driver),
 	)
 
 	for version := currentVersion; version < schemaVersion; version++ {

+ 31 - 0
internal/database/postgresql.go

@@ -0,0 +1,31 @@
+//go:build !sqlite
+
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package database // import "miniflux.app/v2/internal/database"
+
+import (
+	"database/sql"
+	"time"
+
+	_ "github.com/lib/pq"
+)
+
+// NewConnectionPool configures the database connection pool.
+func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
+	db, err := sql.Open("postgres", dsn)
+	if err != nil {
+		return nil, err
+	}
+
+	db.SetMaxOpenConns(maxConnections)
+	db.SetMaxIdleConns(minConnections)
+	db.SetConnMaxLifetime(connectionLifetime)
+
+	return db, nil
+}
+
+func getDriverStr() string {
+	return "postgresql"
+}

+ 26 - 0
internal/database/sqlite.go

@@ -0,0 +1,26 @@
+//go:build sqlite
+
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package database // import "miniflux.app/v2/internal/database"
+
+import (
+	"database/sql"
+	"time"
+
+	_ "github.com/mattn/go-sqlite3"
+)
+
+// NewConnectionPool configures the database connection pool.
+func NewConnectionPool(dsn string, _, _ int, _ time.Duration) (*sql.DB, error) {
+	db, err := sql.Open("sqlite3", dsn)
+	if err != nil {
+		return nil, err
+	}
+	return db, nil
+}
+
+func getDriverStr() string {
+	return "sqlite3"
+}