storage.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage // import "miniflux.app/storage"
  5. import (
  6. "context"
  7. "database/sql"
  8. "time"
  9. )
  10. // Storage handles all operations related to the database.
  11. type Storage struct {
  12. db *sql.DB
  13. }
  14. // NewStorage returns a new Storage.
  15. func NewStorage(db *sql.DB) *Storage {
  16. return &Storage{db}
  17. }
  18. // DatabaseVersion returns the version of the database which is in use.
  19. func (s *Storage) DatabaseVersion() string {
  20. var dbVersion string
  21. err := s.db.QueryRow(`SELECT current_setting('server_version')`).Scan(&dbVersion)
  22. if err != nil {
  23. return err.Error()
  24. }
  25. return dbVersion
  26. }
  27. // Ping checks if the database connection works.
  28. func (s *Storage) Ping() error {
  29. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  30. defer cancel()
  31. return s.db.PingContext(ctx)
  32. }
  33. // DBStats returns database statistics.
  34. func (s *Storage) DBStats() sql.DBStats {
  35. return s.db.Stats()
  36. }