* feat(downloads): offline sync for mobile (downloads v2) Replace internal/download with a unified internal/downloads package and add fully-offline download + watch-sync support for mobile clients, across five independently-shippable phases: - Phase 0: reshape the downloads table and the /downloads contract to be device- and format-aware; add GET /downloads/capability; extend DownloadConfig (default-off keys); update the web download hooks/components in lockstep. This is the one approved pre-lock exception to the additive-only /api/v1 rule (the web app is the only consumer and is updated together). - Phase 1: managed device-library entries (create/list/PATCH/delete/serve), keyed on the X-Silo-Device-Id header. - Phase 2: offline playback manifest plus artwork/subtitle proxy endpoints that strip every presigned URL (inline thumbhashes + authenticated proxies). - Phase 3: prepare-to-file (remux + transcode-to-single-file) as a durable, leased artifact queue with startup recovery, hosted on the task manager; playback.PrepareFile emits one +faststart MP4. Adds the admin transcode toggle and per-artifact LRU cleanup. - Phase 4: offline progress reconciliation -- a clamped event_at LWW key plus a server-assigned synced_seq cursor on watch_progress; an optional clamped updated_at on POST /sync/progress and an opaque ?since= cursor on GET /progress (additive; existing callers unaffected). Security & reliability invariants, each with an acceptance test: 1. Server-owned sync ordering: ?since= delta delivery is driven only by the server-assigned synced_seq; the client clock is bounded (event_at, clamped to now+skew) and used only for last-write-wins on the caller's own profile. 2. Full profile+device authorization on every managed endpoint, with a per-profile content/library access re-check before serving any bytes/assets. 3. Durable artifact recovery: a transactionally-claimed (FOR UPDATE SKIP LOCKED), lease-heartbeat, attempt-counted queue with a startup sweep, so no crash strands a download in preparing and concurrent workers never double-encode. Migrations are timestamped Goose files: reshape downloads (device/format); download_artifacts (durable queue); watch_progress event_at/synced_seq. DB-backed acceptance tests skip without SILO_TEST_DATABASE_URL and run in CI; the invariant-1 progress test also runs against the real SQLite backend locally. Client repos (silo-android, silo-apple) consume the reshaped /downloads/* contract and the updated_at/?since= progress fields and require coordinated follow-up. Implements the maintainer-approved v1 capability proposal for offline sync (downloads v2). AI-use disclosure: implemented by Claude (Claude Code) from the approved design doc under docs/superpowers/specs, with human review. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * feat(downloads): series & season downloads + client-pull monitoring Build season downloads and a "monitor a series" capability on top of the downloads v2 (offline sync for mobile) work. Season downloads: - POST /downloads accepts season_number (with series:true) to download one season. CreateSeries/CreateSeason share one body via a listEpisodes closure and register managed entries under a shared batch_id (original-only). Episode files are resolved in a single batched query. Series monitoring (auto-download), client-driven: - New device-scoped download_subscriptions table with a Sonarr-style mode (all | future | latest_season | specific_seasons), a client-enforced delete_watched flag, and a max_storage_bytes cap. The server never deletes on-device files; retention and the hard cap are the client's, the server only soft-gates registration. - The client calls POST /downloads/subscriptions/sync on open / background refresh; the server registers the in-scope, not-yet-downloaded episodes (idempotent via the managed-entry unique index) and the device pulls them on its own schedule. No background worker and no dependency on the notifications subsystem. latest_season follows new seasons (>= subscribe-time season); future excludes the back catalog via air date. - Subscription CRUD + sync are profile+device authorized (device id from the X-Silo-Device-Id header only) with a per-request content-access re-check. The capability endpoint advertises season_download / series_monitoring / monitoring_modes. Also lands the downloads-v2 work already present in the tree: durable artifact (remux/transcode) preparation and offline watch-progress reconciliation, plus the design-spec updates. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * WIP: epitaxy pre-switch from feat/downloads-v2-offline-sync * test(downloads): fix deterministic ID collision in reconcile test Artifact IDs are time-sortable, so two artifacts created in the same moment share their first 8 chars; combined with a captured timestamp the two preparing-download IDs collided on downloads_pkey. Use the full artifact ID, which is unique per row. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): support sqlite userdb backend for managed downloads With the sqlite userdb backend, profiles live only in per-user SQLite stores and public.user_profiles stays empty, so user_devices' profile FK made every managed create/subscription/offline-sync request fail with an FK violation. Drop the FK (shared Postgres tables must not FK profile tables — same rule as notifications) and replace the lost cascade with an app-level purge on profile deletion, wired through ProfileHandler for both backends. DB-backed regression tests cover the no-Postgres-profile-row path and the purge cascade. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): dispatch encode kick asynchronously triggerDrain invoked the kick inline, and the kick (taskmanager RunTask) executes the encode task on the caller's goroutine — so a POST /api/v1/downloads with a bitrate quality blocked the HTTP request on the entire queue drain, ffmpeg encodes included, delaying the 202 by minutes on an idle queue. Dispatch the kick on a goroutine; the task manager already serializes concurrent runs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): enforce per-user quota on the encode pipeline Two gaps let a user bypass MaxConcurrentPerUser entirely for prepared downloads: artifact-backed rows are created in 'preparing' (never 'queued'/'downloading'), which CountActiveByUser didn't count, and createArtifactDownload enqueued the encode job before limiter.Check, so even a 429-rejected request left a job the worker would transcode. Count 'preparing' as active and check the limiter before Ensure; managed replacements stay quota-exempt since they don't add a row. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): protect ephemeral artifact links from LRU eviction HasActiveLink only counted managed (device_id IS NOT NULL) rows, so under a byte budget Cleanup could delete an artifact still referenced by a ready-but-unfetched ephemeral web download — permanently 404ing a row the API kept listing as ready (the artifact row is gone, so recovery can't re-queue it). Any non-terminal link now protects the artifact; only artifacts whose links are all cancelled/failed/revoked are evictable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): batch manifests skip bad entries instead of failing whole batch One deleted or access-filtered episode made GET /downloads/batches/{id}/manifests 404 for the entire season, so a client could no longer fetch manifests for the still-valid entries. Report unbuildable entries in a skipped[] array (revoked | not_found | error) alongside the delivered manifests, mirroring the create path's skip idiom. Also cut the batch cost: the shared series detail is resolved once per batch instead of once per episode, and buildSubtitles reuses the already-loaded media file instead of re-querying it per manifest. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(migrations): wrap DO block in StatementBegin/End markers Under NO TRANSACTION goose splits statements on semicolons, so the dollar-quoted DO block failed every fresh install with 'unterminated dollar-quoted string' (SQLSTATE 42601). Already-applied databases are unaffected. Same fix is being applied to main; identical content merges cleanly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): allow season 0 (Specials) in season downloads season_number was a plain int dispatched with '> 0', so requesting the Specials season was indistinguishable from omitting the field and silently broadened to a full-series download. Dispatch on pointer presence, treat 0 as the Specials season, and reject negatives with 400. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): capability quality_presets is never JSON null PresetsFor returned a nil slice when downloads are disabled or the user lacks the permission, and Capability's []string{} initialization was immediately overwritten by it — so GET /downloads/capability serialized "quality_presets": null where the contract documents an array. Normalize at the source so every caller inherits the guarantee. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): subscription sync correctness + batched registration Three subscription fixes: - A paused subscription no longer syncs: PATCHing scope (or pausing and changing scope in one request) registered episodes for a monitor the user had just stopped, inconsistently with SyncSubscriptions' guard. - SubModeFuture compares calendar days (UTC): air_date is date-only, so the strict instant comparison permanently excluded episodes airing the same day the user subscribed; episodes with no air date now fall back to their ingest time instead of never registering. - Registration is one batched fetch (GetManagedEntriesByKeys) plus one batched INSERT ... ON CONFLICT DO NOTHING RETURNING (CreateManagedEntriesBatch) instead of a SELECT+INSERT per episode — a 300-episode series cost ~600 sequential round trips per request and every no-op sync re-walked the full set. RETURNING yields exactly the new rows, so the sync response's 'registered' count now honestly reports 0 in the steady state instead of the full in-scope count on every app open. The now-unused InsertManagedEntryIfAbsent is removed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(userstore): stamp triggers own the event_at LWW key MarkProgressBatch (jellycompat series mark-played) advanced updated_at but never event_at, and both stamp triggers only defaulted event_at when NULL — so a queued offline event with a client time between the row's old event_at and the mark could win SetProgressIfNewer and resurrect a stale resume position that then re-synced to every device. Make the triggers authoritative instead of adding a tenth hand-written SET clause: whenever an UPDATE changes updated_at without explicitly changing event_at, the trigger advances the LWW key; writes that do set event_at (offline sync's clamped client event time) keep their value. Postgres gets a CREATE OR REPLACE migration; SQLite gets a v12 userdb migration that drops and reinstalls the trigger bodies (CREATE TRIGGER IF NOT EXISTS never replaces). Conformance tests cover both batch paths, the preserved-client-time invariant, and the v11→v12 upgrade. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): lifecycle hygiene — squash migrations, dead status, stale-row sweeps Migrations: fold the 20260621 corrective migration back into the base Downloads V2 migrations (its columns/constraints already exist there) and fix the reshape Down, which re-added the narrow status CHECK without collapsing managed-lifecycle rows first — rollback aborted on any DB with preparing/ready/revoked rows; validated against a live row. Branch databases that applied the corrective migration need its version row removed: DELETE FROM goose_db_version WHERE version_id = 20260621020459. Code: drop the dead 'registered' status (nothing ever wrote it; the lifecycle is preparing -> ready; 'revoked' stays reserved for the planned admin revoke flow) along with unused KindDirect and ErrInvalidFormat. Sweeps: Cleanup now runs an age-based hygiene pass independent of the byte budget — cold terminally-failed artifacts (with .part leftovers), orphaned ready artifacts no download row references, and ephemeral web rows older than their convenience-record lifetime (also unpinning their artifacts and bounding GET /downloads growth). The byte budget remains the disk quota per the limits & restrictions design. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(downloads): sync API doc with v2 fixes; HEAD on file route; Android handoff Document the contract changes from the review fixes: batch-manifest skipped[] shape, honest subscription 'registered' semantics, season 0 = Specials, always-array quality_presets, bytes_sent actual behavior, ephemeral 7-day retention, header-pairing requirement, progress-delta deletion caveat, and the ready/failed push event schema (new §9.4). Add an Android client handoff section (§11) mirroring the Apple one, register HEAD on /downloads/{id}/file for download stacks that probe before ranged GETs, and add season_number to the web create-request type. Flag the /direct-download session-token-in-URL tradeoff; a short-lived download-scoped URL is a follow-up. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor: consolidate download/progress helpers, prune dead code, gate sweeps Behavior-preserving consolidation from the Downloads V2 review: - appendVideoFilterArgs: one home for the burn-in/hwaccel -vf selection, shared by the HLS builder and the single-file prepare builder (the drift pattern that already bit tone-mapping once). - userstore.ResolveProgressState: one home for the min-resume/watched threshold rule, replacing five identical copies across both store backends and the offline-sync ingest. - Download file selection ranks resolutions via access.CompareQuality (adds 4320p, agrees with playback) instead of a private switch. - writeSubtitle uses the shared subtitles.SubtitleContentType mapping. - config.DefaultTranscodeDir replaces three '/tmp/silo-transcode' literals. - Read-side quality/revision defaulting helpers removed: insertArgs plus the NOT NULL/CHECK schema already guarantee the invariant. - Dead code removed: Repository.ListByUser, SubscriptionRepository. ListActiveBySeries, and the stale auto-register-worker comments (the design is client-pull; no worker exists). - Redundant left-prefix indexes dropped from the base migrations (their unique indexes serve the same prefixes). - recover()'s disk-presence sweep and the stale-row hygiene sweep run on startup then hourly instead of every 30s tick (both are O(cache size)). - gofmt/prettier fixes for pre-existing drift in handlers/playback.go and pages/Profiles.tsx. Deferred (noted for follow-ups): quality-ladder preset table collides with the drafted download limits & restrictions design, which specifies its own ladder helper; Download-literal construction consolidation and the managed-identity value object remain open. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(downloads): draft download limits & restrictions design Design input for the follow-up v1 capability proposal (quality ceiling, batch size cap, per-user quantity/bandwidth overrides). Committed with downloads v2 because the remediation work explicitly defers the quality ladder refactor and revocation wiring to this spec. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(progress): reject malformed updated_at; clamp negative progress inputs Review findings on #258: - A malformed (non-RFC3339) updated_at in POST /sync/progress previously parsed to the zero time, which clampEventAt treated as "now" — letting a stale offline event win LWW as a fresh server-time write. The item is now rejected with a per-item error instead. - ResolveProgressState now clamps negative position/duration before classification so no backend can persist negative progress through UpdateProgress/SetProgress. - The online-write event_at invariant test is table-driven over both SetProgress and UpdateProgress, which share the same contract. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(downloads): close review gaps — permission gates, file-access recheck, artifact-true manifests Review findings on #258: - UpdateSubscription now applies the same feature/DownloadAllowed gate as CreateSubscription and SyncSubscriptions; a PATCH could previously re-activate or widen a monitor and register managed rows after an admin disabled downloads or revoked the user. - Serving download bytes (managed and ephemeral) and /direct-download now mirror playback's per-file authorization via catalog.FileAllowedByAccess: library scope and the profile's max playback quality are re-checked at serve time, with artifact-backed rows checked against the artifact's resolution (a 720p transcode of a 4K source stays servable under a 1080p ceiling). - Offline manifests for remux/transcode entries now describe the prepared artifact (container, codecs, resolution, single selected audio track) instead of the catalog source file the client never receives. - ArtifactRepository.Requeue reports ErrNotFound when the row was concurrently swept; ArtifactManager.Ensure recreates the job in that case instead of linking downloads to a dead artifact id. - "No downloadable episodes" is a sentinel (mapped to 404 no_downloadable_episodes) rather than a bare error that surfaced as 500. - Subscription season_numbers are bounds-checked (0–9999) before the int32 narrowing in the repo could silently wrap them. - HandlePatchDownload reuses requireManaged instead of hand-rolling the same managed-identity checks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
716 lines
22 KiB
Go
716 lines
22 KiB
Go
// Package userdb provides per-user SQLite database management for Silo.
|
|
// Each user gets their own SQLite file storing profiles, watch progress,
|
|
// favorites, collections, playback sessions, and settings.
|
|
package userdb
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
// Schema is the full SQLite schema for per-user databases.
|
|
const Schema = `
|
|
CREATE TABLE IF NOT EXISTS profiles (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
avatar TEXT,
|
|
pin_hash TEXT,
|
|
is_child BOOLEAN DEFAULT false,
|
|
is_primary BOOLEAN NOT NULL DEFAULT false,
|
|
max_content_rating TEXT,
|
|
quality_preference TEXT DEFAULT '1080p',
|
|
language TEXT DEFAULT 'en',
|
|
subtitle_language TEXT,
|
|
subtitle_mode TEXT DEFAULT 'auto',
|
|
auto_skip_intro BOOLEAN DEFAULT false,
|
|
auto_skip_credits BOOLEAN DEFAULT false,
|
|
auto_skip_recap BOOLEAN DEFAULT false,
|
|
auto_play_next_preview BOOLEAN DEFAULT false,
|
|
show_forced_subtitles BOOLEAN NOT NULL DEFAULT true,
|
|
library_restrictions_enabled BOOLEAN DEFAULT false,
|
|
max_playback_quality TEXT DEFAULT '',
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS profile_allowed_libraries (
|
|
profile_id TEXT NOT NULL,
|
|
library_id INTEGER NOT NULL,
|
|
PRIMARY KEY (profile_id, library_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS watch_progress (
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
position_seconds REAL NOT NULL,
|
|
duration_seconds REAL NOT NULL,
|
|
completed BOOLEAN DEFAULT false,
|
|
updated_at TEXT NOT NULL,
|
|
event_at TEXT,
|
|
synced_seq INTEGER,
|
|
last_file_id INTEGER,
|
|
last_resolution TEXT,
|
|
last_hdr BOOLEAN,
|
|
last_codec_video TEXT,
|
|
last_edition_key TEXT,
|
|
PRIMARY KEY (profile_id, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS watch_history (
|
|
id TEXT PRIMARY KEY,
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
watched_at TEXT NOT NULL,
|
|
duration_seconds REAL,
|
|
completed BOOLEAN DEFAULT false,
|
|
source TEXT NOT NULL DEFAULT 'legacy',
|
|
watch_identity TEXT NOT NULL DEFAULT '{}'
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS hidden_history_items (
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
hidden_before TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS playback_sessions (
|
|
session_id TEXT PRIMARY KEY,
|
|
profile_id TEXT NOT NULL,
|
|
media_file_id INTEGER NOT NULL,
|
|
play_method TEXT NOT NULL,
|
|
position_seconds REAL DEFAULT 0,
|
|
is_paused BOOLEAN DEFAULT false,
|
|
started_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS favorites (
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
added_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS watchlist (
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
added_at TEXT NOT NULL,
|
|
sort_index INTEGER,
|
|
PRIMARY KEY (profile_id, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS home_item_dismissals (
|
|
profile_id TEXT NOT NULL,
|
|
surface TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
series_id TEXT,
|
|
progress_updated_at TEXT,
|
|
dismissed_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, surface, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS personal_collections (
|
|
id TEXT PRIMARY KEY,
|
|
profile_id TEXT NOT NULL,
|
|
creator_profile_id TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
collection_type TEXT NOT NULL DEFAULT 'manual',
|
|
is_shared BOOLEAN DEFAULT false,
|
|
query_definition TEXT NOT NULL DEFAULT '{}',
|
|
sort_config TEXT NOT NULL DEFAULT '{}',
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS personal_collection_items (
|
|
collection_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
position INTEGER,
|
|
added_at TEXT NOT NULL,
|
|
PRIMARY KEY (collection_id, media_item_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS personal_collection_profiles (
|
|
collection_id TEXT NOT NULL,
|
|
profile_id TEXT NOT NULL,
|
|
PRIMARY KEY (collection_id, profile_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS subtitle_preferences (
|
|
profile_id TEXT NOT NULL,
|
|
series_id TEXT NOT NULL,
|
|
subtitle_language TEXT,
|
|
subtitle_track_index INT,
|
|
external_subtitle_path TEXT,
|
|
subtitle_mode TEXT,
|
|
subtitle_track_signature TEXT NOT NULL DEFAULT '{}',
|
|
show_forced_subtitles BOOLEAN,
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, series_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS audio_preferences (
|
|
profile_id TEXT NOT NULL,
|
|
series_id TEXT NOT NULL,
|
|
audio_track_index INT,
|
|
audio_language TEXT,
|
|
audio_track_signature TEXT NOT NULL DEFAULT '{}',
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, series_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS series_playback_preferences (
|
|
profile_id TEXT NOT NULL,
|
|
series_id TEXT NOT NULL,
|
|
resolution TEXT,
|
|
hdr BOOLEAN NOT NULL DEFAULT false,
|
|
codec_video TEXT,
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, series_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS library_playback_preferences (
|
|
profile_id TEXT NOT NULL,
|
|
library_id INTEGER NOT NULL,
|
|
audio_language TEXT,
|
|
subtitle_language TEXT,
|
|
subtitle_mode TEXT,
|
|
show_forced_subtitles BOOLEAN,
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, library_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS user_settings (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS user_device_settings (
|
|
profile_id TEXT NOT NULL,
|
|
device_id TEXT NOT NULL,
|
|
key TEXT NOT NULL,
|
|
value TEXT NOT NULL,
|
|
device_name TEXT NOT NULL DEFAULT '',
|
|
device_platform TEXT NOT NULL DEFAULT '',
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, device_id, key)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS user_devices (
|
|
profile_id TEXT NOT NULL,
|
|
device_id TEXT NOT NULL,
|
|
device_name TEXT NOT NULL DEFAULT '',
|
|
device_platform TEXT NOT NULL DEFAULT '',
|
|
last_seen_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, device_id)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS downloads (
|
|
id TEXT PRIMARY KEY,
|
|
profile_id TEXT NOT NULL,
|
|
media_item_id TEXT NOT NULL,
|
|
media_file_id INTEGER NOT NULL,
|
|
quality TEXT,
|
|
transcoded BOOLEAN DEFAULT false,
|
|
file_size INTEGER,
|
|
expires_at TEXT,
|
|
downloaded_at TEXT
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS profile_section_overrides (
|
|
id TEXT PRIMARY KEY,
|
|
profile_id TEXT NOT NULL,
|
|
scope TEXT NOT NULL CHECK (scope IN ('home', 'library')),
|
|
library_id TEXT,
|
|
section_id TEXT,
|
|
position INTEGER,
|
|
hidden INTEGER NOT NULL DEFAULT 0,
|
|
removed INTEGER NOT NULL DEFAULT 0,
|
|
section_type TEXT,
|
|
title TEXT,
|
|
featured INTEGER,
|
|
item_limit INTEGER,
|
|
config TEXT,
|
|
is_user_added INTEGER NOT NULL DEFAULT 0,
|
|
user_section_type TEXT,
|
|
user_config TEXT,
|
|
user_title TEXT,
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_profile_section_overrides_lookup
|
|
ON profile_section_overrides(profile_id, scope, library_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_profile_allowed_libraries_lookup
|
|
ON profile_allowed_libraries(profile_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_personal_collection_profiles_lookup
|
|
ON personal_collection_profiles(profile_id, collection_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_home_item_dismissals_lookup
|
|
ON home_item_dismissals(profile_id, surface);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_hidden_history_items_lookup
|
|
ON hidden_history_items(profile_id, hidden_before);
|
|
`
|
|
|
|
// InitSchema creates all tables in the given SQLite database.
|
|
func InitSchema(db *sql.DB) error {
|
|
_, err := db.Exec(Schema)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := ensureProfileSectionOverridesRemovedColumn(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureProfileSectionOverridesUserAddedColumns(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureWatchHistorySourceColumn(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureShowForcedSubtitleColumns(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureProfileIsPrimaryColumn(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureDeviceSettingsProfileColumn(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureAutoSkipRecapPreviewColumns(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureWatchHistoryIdentityColumn(db); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureWatchProgressSyncColumns(db); err != nil {
|
|
return err
|
|
}
|
|
if err := migratePlaybackSettingsToDeviceScope(db); err != nil {
|
|
return err
|
|
}
|
|
return backfillUserDevices(db)
|
|
}
|
|
|
|
// watchProgressSyncTriggers stamps the server-owned cursor (synced_seq) on every
|
|
// watch_progress write and owns the LWW key: event_at defaults to the write
|
|
// time, and an UPDATE that changed updated_at without explicitly changing
|
|
// event_at advances it too (else a queued offline event older than that write
|
|
// would win SetProgressIfNewer's comparison and resurrect stale progress).
|
|
// Writes that DO set event_at — offline sync's clamped client event time —
|
|
// keep their value. With recursive_triggers OFF (the userdb default) the
|
|
// trigger's own UPDATE does not re-fire it. See the offline-sync design
|
|
// (invariant 1). CREATE TRIGGER IF NOT EXISTS never replaces an existing
|
|
// trigger, so changing a body requires a migrate.go step dropping the old one
|
|
// (see migrateToV13).
|
|
const watchProgressSyncTriggers = `
|
|
CREATE TRIGGER IF NOT EXISTS watch_progress_stamp_ins AFTER INSERT ON watch_progress
|
|
BEGIN
|
|
UPDATE watch_progress
|
|
SET synced_seq = (SELECT COALESCE(MAX(synced_seq), 0) + 1 FROM watch_progress),
|
|
event_at = COALESCE(event_at, updated_at)
|
|
WHERE rowid = NEW.rowid;
|
|
END;
|
|
CREATE TRIGGER IF NOT EXISTS watch_progress_stamp_upd AFTER UPDATE ON watch_progress
|
|
BEGIN
|
|
UPDATE watch_progress
|
|
SET synced_seq = (SELECT COALESCE(MAX(synced_seq), 0) + 1 FROM watch_progress),
|
|
event_at = CASE
|
|
WHEN NEW.event_at IS NULL THEN NEW.updated_at
|
|
WHEN NEW.event_at IS OLD.event_at AND NEW.updated_at IS NOT OLD.updated_at THEN NEW.updated_at
|
|
ELSE NEW.event_at
|
|
END
|
|
WHERE rowid = NEW.rowid;
|
|
END;
|
|
CREATE INDEX IF NOT EXISTS watch_progress_synced_idx ON watch_progress (profile_id, synced_seq);
|
|
`
|
|
|
|
// ensureWatchProgressSyncColumns adds the event_at (LWW key) and synced_seq
|
|
// (server cursor) columns, backfills them, and installs the stamping triggers.
|
|
// Idempotent: safe to run on every open for both fresh and existing databases.
|
|
func ensureWatchProgressSyncColumns(db *sql.DB) error {
|
|
columns := []struct{ name, definition string }{
|
|
{name: "event_at", definition: "TEXT"},
|
|
{name: "synced_seq", definition: "INTEGER"},
|
|
}
|
|
for _, column := range columns {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"watch_progress", column.name,
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking watch_progress.%s column: %w", column.name, err)
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if _, err := db.Exec(
|
|
fmt.Sprintf("ALTER TABLE watch_progress ADD COLUMN %s %s", column.name, column.definition),
|
|
); err != nil {
|
|
return fmt.Errorf("adding watch_progress.%s column: %w", column.name, err)
|
|
}
|
|
}
|
|
|
|
// Backfill before the triggers exist so these one-off writes don't trip them.
|
|
if _, err := db.Exec(`UPDATE watch_progress SET event_at = updated_at WHERE event_at IS NULL`); err != nil {
|
|
return fmt.Errorf("backfilling watch_progress.event_at: %w", err)
|
|
}
|
|
if _, err := db.Exec(`
|
|
UPDATE watch_progress SET synced_seq = (
|
|
SELECT COUNT(*) FROM watch_progress w2
|
|
WHERE w2.updated_at < watch_progress.updated_at
|
|
OR (w2.updated_at = watch_progress.updated_at AND w2.rowid <= watch_progress.rowid)
|
|
) WHERE synced_seq IS NULL`); err != nil {
|
|
return fmt.Errorf("backfilling watch_progress.synced_seq: %w", err)
|
|
}
|
|
|
|
if _, err := db.Exec(watchProgressSyncTriggers); err != nil {
|
|
return fmt.Errorf("installing watch_progress sync triggers: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureAutoSkipRecapPreviewColumns(db *sql.DB) error {
|
|
columns := []struct {
|
|
name string
|
|
definition string
|
|
}{
|
|
{name: "auto_skip_recap", definition: "BOOLEAN DEFAULT false"},
|
|
{name: "auto_play_next_preview", definition: "BOOLEAN DEFAULT false"},
|
|
}
|
|
|
|
for _, column := range columns {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"profiles",
|
|
column.name,
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking profiles.%s column: %w", column.name, err)
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if _, err := db.Exec(
|
|
fmt.Sprintf("ALTER TABLE profiles ADD COLUMN %s %s", column.name, column.definition),
|
|
); err != nil {
|
|
return fmt.Errorf("adding profiles.%s column: %w", column.name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureWatchHistoryIdentityColumn(db *sql.DB) error {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"watch_history",
|
|
"watch_identity",
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking watch_history.watch_identity column: %w", err)
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
if _, err := db.Exec("ALTER TABLE watch_history ADD COLUMN watch_identity TEXT NOT NULL DEFAULT '{}'"); err != nil {
|
|
return fmt.Errorf("adding watch_history.watch_identity: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureProfileIsPrimaryColumn(db *sql.DB) error {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"profiles",
|
|
"is_primary",
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking profiles.is_primary column: %w", err)
|
|
}
|
|
if count == 0 {
|
|
if _, err := db.Exec(
|
|
"ALTER TABLE profiles ADD COLUMN is_primary BOOLEAN NOT NULL DEFAULT false",
|
|
); err != nil {
|
|
return fmt.Errorf("adding profiles.is_primary column: %w", err)
|
|
}
|
|
}
|
|
// Backfill: the oldest existing profile (per-user sqlite file, so just the
|
|
// oldest row overall) is the primary. No-op if a primary already exists.
|
|
if _, err := db.Exec(`
|
|
UPDATE profiles
|
|
SET is_primary = 1
|
|
WHERE id = (
|
|
SELECT id FROM profiles
|
|
WHERE NOT EXISTS (SELECT 1 FROM profiles WHERE is_primary)
|
|
ORDER BY created_at ASC, id ASC
|
|
LIMIT 1
|
|
)
|
|
`); err != nil {
|
|
return fmt.Errorf("backfilling profiles.is_primary: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureDeviceSettingsProfileColumn(db *sql.DB) error {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"user_device_settings",
|
|
"profile_id",
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking user_device_settings.profile_id column: %w", err)
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return fmt.Errorf("beginning user_device_settings profile migration: %w", err)
|
|
}
|
|
defer tx.Rollback() //nolint:errcheck
|
|
|
|
if _, err := tx.Exec(`
|
|
CREATE TABLE user_device_settings_new (
|
|
profile_id TEXT NOT NULL,
|
|
device_id TEXT NOT NULL,
|
|
key TEXT NOT NULL,
|
|
value TEXT NOT NULL,
|
|
device_name TEXT NOT NULL DEFAULT '',
|
|
device_platform TEXT NOT NULL DEFAULT '',
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (profile_id, device_id, key)
|
|
)
|
|
`); err != nil {
|
|
return fmt.Errorf("creating user_device_settings_new: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO profiles (id, name, is_primary, created_at, updated_at)
|
|
SELECT 'default', 'Default', 1, datetime('now'), datetime('now')
|
|
WHERE NOT EXISTS (SELECT 1 FROM profiles)
|
|
`); err != nil {
|
|
return fmt.Errorf("ensuring default profile for user_device_settings migration: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO user_device_settings_new (
|
|
profile_id, device_id, key, value, device_name, device_platform, updated_at
|
|
)
|
|
SELECT p.id, uds.device_id, uds.key, uds.value, uds.device_name, uds.device_platform, uds.updated_at
|
|
FROM user_device_settings uds
|
|
JOIN (
|
|
SELECT id
|
|
FROM profiles
|
|
ORDER BY is_primary DESC, created_at ASC, id ASC
|
|
LIMIT 1
|
|
) p ON 1 = 1
|
|
`); err != nil {
|
|
return fmt.Errorf("backfilling profile-aware user_device_settings: %w", err)
|
|
}
|
|
|
|
if _, err := tx.Exec(`DROP TABLE user_device_settings`); err != nil {
|
|
return fmt.Errorf("dropping legacy user_device_settings: %w", err)
|
|
}
|
|
if _, err := tx.Exec(`ALTER TABLE user_device_settings_new RENAME TO user_device_settings`); err != nil {
|
|
return fmt.Errorf("renaming profile-aware user_device_settings: %w", err)
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
func backfillUserDevices(db *sql.DB) error {
|
|
_, err := db.Exec(`
|
|
INSERT INTO user_devices (
|
|
profile_id, device_id, device_name, device_platform, last_seen_at
|
|
)
|
|
SELECT
|
|
profile_id,
|
|
device_id,
|
|
COALESCE(MAX(device_name), ''),
|
|
COALESCE(MAX(device_platform), ''),
|
|
MAX(updated_at)
|
|
FROM user_device_settings
|
|
WHERE TRIM(device_id) <> ''
|
|
GROUP BY profile_id, device_id
|
|
ON CONFLICT(profile_id, device_id) DO UPDATE SET
|
|
device_name = CASE
|
|
WHEN excluded.device_name <> '' THEN excluded.device_name
|
|
ELSE user_devices.device_name
|
|
END,
|
|
device_platform = CASE
|
|
WHEN excluded.device_platform <> '' THEN excluded.device_platform
|
|
ELSE user_devices.device_platform
|
|
END,
|
|
last_seen_at = CASE
|
|
WHEN excluded.last_seen_at > user_devices.last_seen_at THEN excluded.last_seen_at
|
|
ELSE user_devices.last_seen_at
|
|
END
|
|
`)
|
|
if err != nil {
|
|
return fmt.Errorf("backfilling user_devices: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureProfileSectionOverridesRemovedColumn(db *sql.DB) error {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"profile_section_overrides",
|
|
"removed",
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking profile_section_overrides.removed column: %w", err)
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
if _, err := db.Exec("ALTER TABLE profile_section_overrides ADD COLUMN removed INTEGER NOT NULL DEFAULT 0"); err != nil {
|
|
return fmt.Errorf("adding profile_section_overrides.removed: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ensureProfileSectionOverridesUserAddedColumns adds the four columns that back
|
|
// user-added (profile-built) sections so SaveSectionOverrides can round-trip
|
|
// IsUserAdded / UserSectionType / UserConfig / UserTitle. Without these columns
|
|
// those fields are silently dropped on save and lost on subsequent reads.
|
|
func ensureProfileSectionOverridesUserAddedColumns(db *sql.DB) error {
|
|
columns := []struct {
|
|
name string
|
|
definition string
|
|
}{
|
|
{name: "is_user_added", definition: "INTEGER NOT NULL DEFAULT 0"},
|
|
{name: "user_section_type", definition: "TEXT"},
|
|
{name: "user_config", definition: "TEXT"},
|
|
{name: "user_title", definition: "TEXT"},
|
|
}
|
|
for _, c := range columns {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"profile_section_overrides",
|
|
c.name,
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking profile_section_overrides.%s column: %w", c.name, err)
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if _, err := db.Exec(fmt.Sprintf(
|
|
"ALTER TABLE profile_section_overrides ADD COLUMN %s %s", c.name, c.definition,
|
|
)); err != nil {
|
|
return fmt.Errorf("adding profile_section_overrides.%s: %w", c.name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureWatchHistorySourceColumn(db *sql.DB) error {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
"watch_history",
|
|
"source",
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking watch_history.source column: %w", err)
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
if _, err := db.Exec("ALTER TABLE watch_history ADD COLUMN source TEXT NOT NULL DEFAULT 'legacy'"); err != nil {
|
|
return fmt.Errorf("adding watch_history.source: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureShowForcedSubtitleColumns(db *sql.DB) error {
|
|
columns := []struct {
|
|
table string
|
|
column string
|
|
definition string
|
|
}{
|
|
{table: "profiles", column: "show_forced_subtitles", definition: "BOOLEAN NOT NULL DEFAULT true"},
|
|
{table: "library_playback_preferences", column: "show_forced_subtitles", definition: "BOOLEAN"},
|
|
{table: "subtitle_preferences", column: "show_forced_subtitles", definition: "BOOLEAN"},
|
|
}
|
|
|
|
for _, column := range columns {
|
|
var count int
|
|
if err := db.QueryRow(
|
|
"SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?",
|
|
column.table,
|
|
column.column,
|
|
).Scan(&count); err != nil {
|
|
return fmt.Errorf("checking %s.%s column: %w", column.table, column.column, err)
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if _, err := db.Exec(
|
|
fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", column.table, column.column, column.definition),
|
|
); err != nil {
|
|
return fmt.Errorf("adding %s.%s column: %w", column.table, column.column, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func migratePlaybackSettingsToDeviceScope(db *sql.DB) error {
|
|
version, err := userVersion(db)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if version == 0 || version >= 9 {
|
|
return nil
|
|
}
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return fmt.Errorf("beginning playback settings device-scope migration: %w", err)
|
|
}
|
|
defer tx.Rollback() //nolint:errcheck
|
|
|
|
if _, err := tx.Exec(`
|
|
INSERT OR IGNORE INTO user_device_settings (
|
|
profile_id, device_id, key, value, device_name, device_platform, updated_at
|
|
)
|
|
SELECT
|
|
devices.profile_id,
|
|
devices.device_id,
|
|
settings.key,
|
|
settings.value,
|
|
devices.device_name,
|
|
devices.device_platform,
|
|
devices.updated_at
|
|
FROM user_settings AS settings
|
|
JOIN (
|
|
SELECT DISTINCT profile_id, device_id, device_name, device_platform, updated_at
|
|
FROM user_device_settings
|
|
) AS devices ON 1 = 1
|
|
WHERE settings.key IN (
|
|
'playback.preferred_quality',
|
|
'playback.audio_language',
|
|
'playback.auto_skip_intro',
|
|
'playback.auto_skip_credits',
|
|
'playback.auto_play_next'
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM user_device_settings AS existing
|
|
WHERE existing.profile_id = devices.profile_id
|
|
AND existing.device_id = devices.device_id
|
|
AND existing.key = settings.key
|
|
)
|
|
`); err != nil {
|
|
return fmt.Errorf("backfilling playback settings into user_device_settings: %w", err)
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|