feat(metadata): SQLite backend, storage reconciliation and local defaults
- pluggable per-domain Repository interfaces; PostgreSQL and SQLite impls - SQLite on modernc.org/sqlite (WAL, busy_timeout, foreign_keys, immediate tx) - separate migrations/sqlite and db.MigrateSQLite; store.NewPostgres/NewSQLite - cmd/reconcile compares object storage with the journal (report, then --delete) - storage.Backend.List and photos.Repository.AllStorageKeys - DB_BACKEND/SQLITE_PATH and STORAGE_BACKEND/FS_ROOT selectable; local defaults (sqlite ./galo.db, fs ./data) - Makefile reconcile/reconcile-delete; docs and .env.example updated
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package albums
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shared-album/internal/models"
|
||||
)
|
||||
|
||||
// Repository is the persistence contract for the albums domain.
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, a *models.Album) error
|
||||
OwnerID(ctx context.Context, id string) (string, error)
|
||||
GetByID(ctx context.Context, id string) (*models.Album, error)
|
||||
ListForUser(ctx context.Context, userID string) ([]models.Album, error)
|
||||
Update(ctx context.Context, a *models.Album) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
TrashPhotosByAlbum(ctx context.Context, albumID string) error
|
||||
}
|
||||
@@ -18,13 +18,13 @@ type MemberLookup interface {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
store *Store
|
||||
store Repository
|
||||
members MemberLookup
|
||||
hub *sse.Hub
|
||||
storage storage.Backend
|
||||
}
|
||||
|
||||
func NewService(store *Store, members MemberLookup, hub *sse.Hub, storage storage.Backend) *Service {
|
||||
func NewService(store Repository, members MemberLookup, hub *sse.Hub, storage storage.Backend) *Service {
|
||||
return &Service{store: store, members: members, hub: hub, storage: storage}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package albums
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"shared-album/internal/models"
|
||||
"shared-album/internal/sqliteutil"
|
||||
)
|
||||
|
||||
// SQLiteStore is the SQLite implementation of Repository.
|
||||
type SQLiteStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSQLiteStore(db *sql.DB) *SQLiteStore { return &SQLiteStore{db: db} }
|
||||
|
||||
func (s *SQLiteStore) Create(ctx context.Context, a *models.Album) error {
|
||||
var createdAt, updatedAt string
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`INSERT INTO albums (id, owner_id, title, description, cover_photo_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING created_at, updated_at`,
|
||||
a.ID, a.OwnerID, a.Title, a.Description, a.CoverPhotoID,
|
||||
).Scan(&createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.CreatedAt = sqliteutil.ParseTime(createdAt)
|
||||
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) OwnerID(ctx context.Context, id string) (string, error) {
|
||||
var ownerID string
|
||||
err := s.db.QueryRowContext(ctx, `SELECT owner_id FROM albums WHERE id = $1`, id).Scan(&ownerID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return ownerID, nil
|
||||
}
|
||||
|
||||
const sqliteAlbumColumns = `a.id, a.owner_id, a.title, a.description, a.cover_photo_id,
|
||||
a.created_at, a.updated_at, COALESCE(co.storage_key, ''),
|
||||
(SELECT count(*) FROM photos WHERE album_id = a.id),
|
||||
(SELECT count(*) FROM album_members WHERE album_id = a.id)`
|
||||
|
||||
const sqliteAlbumJoins = `
|
||||
LEFT JOIN photo_objects cpo ON cpo.photo_id = a.cover_photo_id AND cpo.role = 'thumb'
|
||||
LEFT JOIN objects co ON co.id = cpo.object_id`
|
||||
|
||||
func scanSQLiteAlbum(scan func(...any) error) (*models.Album, error) {
|
||||
var a models.Album
|
||||
var cover sql.NullString
|
||||
var createdAt, updatedAt string
|
||||
if err := scan(&a.ID, &a.OwnerID, &a.Title, &a.Description, &cover, &createdAt, &updatedAt,
|
||||
&a.CoverThumbKey, &a.PhotosCount, &a.MembersCount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cover.Valid {
|
||||
a.CoverPhotoID = &cover.String
|
||||
}
|
||||
a.CreatedAt = sqliteutil.ParseTime(createdAt)
|
||||
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetByID(ctx context.Context, id string) (*models.Album, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT `+sqliteAlbumColumns+` FROM albums a`+sqliteAlbumJoins+` WHERE a.id = $1`, id)
|
||||
a, err := scanSQLiteAlbum(row.Scan)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) ListForUser(ctx context.Context, userID string) ([]models.Album, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT `+sqliteAlbumColumns+` FROM albums a
|
||||
LEFT JOIN album_members m ON m.album_id = a.id AND m.user_id = $1`+sqliteAlbumJoins+`
|
||||
WHERE a.owner_id = $1 OR m.user_id IS NOT NULL
|
||||
ORDER BY a.created_at DESC`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []models.Album{}
|
||||
for rows.Next() {
|
||||
a, err := scanSQLiteAlbum(rows.Scan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, *a)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) Update(ctx context.Context, a *models.Album) error {
|
||||
var updatedAt string
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`UPDATE albums
|
||||
SET title = $2, description = $3, cover_photo_id = $4, updated_at = $5
|
||||
WHERE id = $1
|
||||
RETURNING updated_at`,
|
||||
a.ID, a.Title, a.Description, a.CoverPhotoID, sqliteutil.FormatTime(time.Now().UTC()),
|
||||
).Scan(&updatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) Delete(ctx context.Context, id string) error {
|
||||
res, err := s.db.ExecContext(ctx, `DELETE FROM albums WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) TrashPhotosByAlbum(ctx context.Context, albumID string) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE photos SET deleted_at = $2 WHERE album_id = $1 AND deleted_at IS NULL`,
|
||||
albumID, sqliteutil.FormatTime(time.Now().UTC()))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user