1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-30 19:22:11 +00:00

feat: multi db support

This commit is contained in:
haras 2025-09-14 08:50:42 +02:00
parent 10b2b36895
commit fe6c000897
25 changed files with 2612 additions and 1433 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt"
"strings"
"miniflux.app/v2/internal/database"
"miniflux.app/v2/internal/model"
"github.com/lib/pq"
@ -49,7 +50,6 @@ func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
&enclosure.MimeType,
&enclosure.MediaProgression,
)
if err != nil {
return nil, fmt.Errorf(`store: unable to fetch enclosure row: %v`, err)
}
@ -150,15 +150,20 @@ func (s *Storage) createEnclosure(tx *sql.Tx, enclosure *model.Enclosure) error
return nil
}
query := `
urlPart := "md5(url)"
if s.kind == database.DBKindCockroach {
urlPart = "url"
}
query := fmt.Sprintf(`
INSERT INTO enclosures
(url, size, mime_type, entry_id, user_id, media_progression)
VALUES
($1, $2, $3, $4, $5, $6)
ON CONFLICT (user_id, entry_id, md5(url)) DO NOTHING
ON CONFLICT (user_id, entry_id, %s) DO NOTHING
RETURNING
id
`
`, urlPart)
if err := tx.QueryRow(
query,
enclosureURL,
@ -226,7 +231,6 @@ func (s *Storage) UpdateEnclosure(enclosure *model.Enclosure) error {
enclosure.MediaProgression,
enclosure.ID,
)
if err != nil {
return fmt.Errorf(`store: unable to update enclosure #%d : %v`, enclosure.ID, err)
}

View file

@ -11,6 +11,7 @@ import (
"time"
"miniflux.app/v2/internal/crypto"
"miniflux.app/v2/internal/database"
"miniflux.app/v2/internal/model"
"github.com/lib/pq"
@ -70,17 +71,21 @@ func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
// UpdateEntryTitleAndContent updates entry title and content.
func (s *Storage) UpdateEntryTitleAndContent(entry *model.Entry) error {
truncatedTitle, truncatedContent := truncateTitleAndContentForTSVectorField(entry.Title, entry.Content)
query := `
setweightPart := "setweight(to_tsvector($4), 'A') || setweight(to_tsvector($5), 'B')"
if s.kind == database.DBKindCockroach {
setweightPart = "to_tsvector(substring($4 || ' ' || $4 || ' ' || coalesce($5, '') for 1000000))"
}
query := fmt.Sprintf(`
UPDATE
entries
SET
title=$1,
content=$2,
reading_time=$3,
document_vectors = setweight(to_tsvector($4), 'A') || setweight(to_tsvector($5), 'B')
document_vectors = %s
WHERE
id=$6 AND user_id=$7
`
`, setweightPart)
if _, err := s.db.Exec(
query,
@ -100,7 +105,11 @@ func (s *Storage) UpdateEntryTitleAndContent(entry *model.Entry) error {
// createEntry add a new entry.
func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
truncatedTitle, truncatedContent := truncateTitleAndContentForTSVectorField(entry.Title, entry.Content)
query := `
setweightPart := "setweight(to_tsvector($11), 'A') || setweight(to_tsvector($12), 'B')"
if s.kind == database.DBKindCockroach {
setweightPart = "to_tsvector(substring($11 || ' ' || $11 || ' ' || coalesce($12, '') for 1000000))"
}
query := fmt.Sprintf(`
INSERT INTO entries
(
title,
@ -130,12 +139,12 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
$9,
$10,
now(),
setweight(to_tsvector($11), 'A') || setweight(to_tsvector($12), 'B'),
%s,
$13
)
RETURNING
id, status, created_at, changed_at
`
`, setweightPart)
err := tx.QueryRow(
query,
entry.Title,
@ -178,7 +187,11 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
// it default to time.Now() which could change the order of items on the history page.
func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
truncatedTitle, truncatedContent := truncateTitleAndContentForTSVectorField(entry.Title, entry.Content)
query := `
setweightPart := "setweight(to_tsvector($7), 'A') || setweight(to_tsvector($8), 'B')"
if s.kind == database.DBKindCockroach {
setweightPart = "to_tsvector(substring($7 || ' ' || $7 || ' ' || coalesce($8, '') for 1000000))"
}
query := fmt.Sprintf(`
UPDATE
entries
SET
@ -188,13 +201,13 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
content=$4,
author=$5,
reading_time=$6,
document_vectors = setweight(to_tsvector($7), 'A') || setweight(to_tsvector($8), 'B'),
document_vectors = %s,
tags=$12
WHERE
user_id=$9 AND feed_id=$10 AND hash=$11
RETURNING
id
`
`, setweightPart)
err := tx.QueryRow(
query,
entry.Title,

View file

@ -7,16 +7,19 @@ import (
"context"
"database/sql"
"time"
"miniflux.app/v2/internal/database"
)
// Storage handles all operations related to the database.
type Storage struct {
db *sql.DB
kind database.DBKind
db *sql.DB
}
// NewStorage returns a new Storage.
func NewStorage(db *sql.DB) *Storage {
return &Storage{db}
func NewStorage(kind database.DBKind, db *sql.DB) *Storage {
return &Storage{kind, db}
}
// DatabaseVersion returns the version of the database which is in use.