release / go (push) Successful in 1m56s
release / web (push) Successful in 1m18s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m20s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped
- library.ProbeDurationMs and IsAudioFile, and db.MigrationsFS, had no callers. - The delta sync logged and hydrated like and playlist changes on every like, playlist edit and duplicate merge, including a GetPlaylistsByIDs query per sync. Android applies only artists, albums and tracks, and the web doesn't sync; likes and playlists reach both through their own endpoints and live events. The like and playlist-entry deletes also went to every caller unscoped, user ids included. They are no longer written or served, and migration 0112 clears the rows already logged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
_ "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
// Open establishes a pgx connection pool. The caller owns the returned pool
|
|
// and must Close() it on shutdown.
|
|
func Open(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
|
if dsn == "" {
|
|
return nil, errors.New("db: database URL is empty")
|
|
}
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("db: create pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("db: ping: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
// Migrate applies any pending migrations embedded in this package. It is safe
|
|
// to call concurrently with application traffic only if the database is idle;
|
|
// in practice this runs once at startup before the HTTP server binds.
|
|
func Migrate(dsn string, logger *slog.Logger) error {
|
|
if dsn == "" {
|
|
return errors.New("db: database URL is empty")
|
|
}
|
|
|
|
src, err := iofs.New(migrationsFS, "migrations")
|
|
if err != nil {
|
|
return fmt.Errorf("db: iofs source: %w", err)
|
|
}
|
|
|
|
m, err := migrate.NewWithSourceInstance("iofs", src, "pgx5://"+trimScheme(dsn))
|
|
if err != nil {
|
|
return fmt.Errorf("db: migrate init: %w", err)
|
|
}
|
|
defer func() {
|
|
if srcErr, dbErr := m.Close(); srcErr != nil || dbErr != nil {
|
|
logger.Warn("migrate close", "src_err", srcErr, "db_err", dbErr)
|
|
}
|
|
}()
|
|
|
|
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
|
return fmt.Errorf("db: migrate up: %w", err)
|
|
}
|
|
version, dirty, verr := m.Version()
|
|
if verr != nil && !errors.Is(verr, migrate.ErrNilVersion) {
|
|
return fmt.Errorf("db: migrate version: %w", verr)
|
|
}
|
|
logger.Info("migrations applied", "version", version, "dirty", dirty)
|
|
return nil
|
|
}
|
|
|
|
// trimScheme rewrites a postgres://... or postgresql://... DSN to the bare
|
|
// form expected by the migrate pgx driver (it registers its own scheme).
|
|
func trimScheme(dsn string) string {
|
|
for _, prefix := range []string{"postgres://", "postgresql://"} {
|
|
if len(dsn) > len(prefix) && dsn[:len(prefix)] == prefix {
|
|
return dsn[len(prefix):]
|
|
}
|
|
}
|
|
return dsn
|
|
}
|