mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-03-18 21:30:44 +01:00
WIP: Postgres read-only replica
This commit is contained in:
@@ -24,7 +24,7 @@ var (
|
||||
|
||||
// Store holds the database connection and queries for web push subscriptions.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
db *db.DB
|
||||
queries queries
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ func (s *Store) UpsertSubscription(endpoint string, auth, p256dh, userID string,
|
||||
|
||||
// SubscriptionsForTopic returns all subscriptions for the given topic.
|
||||
func (s *Store) SubscriptionsForTopic(topic string) ([]*Subscription, error) {
|
||||
rows, err := s.db.Query(s.queries.selectSubscriptionsForTopic, topic)
|
||||
rows, err := s.db.ReadOnly().Query(s.queries.selectSubscriptionsForTopic, topic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func (s *Store) SubscriptionsForTopic(topic string) ([]*Subscription, error) {
|
||||
|
||||
// SubscriptionsExpiring returns all subscriptions that have not been updated for a given time period.
|
||||
func (s *Store) SubscriptionsExpiring(warnAfter time.Duration) ([]*Subscription, error) {
|
||||
rows, err := s.db.Query(s.queries.selectSubscriptionsExpiringSoon, time.Now().Add(-warnAfter).Unix())
|
||||
rows, err := s.db.ReadOnly().Query(s.queries.selectSubscriptionsExpiringSoon, time.Now().Add(-warnAfter).Unix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"heckel.io/ntfy/v2/db"
|
||||
ntfydb "heckel.io/ntfy/v2/db"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -73,12 +73,12 @@ const (
|
||||
)
|
||||
|
||||
// NewPostgresStore creates a new PostgreSQL-backed web push store using an existing database connection pool.
|
||||
func NewPostgresStore(db *sql.DB) (*Store, error) {
|
||||
if err := setupPostgres(db); err != nil {
|
||||
func NewPostgresStore(d *ntfydb.DB) (*Store, error) {
|
||||
if err := setupPostgres(d.SetupPrimary()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{
|
||||
db: db,
|
||||
db: d,
|
||||
queries: queries{
|
||||
selectSubscriptionIDByEndpoint: postgresSelectSubscriptionIDByEndpointQuery,
|
||||
selectSubscriptionCountBySubscriberIP: postgresSelectSubscriptionCountBySubscriberIPQuery,
|
||||
@@ -110,7 +110,7 @@ func setupPostgres(db *sql.DB) error {
|
||||
}
|
||||
|
||||
func setupNewPostgres(sqlDB *sql.DB) error {
|
||||
return db.ExecTx(sqlDB, func(tx *sql.Tx) error {
|
||||
return ntfydb.ExecTx(sqlDB, func(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(postgresCreateTablesQuery); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -79,18 +79,18 @@ const (
|
||||
|
||||
// NewSQLiteStore creates a new SQLite-backed web push store.
|
||||
func NewSQLiteStore(filename, startupQueries string) (*Store, error) {
|
||||
db, err := sql.Open("sqlite3", filename)
|
||||
sqlDB, err := sql.Open("sqlite3", filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := setupSQLite(db); err != nil {
|
||||
if err := setupSQLite(sqlDB); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := runSQLiteStartupQueries(db, startupQueries); err != nil {
|
||||
if err := runSQLiteStartupQueries(sqlDB, startupQueries); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{
|
||||
db: db,
|
||||
db: db.NewDB(sqlDB, nil),
|
||||
queries: queries{
|
||||
selectSubscriptionIDByEndpoint: sqliteSelectSubscriptionIDByEndpointQuery,
|
||||
selectSubscriptionCountBySubscriberIP: sqliteSelectSubscriptionCountBySubscriberIPQuery,
|
||||
|
||||
Reference in New Issue
Block a user