Files
minstrel/internal/db/dbq/library_changes.sql.go
T
bvandeusenandClaude Opus 5.5 29042e4eb2
release / govulncheck (push) Successful in 29s
release / web (push) Failing after 1m21s
release / go (push) Successful in 1m39s
release / integration (push) Successful in 5m26s
release / android (push) Successful in 5m27s
release / Build signed APK (push) Successful in 5m44s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
feat: a new Minstrel restores a backup, from the web app or the command line, then rescans (#6775)
A new server (no accounts, no library) offers "Restore a backup" beside
"Create account". The file goes up as the request body to
POST /api/auth/restore with the setup token from the log in X-Setup-Token,
since the backup's admins become this server's. It loads in one transaction,
then a library scan starts and finds each file at its path. The same load runs
offline as `minstrel restore FILE`, which migrates the schema first.

That path alone gets a 1 GiB, 15-minute body allowance instead of 4 MiB and
30s, and skips the JSON-only check for cookie writes: a stale cookie from the
old server would otherwise refuse the upload, and the custom header already
rules out a cross-site form.

The library sync answers 410 cursor_ahead to a cursor past the last one the
server has handed out, read from the change sequence, so an app synced with
the old server after the backup was made starts over instead of skipping the
ids the restored server reuses. Restore now leaves each sequence as having
issued the restored maximum, so that reading holds; a compacted log still
counts its handed-out cursors.

backup.Read keeps the underlying error in its chain, so an oversized upload
answers 413 rather than "not a backup". The hosting docs say how to put the
music back first and what is entered again afterwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 16:10:06 -05:00

141 lines
4.0 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: library_changes.sql
package dbq
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const deleteOldLibraryChanges = `-- name: DeleteOldLibraryChanges :execrows
DELETE FROM library_changes WHERE changed_at < $1
`
// Removes library_changes rows older than the given cutoff. Run from
// the sync compactor goroutine on a daily tick. Returns the number of
// rows deleted so the worker can log meaningful progress.
func (q *Queries) DeleteOldLibraryChanges(ctx context.Context, changedAt pgtype.Timestamptz) (int64, error) {
result, err := q.db.Exec(ctx, deleteOldLibraryChanges, changedAt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getLastIssuedLibraryChangeCursor = `-- name: GetLastIssuedLibraryChangeCursor :one
SELECT COALESCE(pg_sequence_last_value(pg_get_serial_sequence('library_changes', 'id')::regclass), 0)::BIGINT
`
// The highest cursor this server has handed out, even once the compactor has
// trimmed the rows: the id sequence's last value. A restored server's
// sequence stands at the backup's last change.
func (q *Queries) GetLastIssuedLibraryChangeCursor(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, getLastIssuedLibraryChangeCursor)
var column_1 int64
err := row.Scan(&column_1)
return column_1, err
}
const getLibraryChangesSince = `-- name: GetLibraryChangesSince :many
SELECT id, entity_type, entity_id, op, changed_at
FROM library_changes
WHERE id > $1
ORDER BY id ASC
LIMIT $2
`
type GetLibraryChangesSinceParams struct {
ID int64
Limit int32
}
func (q *Queries) GetLibraryChangesSince(ctx context.Context, arg GetLibraryChangesSinceParams) ([]LibraryChange, error) {
rows, err := q.db.Query(ctx, getLibraryChangesSince, arg.ID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []LibraryChange
for rows.Next() {
var i LibraryChange
if err := rows.Scan(
&i.ID,
&i.EntityType,
&i.EntityID,
&i.Op,
&i.ChangedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getMaxLibraryChangeCursor = `-- name: GetMaxLibraryChangeCursor :one
SELECT COALESCE(MAX(id), 0)::BIGINT FROM library_changes
`
func (q *Queries) GetMaxLibraryChangeCursor(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, getMaxLibraryChangeCursor)
var column_1 int64
err := row.Scan(&column_1)
return column_1, err
}
const getMinLibraryChangeCursor = `-- name: GetMinLibraryChangeCursor :one
SELECT COALESCE(MIN(id), 0)::BIGINT FROM library_changes
`
func (q *Queries) GetMinLibraryChangeCursor(ctx context.Context) (int64, error) {
row := q.db.QueryRow(ctx, getMinLibraryChangeCursor)
var column_1 int64
err := row.Scan(&column_1)
return column_1, err
}
const insertLibraryChange = `-- name: InsertLibraryChange :exec
INSERT INTO library_changes (entity_type, entity_id, op)
VALUES ($1, $2, $3)
`
type InsertLibraryChangeParams struct {
EntityType string
EntityID string
Op string
}
func (q *Queries) InsertLibraryChange(ctx context.Context, arg InsertLibraryChangeParams) error {
_, err := q.db.Exec(ctx, insertLibraryChange, arg.EntityType, arg.EntityID, arg.Op)
return err
}
const insertLibraryChanges = `-- name: InsertLibraryChanges :exec
INSERT INTO library_changes (entity_type, entity_id, op)
SELECT $1::text,
unnest($2::text[]),
$3::text
`
type InsertLibraryChangesParams struct {
EntityType string
EntityIds []string
Op string
}
// Batch form, for a mutation that touches many rows at once (the scan's
// reconcile pass can mark up to a quarter of a library missing in one go). A
// loop of single inserts would be thousands of round-trips inside a scan that
// is already the slow path.
func (q *Queries) InsertLibraryChanges(ctx context.Context, arg InsertLibraryChangesParams) error {
_, err := q.db.Exec(ctx, insertLibraryChanges, arg.EntityType, arg.EntityIds, arg.Op)
return err
}