From daa0092de64ef96dac48f2d19d23b3c8a0bee6a0 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 29 Dec 2024 21:50:32 +0100 Subject: [PATCH] Add 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. --- go.mod | 1 + go.sum | 2 ++ internal/database/database.go | 28 ++-------------------------- internal/database/postgresql.go | 31 +++++++++++++++++++++++++++++++ internal/database/sqlite.go | 26 ++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 internal/database/postgresql.go create mode 100644 internal/database/sqlite.go diff --git a/go.mod b/go.mod index 82308ef5ac3..09e7f4d0382 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index abfd5ddf3f0..bd9edb5a183 100644 --- a/go.sum +++ b/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= diff --git a/internal/database/database.go b/internal/database/database.go index 851d601fb61..c8086554c2d 100644 --- a/internal/database/database.go +++ b/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(¤tVersion) - 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++ { diff --git a/internal/database/postgresql.go b/internal/database/postgresql.go new file mode 100644 index 00000000000..2e579d797e5 --- /dev/null +++ b/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" +} diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go new file mode 100644 index 00000000000..51d90ef90d5 --- /dev/null +++ b/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" +}