2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package storage // import "miniflux.app/v2/internal/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2021-05-23 21:24:57 -04:00
|
|
|
"context"
|
2017-11-20 00:10:04 -05:00
|
|
|
"database/sql"
|
2021-05-23 21:24:57 -04:00
|
|
|
"time"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Storage handles all operations related to the database.
|
2017-11-20 00:10:04 -05:00
|
|
|
type Storage struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// NewStorage returns a new Storage.
|
2018-08-01 23:28:45 -04:00
|
|
|
func NewStorage(db *sql.DB) *Storage {
|
|
|
|
return &Storage{db}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2021-02-19 21:47:50 -05:00
|
|
|
|
|
|
|
// DatabaseVersion returns the version of the database which is in use.
|
|
|
|
func (s *Storage) DatabaseVersion() string {
|
|
|
|
var dbVersion string
|
|
|
|
err := s.db.QueryRow(`SELECT current_setting('server_version')`).Scan(&dbVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping checks if the database connection works.
|
|
|
|
func (s *Storage) Ping() error {
|
2021-05-23 21:24:57 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return s.db.PingContext(ctx)
|
2021-05-22 23:27:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DBStats returns database statistics.
|
|
|
|
func (s *Storage) DBStats() sql.DBStats {
|
|
|
|
return s.db.Stats()
|
2021-02-19 21:47:50 -05:00
|
|
|
}
|