diff --git a/cli/cli.go b/cli/cli.go index 4f5417ca..f4014db3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -10,6 +10,7 @@ import ( "github.com/miniflux/miniflux/config" "github.com/miniflux/miniflux/daemon" + "github.com/miniflux/miniflux/database" "github.com/miniflux/miniflux/logger" "github.com/miniflux/miniflux/storage" "github.com/miniflux/miniflux/version" @@ -33,10 +34,13 @@ func Parse() { logger.EnableDebug() } - store := storage.NewStorage( - cfg.DatabaseURL(), - cfg.DatabaseMaxConnections(), - ) + db, err := database.NewConnectionPool(cfg.DatabaseURL(), cfg.DatabaseMinConns(), cfg.DatabaseMaxConns()) + if err != nil { + logger.Fatal("Unable to connect to the database: %v", err) + } + defer db.Close() + + store := storage.NewStorage(db) if *flagInfo { info() @@ -49,7 +53,7 @@ func Parse() { } if *flagMigrate { - store.Migrate() + database.Migrate(db) return } @@ -75,7 +79,7 @@ func Parse() { // Run migrations and start the deamon. if cfg.RunMigrations() { - store.Migrate() + database.Migrate(db) } // Create admin user and start the deamon. diff --git a/config/config.go b/config/config.go index 4cba7895..580f01d6 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,7 @@ const ( defaultPollingFrequency = 60 defaultBatchSize = 10 defaultDatabaseMaxConns = 20 + defaultDatabaseMinConns = 1 defaultListenAddr = "127.0.0.1:8080" defaultCertFile = "" defaultKeyFile = "" @@ -119,11 +120,16 @@ func (c *Config) DatabaseURL() string { return value } -// DatabaseMaxConnections returns the number of maximum database connections. -func (c *Config) DatabaseMaxConnections() int { +// DatabaseMaxConns returns the maximum number of database connections. +func (c *Config) DatabaseMaxConns() int { return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns) } +// DatabaseMinConns returns the minimum number of database connections. +func (c *Config) DatabaseMinConns() int { + return c.getInt("DATABASE_MIN_CONNS", defaultDatabaseMinConns) +} + // ListenAddr returns the listen address for the HTTP server. func (c *Config) ListenAddr() string { if port := os.Getenv("PORT"); port != "" { diff --git a/daemon/daemon.go b/daemon/daemon.go index cdd81ef1..de91d71a 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -59,6 +59,5 @@ func Run(cfg *config.Config, store *storage.Storage) { defer cancel() server.Shutdown(ctx) - store.Close() logger.Info("Server gracefully stopped") } diff --git a/database/database.go b/database/database.go new file mode 100644 index 00000000..42f22925 --- /dev/null +++ b/database/database.go @@ -0,0 +1,25 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package database + +import ( + "database/sql" + + // Postgresql driver import + _ "github.com/lib/pq" +) + +// NewConnectionPool configures the database connection pool. +func NewConnectionPool(dsn string, minConnections, maxConnections int) (*sql.DB, error) { + db, err := sql.Open("postgres", dsn) + if err != nil { + return nil, err + } + + db.SetMaxOpenConns(maxConnections) + db.SetMaxIdleConns(minConnections) + + return db, nil +} diff --git a/storage/migration.go b/database/migration.go similarity index 59% rename from storage/migration.go rename to database/migration.go index e78f1d55..e348e40c 100644 --- a/storage/migration.go +++ b/database/migration.go @@ -1,23 +1,23 @@ -// Copyright 2017 Frédéric Guillot. All rights reserved. +// Copyright 2018 Frédéric Guillot. All rights reserved. // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. -package storage +package database import ( + "database/sql" "fmt" "strconv" "github.com/miniflux/miniflux/logger" - "github.com/miniflux/miniflux/sql" ) const schemaVersion = 20 -// Migrate run database migrations. -func (s *Storage) Migrate() { +// Migrate executes database migrations. +func Migrate(db *sql.DB) { var currentVersion int - s.db.QueryRow(`select version from schema_version`).Scan(¤tVersion) + db.QueryRow(`select version from schema_version`).Scan(¤tVersion) fmt.Println("Current schema version:", currentVersion) fmt.Println("Latest schema version:", schemaVersion) @@ -25,31 +25,31 @@ func (s *Storage) Migrate() { for version := currentVersion + 1; version <= schemaVersion; version++ { fmt.Println("Migrating to version:", version) - tx, err := s.db.Begin() + tx, err := db.Begin() if err != nil { - logger.Fatal("[Storage:Migrate] %v", err) + logger.Fatal("[Migrate] %v", err) } - rawSQL := sql.SqlMap["schema_version_"+strconv.Itoa(version)] + rawSQL := SqlMap["schema_version_"+strconv.Itoa(version)] // fmt.Println(rawSQL) _, err = tx.Exec(rawSQL) if err != nil { tx.Rollback() - logger.Fatal("[Storage:Migrate] %v", err) + logger.Fatal("[Migrate] %v", err) } if _, err := tx.Exec(`delete from schema_version`); err != nil { tx.Rollback() - logger.Fatal("[Storage:Migrate] %v", err) + logger.Fatal("[Migrate] %v", err) } if _, err := tx.Exec(`insert into schema_version (version) values($1)`, version); err != nil { tx.Rollback() - logger.Fatal("[Storage:Migrate] %v", err) + logger.Fatal("[Migrate] %v", err) } if err := tx.Commit(); err != nil { - logger.Fatal("[Storage:Migrate] %v", err) + logger.Fatal("[Migrate] %v", err) } } } diff --git a/sql/sql.go b/database/sql.go similarity index 99% rename from sql/sql.go rename to database/sql.go index 60af4c5b..a9468ecb 100644 --- a/sql/sql.go +++ b/database/sql.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. -package sql +package database var SqlMap = map[string]string{ "schema_version_1": `create table schema_version ( diff --git a/sql/schema_version_1.sql b/database/sql/schema_version_1.sql similarity index 100% rename from sql/schema_version_1.sql rename to database/sql/schema_version_1.sql diff --git a/sql/schema_version_10.sql b/database/sql/schema_version_10.sql similarity index 100% rename from sql/schema_version_10.sql rename to database/sql/schema_version_10.sql diff --git a/sql/schema_version_11.sql b/database/sql/schema_version_11.sql similarity index 100% rename from sql/schema_version_11.sql rename to database/sql/schema_version_11.sql diff --git a/sql/schema_version_12.sql b/database/sql/schema_version_12.sql similarity index 100% rename from sql/schema_version_12.sql rename to database/sql/schema_version_12.sql diff --git a/sql/schema_version_13.sql b/database/sql/schema_version_13.sql similarity index 100% rename from sql/schema_version_13.sql rename to database/sql/schema_version_13.sql diff --git a/sql/schema_version_14.sql b/database/sql/schema_version_14.sql similarity index 100% rename from sql/schema_version_14.sql rename to database/sql/schema_version_14.sql diff --git a/sql/schema_version_15.sql b/database/sql/schema_version_15.sql similarity index 100% rename from sql/schema_version_15.sql rename to database/sql/schema_version_15.sql diff --git a/sql/schema_version_16.sql b/database/sql/schema_version_16.sql similarity index 100% rename from sql/schema_version_16.sql rename to database/sql/schema_version_16.sql diff --git a/sql/schema_version_17.sql b/database/sql/schema_version_17.sql similarity index 100% rename from sql/schema_version_17.sql rename to database/sql/schema_version_17.sql diff --git a/sql/schema_version_18.sql b/database/sql/schema_version_18.sql similarity index 100% rename from sql/schema_version_18.sql rename to database/sql/schema_version_18.sql diff --git a/sql/schema_version_19.sql b/database/sql/schema_version_19.sql similarity index 100% rename from sql/schema_version_19.sql rename to database/sql/schema_version_19.sql diff --git a/sql/schema_version_2.sql b/database/sql/schema_version_2.sql similarity index 100% rename from sql/schema_version_2.sql rename to database/sql/schema_version_2.sql diff --git a/sql/schema_version_20.sql b/database/sql/schema_version_20.sql similarity index 100% rename from sql/schema_version_20.sql rename to database/sql/schema_version_20.sql diff --git a/sql/schema_version_3.sql b/database/sql/schema_version_3.sql similarity index 100% rename from sql/schema_version_3.sql rename to database/sql/schema_version_3.sql diff --git a/sql/schema_version_4.sql b/database/sql/schema_version_4.sql similarity index 100% rename from sql/schema_version_4.sql rename to database/sql/schema_version_4.sql diff --git a/sql/schema_version_5.sql b/database/sql/schema_version_5.sql similarity index 100% rename from sql/schema_version_5.sql rename to database/sql/schema_version_5.sql diff --git a/sql/schema_version_6.sql b/database/sql/schema_version_6.sql similarity index 100% rename from sql/schema_version_6.sql rename to database/sql/schema_version_6.sql diff --git a/sql/schema_version_7.sql b/database/sql/schema_version_7.sql similarity index 100% rename from sql/schema_version_7.sql rename to database/sql/schema_version_7.sql diff --git a/sql/schema_version_8.sql b/database/sql/schema_version_8.sql similarity index 100% rename from sql/schema_version_8.sql rename to database/sql/schema_version_8.sql diff --git a/sql/schema_version_9.sql b/database/sql/schema_version_9.sql similarity index 100% rename from sql/schema_version_9.sql rename to database/sql/schema_version_9.sql diff --git a/generate.go b/generate.go index 7b845aa9..cba64e31 100644 --- a/generate.go +++ b/generate.go @@ -209,7 +209,7 @@ func main() { generateBinaryBundle("ui/static/bin.go", glob("ui/static/bin/*")) - generateBundle("sql/sql.go", "sql", "SqlMap", glob("sql/*.sql")) + generateBundle("database/sql.go", "database", "SqlMap", glob("database/sql/*.sql")) generateBundle("template/views.go", "template", "templateViewsMap", glob("template/html/*.html")) generateBundle("template/common.go", "template", "templateCommonMap", glob("template/html/common/*.html")) generateBundle("locale/translations.go", "locale", "translations", glob("locale/translations/*.json")) diff --git a/main.go b/main.go index 951329a8..44f38dbb 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ package main //go:generate go run generate.go -//go:generate gofmt -s -w sql/sql.go +//go:generate gofmt -s -w database/sql.go //go:generate gofmt -s -w ui/static/css.go //go:generate gofmt -s -w ui/static/bin.go //go:generate gofmt -s -w ui/static/js.go diff --git a/sql/doc.go b/sql/doc.go deleted file mode 100644 index 2096efc6..00000000 --- a/sql/doc.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2018 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the MIT license -// that can be found in the LICENSE file. - -/* - -Package sql contains all SQL queries or database schema instructions. - -*/ -package sql diff --git a/storage/storage.go b/storage/storage.go index f7a06b4d..19372f07 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -6,10 +6,6 @@ package storage import ( "database/sql" - - // Postgresql driver import - _ "github.com/lib/pq" - "github.com/miniflux/miniflux/logger" ) // Storage handles all operations related to the database. @@ -17,20 +13,7 @@ type Storage struct { db *sql.DB } -// Close closes all database connections. -func (s *Storage) Close() { - s.db.Close() -} - // NewStorage returns a new Storage. -func NewStorage(databaseURL string, maxOpenConns int) *Storage { - db, err := sql.Open("postgres", databaseURL) - if err != nil { - logger.Fatal("[Storage] Unable to connect to the database: %v", err) - } - - db.SetMaxOpenConns(maxOpenConns) - db.SetMaxIdleConns(2) - - return &Storage{db: db} +func NewStorage(db *sql.DB) *Storage { + return &Storage{db} }