-- The whole schema at version 106, and the settings rows it starts with. -- Earlier versions were folded into this one; a database already at 106 -- skips it, and an older one restores from a backup instead. CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public; CREATE TYPE public.quarantine_action_kind AS ENUM ( 'resolved', 'deleted_file', 'deleted_album', 'removed_file' ); CREATE TYPE public.quarantine_reason AS ENUM ( 'bad_rip', 'wrong_file', 'wrong_tags', 'duplicate', 'other' ); CREATE TABLE public.acoustid_settings ( id boolean DEFAULT true NOT NULL, enabled boolean DEFAULT false NOT NULL, api_key text, min_score real DEFAULT 0.85 NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT acoustid_settings_min_score_range CHECK (((min_score >= (0.5)::double precision) AND (min_score <= (1)::double precision))), CONSTRAINT acoustid_settings_singleton CHECK ((id = true)) ); CREATE TABLE public.album_loudness ( album_id uuid NOT NULL, integrated_lufs real, true_peak_dbtp real, tracks_total integer NOT NULL, tracks_settled integer NOT NULL, inputs_digest text NOT NULL, computed_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.albums ( id uuid DEFAULT gen_random_uuid() NOT NULL, title text NOT NULL, sort_title text NOT NULL, artist_id uuid NOT NULL, release_date date, mbid text, cover_art_path text, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, cover_art_source text, cover_art_sources_version integer DEFAULT 0 NOT NULL, release_group_mbid text, announced_at timestamp with time zone, CONSTRAINT albums_cover_art_source_check CHECK (((cover_art_source IS NULL) OR (cover_art_source <> ''::text))) ); CREATE TABLE public.artist_similarity ( artist_a_id uuid NOT NULL, artist_b_id uuid NOT NULL, score double precision NOT NULL, source text NOT NULL, fetched_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT artist_similarity_check CHECK ((artist_a_id <> artist_b_id)), CONSTRAINT artist_similarity_source_check CHECK ((source = ANY (ARRAY['listenbrainz'::text, 'musicbrainz_tag'::text, 'user_cooccurrence'::text]))) ); CREATE TABLE public.artist_similarity_fetches ( artist_id uuid NOT NULL, fetched_at timestamp with time zone DEFAULT now() NOT NULL, returned integer NOT NULL ); CREATE TABLE public.artists ( id uuid DEFAULT gen_random_uuid() NOT NULL, name text NOT NULL, sort_name text NOT NULL, mbid text, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, artist_thumb_path text, artist_fanart_path text, artist_art_source text, artist_art_sources_version integer DEFAULT 0 NOT NULL, CONSTRAINT artists_artist_art_source_check CHECK (((artist_art_source IS NULL) OR (artist_art_source <> ''::text))) ); CREATE TABLE public.audit_log ( id uuid DEFAULT gen_random_uuid() NOT NULL, actor_id uuid, target_id uuid, action text NOT NULL, metadata jsonb, created_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.backup_music_files ( track_id uuid NOT NULL, size bigint NOT NULL, mtime timestamp with time zone NOT NULL, location text NOT NULL, checked_at timestamp with time zone DEFAULT now() NOT NULL, error text DEFAULT ''::text NOT NULL ); CREATE TABLE public.backup_runs ( id uuid DEFAULT gen_random_uuid() NOT NULL, trigger text NOT NULL, status text DEFAULT 'running'::text NOT NULL, started_at timestamp with time zone DEFAULT now() NOT NULL, finished_at timestamp with time zone, location text DEFAULT ''::text NOT NULL, bytes bigint DEFAULT 0 NOT NULL, error text DEFAULT ''::text NOT NULL, music_files integer DEFAULT 0 NOT NULL, music_bytes bigint DEFAULT 0 NOT NULL, music_note text DEFAULT ''::text NOT NULL, CONSTRAINT backup_runs_status_check CHECK ((status = ANY (ARRAY['running'::text, 'succeeded'::text, 'failed'::text]))), CONSTRAINT backup_runs_trigger_check CHECK ((trigger = ANY (ARRAY['schedule'::text, 'manual'::text]))) ); CREATE TABLE public.backup_settings ( id boolean DEFAULT true NOT NULL, enabled boolean DEFAULT false NOT NULL, destination text DEFAULT 's3'::text NOT NULL, folder text DEFAULT ''::text NOT NULL, endpoint text DEFAULT ''::text NOT NULL, bucket text DEFAULT ''::text NOT NULL, access_key text DEFAULT ''::text NOT NULL, secret_key text DEFAULT ''::text NOT NULL, run_hour smallint DEFAULT 3 NOT NULL, upload_limit_kib integer DEFAULT 4096 NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, include_music boolean DEFAULT false NOT NULL, CONSTRAINT backup_settings_destination_check CHECK ((destination = ANY (ARRAY['folder'::text, 's3'::text]))), CONSTRAINT backup_settings_one_row CHECK (id), CONSTRAINT backup_settings_run_hour_check CHECK (((run_hour >= 0) AND (run_hour <= 23))), CONSTRAINT backup_settings_upload_limit_check CHECK ((upload_limit_kib >= 0)) ); CREATE TABLE public.contextual_likes ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, track_id uuid NOT NULL, liked_at timestamp with time zone DEFAULT now() NOT NULL, deleted_at timestamp with time zone, session_vector jsonb, session_id uuid ); CREATE TABLE public.cover_art_provider_settings ( provider_id text NOT NULL, enabled boolean DEFAULT true NOT NULL, api_key text, display_order integer DEFAULT 0 NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.cover_art_sources_meta ( id boolean DEFAULT true NOT NULL, current_version integer DEFAULT 1 NOT NULL, last_registered_providers_hash text DEFAULT ''::text NOT NULL, CONSTRAINT cover_art_sources_meta_singleton CHECK ((id = true)) ); CREATE TABLE public.diagnostic_events ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, client_id text NOT NULL, app_version text, os_version text, kind text NOT NULL, payload jsonb DEFAULT '{}'::jsonb NOT NULL, occurred_at timestamp with time zone NOT NULL, received_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT diagnostic_events_kind_check CHECK ((kind = ANY (ARRAY['connectivity'::text, 'upnp_sync'::text, 'power'::text, 'lifecycle'::text, 'heartbeat'::text, 'http'::text, 'playback'::text]))) ); CREATE TABLE public.duplicate_group_members ( group_id uuid NOT NULL, track_id uuid NOT NULL ); CREATE TABLE public.duplicate_groups ( id uuid DEFAULT gen_random_uuid() NOT NULL, member_key text NOT NULL, tier text NOT NULL, worst_bit_error_rate real, status text DEFAULT 'pending'::text NOT NULL, detected_at timestamp with time zone DEFAULT now() NOT NULL, last_seen_sweep_id uuid, resolved_at timestamp with time zone, class text, resolve_note text, resolved_automatically boolean DEFAULT false NOT NULL, CONSTRAINT duplicate_groups_class_check CHECK ((class = ANY (ARRAY['same_release'::text, 'cross_release'::text, 'mismatch'::text, 'distinct'::text, 'review'::text]))), CONSTRAINT duplicate_groups_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'dismissed'::text, 'merged'::text]))), CONSTRAINT duplicate_groups_tier_check CHECK ((tier = ANY (ARRAY['exact'::text, 'acoustic'::text]))) ); CREATE TABLE public.duplicate_sweeps ( id uuid DEFAULT gen_random_uuid() NOT NULL, started_at timestamp with time zone DEFAULT now() NOT NULL, finished_at timestamp with time zone, candidates integer, groups_found integer, oversize_clusters integer, error_message text ); CREATE TABLE public.fingerprint_settings ( id boolean DEFAULT true NOT NULL, enabled boolean DEFAULT true NOT NULL, chromaprint_length_sec integer DEFAULT 120 NOT NULL, acoustic_max_bit_error_rate double precision DEFAULT 0.15 NOT NULL, backfill_concurrency integer DEFAULT 2 NOT NULL, sweep_interval_hours integer DEFAULT 1 NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, auto_resolve boolean DEFAULT true NOT NULL, CONSTRAINT fingerprint_settings_concurrency_range CHECK (((backfill_concurrency >= 1) AND (backfill_concurrency <= 8))), CONSTRAINT fingerprint_settings_length_range CHECK (((chromaprint_length_sec >= 30) AND (chromaprint_length_sec <= 600))), CONSTRAINT fingerprint_settings_singleton CHECK ((id = true)), CONSTRAINT fingerprint_settings_sweep_interval_range CHECK (((sweep_interval_hours >= 1) AND (sweep_interval_hours <= 168))), CONSTRAINT fingerprint_settings_threshold_range CHECK (((acoustic_max_bit_error_rate >= (0.01)::double precision) AND (acoustic_max_bit_error_rate <= (0.35)::double precision))) ); CREATE TABLE public.general_likes ( user_id uuid NOT NULL, track_id uuid NOT NULL, liked_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.general_likes_albums ( user_id uuid NOT NULL, album_id uuid NOT NULL, liked_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.general_likes_artists ( user_id uuid NOT NULL, artist_id uuid NOT NULL, liked_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.library_changes ( id bigint NOT NULL, entity_type text NOT NULL, entity_id text NOT NULL, op text NOT NULL, changed_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT library_changes_entity_type_check CHECK ((entity_type = ANY (ARRAY['artist'::text, 'album'::text, 'track'::text, 'like_track'::text, 'like_album'::text, 'like_artist'::text, 'playlist'::text, 'playlist_track'::text]))), CONSTRAINT library_changes_op_check CHECK ((op = ANY (ARRAY['upsert'::text, 'delete'::text]))) ); CREATE SEQUENCE public.library_changes_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER SEQUENCE public.library_changes_id_seq OWNED BY public.library_changes.id; CREATE TABLE public.library_changes_trimmed ( id boolean DEFAULT true NOT NULL, through bigint DEFAULT 0 NOT NULL, CONSTRAINT library_changes_trimmed_one_row CHECK (id) ); CREATE TABLE public.library_settings ( id boolean DEFAULT true NOT NULL, scan_paths text[], updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT library_settings_singleton CHECK ((id = true)) ); CREATE TABLE public.listenbrainz_similar_recordings ( seed_track_id uuid NOT NULL, recording_mbid text NOT NULL, score double precision NOT NULL ); CREATE TABLE public.loudness_settings ( id boolean DEFAULT true NOT NULL, enabled boolean DEFAULT true NOT NULL, backfill_concurrency integer DEFAULT 2 NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, leveled_cache_mb integer DEFAULT 2048 NOT NULL, CONSTRAINT loudness_settings_concurrency_range CHECK (((backfill_concurrency >= 1) AND (backfill_concurrency <= 8))), CONSTRAINT loudness_settings_leveled_cache_range CHECK (((leveled_cache_mb >= 256) AND (leveled_cache_mb <= 65536))), CONSTRAINT loudness_settings_singleton CHECK ((id = true)) ); CREATE TABLE public.mb_release_groups ( mbid text NOT NULL, title text NOT NULL, primary_type text DEFAULT ''::text NOT NULL, secondary_types text[] DEFAULT '{}'::text[] NOT NULL, first_release_date text DEFAULT ''::text NOT NULL, artist_credit text DEFAULT ''::text NOT NULL, releases_refreshed_at timestamp with time zone, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.mb_release_tracks ( release_mbid text NOT NULL, medium_position integer NOT NULL, "position" integer NOT NULL, medium_format text DEFAULT ''::text NOT NULL, number text DEFAULT ''::text NOT NULL, title text NOT NULL, length_ms integer DEFAULT 0 NOT NULL, recording_mbid text DEFAULT ''::text NOT NULL ); CREATE TABLE public.mb_releases ( mbid text NOT NULL, release_group_mbid text NOT NULL, title text NOT NULL, status text DEFAULT ''::text NOT NULL, release_date text DEFAULT ''::text NOT NULL, country text DEFAULT ''::text NOT NULL, disambiguation text DEFAULT ''::text NOT NULL, formats text DEFAULT ''::text NOT NULL, track_count integer DEFAULT 0 NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.network_settings ( id boolean DEFAULT true NOT NULL, trusted_proxy_hops integer DEFAULT 1 NOT NULL, public_url text DEFAULT ''::text NOT NULL, CONSTRAINT network_settings_hops_range CHECK (((trusted_proxy_hops >= 0) AND (trusted_proxy_hops <= 10))), CONSTRAINT network_settings_singleton CHECK ((id = true)) ); CREATE TABLE public.notification_email_settings ( id boolean DEFAULT true NOT NULL, summary_hour integer DEFAULT 9 NOT NULL, batch_window_minutes integer DEFAULT 60 NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT notification_email_settings_hour_range CHECK (((summary_hour >= 0) AND (summary_hour <= 23))), CONSTRAINT notification_email_settings_singleton CHECK ((id = true)), CONSTRAINT notification_email_settings_window_range CHECK (((batch_window_minutes >= 15) AND (batch_window_minutes <= 1440))) ); CREATE TABLE public.password_resets ( token text NOT NULL, user_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, expires_at timestamp with time zone NOT NULL, used_at timestamp with time zone ); CREATE TABLE public.play_events ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, track_id uuid NOT NULL, session_id uuid NOT NULL, started_at timestamp with time zone NOT NULL, ended_at timestamp with time zone, duration_played_ms integer, completion_ratio double precision, was_skipped boolean DEFAULT false NOT NULL, client_id text, session_vector_at_play jsonb, scrobbled_at timestamp with time zone, source text, pick_kind text, device_class text, CONSTRAINT play_events_pick_kind_check CHECK ((pick_kind = ANY (ARRAY['taste'::text, 'fresh'::text, 'dormant'::text, 'cross_user'::text, 'random'::text, 'taste_unheard'::text, 'tier1'::text, 'tier2'::text, 'tier3'::text]))) ); CREATE TABLE public.play_sessions ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, started_at timestamp with time zone NOT NULL, ended_at timestamp with time zone, last_event_at timestamp with time zone NOT NULL, track_count integer DEFAULT 0 NOT NULL, client_id text ); CREATE TABLE public.playback_errors ( id uuid DEFAULT gen_random_uuid() NOT NULL, track_id uuid NOT NULL, user_id uuid NOT NULL, client_id text NOT NULL, kind text NOT NULL, detail text, occurred_at timestamp with time zone DEFAULT now() NOT NULL, resolved_at timestamp with time zone, resolved_by uuid, resolution text, CONSTRAINT playback_errors_kind_check CHECK ((kind = ANY (ARRAY['zero_duration'::text, 'load_failed'::text, 'stalled'::text]))), CONSTRAINT playback_errors_resolution_check CHECK (((resolution IS NULL) OR (resolution = ANY (ARRAY['hidden'::text, 'deleted'::text, 'fixed'::text, 'ignored'::text])))), CONSTRAINT playback_errors_resolved_consistency CHECK (((resolved_at IS NULL) = (resolution IS NULL))) ); CREATE TABLE public.playlist_tracks ( playlist_id uuid NOT NULL, "position" integer NOT NULL, track_id uuid, title text NOT NULL, artist_name text NOT NULL, album_title text NOT NULL, duration_sec integer NOT NULL, added_at timestamp with time zone DEFAULT now() NOT NULL, pick_kind text, CONSTRAINT playlist_tracks_pick_kind_check CHECK ((pick_kind = ANY (ARRAY['taste'::text, 'fresh'::text, 'dormant'::text, 'cross_user'::text, 'random'::text, 'taste_unheard'::text, 'tier1'::text, 'tier2'::text, 'tier3'::text]))) ); CREATE TABLE public.playlists ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, name text NOT NULL, description text DEFAULT ''::text NOT NULL, is_public boolean DEFAULT false NOT NULL, cover_path text, track_count integer DEFAULT 0 NOT NULL, duration_sec integer DEFAULT 0 NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, kind text DEFAULT 'user'::text NOT NULL, system_variant text, seed_artist_id uuid, CONSTRAINT playlists_kind_variant_consistent CHECK ((((kind = 'user'::text) AND (system_variant IS NULL)) OR ((kind = 'system'::text) AND (system_variant = ANY (ARRAY['for_you'::text, 'songs_like_artist'::text, 'discover'::text, 'deep_cuts'::text, 'rediscover'::text, 'new_for_you'::text, 'on_this_day'::text, 'first_listens'::text, 'genre_mix'::text]))))), CONSTRAINT playlists_seed_consistent CHECK ((((system_variant IS NULL) AND (seed_artist_id IS NULL)) OR ((system_variant = 'for_you'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'discover'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'deep_cuts'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'rediscover'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'new_for_you'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'on_this_day'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'first_listens'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'genre_mix'::text) AND (seed_artist_id IS NULL)) OR ((system_variant = 'songs_like_artist'::text) AND (seed_artist_id IS NOT NULL)))) ); CREATE TABLE public.quarantine ( user_id uuid NOT NULL, track_id uuid NOT NULL, reason public.quarantine_reason NOT NULL, notes text, created_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.quarantine_actions ( id uuid DEFAULT gen_random_uuid() NOT NULL, track_id uuid NOT NULL, track_title text NOT NULL, artist_name text NOT NULL, album_title text, action public.quarantine_action_kind NOT NULL, admin_id uuid, album_mbid text, affected_users integer NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.recommendation_tuning_audit ( id bigint NOT NULL, changed_at timestamp with time zone DEFAULT now() NOT NULL, scope text NOT NULL, action text NOT NULL, changes jsonb NOT NULL, CONSTRAINT recommendation_tuning_audit_action_check CHECK ((action = ANY (ARRAY['update'::text, 'reset'::text]))), CONSTRAINT recommendation_tuning_audit_scope_check CHECK ((scope = ANY (ARRAY['radio'::text, 'daily_mix'::text, 'taste'::text, 'discover'::text, 'songs_like'::text]))) ); CREATE SEQUENCE public.recommendation_tuning_audit_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER SEQUENCE public.recommendation_tuning_audit_id_seq OWNED BY public.recommendation_tuning_audit.id; CREATE TABLE public.recommendation_weight_profiles ( profile text NOT NULL, base_weight double precision NOT NULL, like_boost double precision NOT NULL, recency_weight double precision NOT NULL, skip_penalty double precision NOT NULL, jitter_magnitude double precision NOT NULL, context_weight double precision NOT NULL, similarity_weight double precision NOT NULL, taste_weight double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, context_time_weight double precision DEFAULT 1.0 NOT NULL, CONSTRAINT recommendation_weight_profiles_profile_check CHECK ((profile = ANY (ARRAY['radio'::text, 'daily_mix'::text, 'songs_like'::text]))) ); CREATE TABLE public.registration_settings ( id boolean DEFAULT true NOT NULL, mode text DEFAULT 'invite_only'::text NOT NULL, CONSTRAINT registration_settings_mode_check CHECK ((mode = ANY (ARRAY['invite_only'::text, 'open'::text]))), CONSTRAINT registration_settings_singleton CHECK ((id = true)) ); CREATE TABLE public.scan_runs ( id uuid DEFAULT gen_random_uuid() NOT NULL, started_at timestamp with time zone DEFAULT now() NOT NULL, finished_at timestamp with time zone, library jsonb, mbid_backfill jsonb, cover_enrich jsonb, error_message text, artist_art_enrich jsonb ); CREATE TABLE public.scrobble_queue ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, play_event_id uuid NOT NULL, status text NOT NULL, attempts integer DEFAULT 0 NOT NULL, next_attempt_at timestamp with time zone DEFAULT now() NOT NULL, last_error text, enqueued_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT scrobble_queue_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'failed'::text]))) ); CREATE TABLE public.sessions ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, token_hash bytea NOT NULL, user_agent text DEFAULT ''::text NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, last_seen_at timestamp with time zone DEFAULT now() NOT NULL, created_ip text DEFAULT ''::text NOT NULL, last_ip text DEFAULT ''::text NOT NULL ); CREATE TABLE public.skip_events ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, track_id uuid NOT NULL, session_id uuid NOT NULL, skipped_at timestamp with time zone NOT NULL, position_ms integer NOT NULL ); CREATE TABLE public.smtp_config ( id boolean DEFAULT true NOT NULL, enabled boolean DEFAULT false NOT NULL, host text DEFAULT ''::text NOT NULL, port integer DEFAULT 587 NOT NULL, username text DEFAULT ''::text NOT NULL, password text DEFAULT ''::text NOT NULL, from_address text DEFAULT ''::text NOT NULL, from_name text DEFAULT 'Minstrel'::text NOT NULL, use_tls boolean DEFAULT true NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT smtp_config_singleton CHECK ((id = true)) ); CREATE TABLE public.system_playlist_rotation_state ( user_id uuid NOT NULL, playlist_kind text NOT NULL, played_track_ids uuid[] DEFAULT '{}'::uuid[] NOT NULL, rotation_started_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.system_playlist_runs ( user_id uuid NOT NULL, last_run_at timestamp with time zone, last_run_date date, in_flight boolean DEFAULT false NOT NULL, last_error text ); CREATE TABLE public.tag_provider_settings ( provider_id text NOT NULL, enabled boolean DEFAULT true NOT NULL, api_key text, display_order integer DEFAULT 0 NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.tag_sources_meta ( id boolean DEFAULT true NOT NULL, current_version integer DEFAULT 1 NOT NULL, last_registered_providers_hash text DEFAULT ''::text NOT NULL, CONSTRAINT tag_sources_meta_singleton CHECK ((id = true)) ); CREATE TABLE public.taste_profile_artists ( user_id uuid NOT NULL, artist_id uuid NOT NULL, weight double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.taste_profile_eras ( user_id uuid NOT NULL, era text NOT NULL, weight double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.taste_profile_moods ( user_id uuid NOT NULL, mood text NOT NULL, weight double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.taste_profile_tags ( user_id uuid NOT NULL, tag text NOT NULL, weight double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.taste_tuning ( singleton boolean DEFAULT true NOT NULL, half_life_days double precision NOT NULL, engagement_hard_skip double precision NOT NULL, engagement_neutral double precision NOT NULL, engagement_full double precision NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, enriched_tag_scale double precision DEFAULT 0.5 NOT NULL, era_scale double precision DEFAULT 0.5 NOT NULL, mood_scale double precision DEFAULT 0.5 NOT NULL, CONSTRAINT taste_tuning_singleton_check CHECK (singleton) ); CREATE TABLE public.track_acoustid_lookups ( track_id uuid NOT NULL, state text NOT NULL, best_score real, candidates integer DEFAULT 0 NOT NULL, recording_mbid text, detail text, looked_up_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT track_acoustid_lookups_matched_has_mbid CHECK (((state = 'matched'::text) = (recording_mbid IS NOT NULL))), CONSTRAINT track_acoustid_lookups_state_check CHECK ((state = ANY (ARRAY['matched'::text, 'ambiguous'::text, 'no_match'::text, 'failed'::text]))) ); CREATE TABLE public.track_fingerprints ( track_id uuid NOT NULL, audio_stream_sha256 bytea, chromaprint integer[], fingerprint_version smallint NOT NULL, computed_at timestamp with time zone DEFAULT now() NOT NULL, chromaprint_length_sec integer DEFAULT 120 NOT NULL, CONSTRAINT track_fingerprints_audio_stream_sha256_check CHECK (((audio_stream_sha256 IS NULL) OR (octet_length(audio_stream_sha256) = 32))) ); CREATE TABLE public.track_loudness ( track_id uuid NOT NULL, integrated_lufs real, true_peak_dbtp real, loudness_range_lu real, block_hist_start smallint, block_hist integer[], unreadable boolean DEFAULT false NOT NULL, analysis_version smallint NOT NULL, analyzed_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT track_loudness_hist_pair CHECK (((block_hist IS NULL) = (block_hist_start IS NULL))) ); CREATE TABLE public.track_similarity ( track_a_id uuid NOT NULL, track_b_id uuid NOT NULL, score double precision NOT NULL, source text NOT NULL, fetched_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT track_similarity_check CHECK ((track_a_id <> track_b_id)), CONSTRAINT track_similarity_source_check CHECK ((source = ANY (ARRAY['listenbrainz'::text, 'musicbrainz_tag'::text, 'user_cooccurrence'::text]))) ); CREATE TABLE public.track_similarity_fetches ( track_id uuid NOT NULL, fetched_at timestamp with time zone DEFAULT now() NOT NULL, returned integer NOT NULL ); CREATE TABLE public.track_tags ( track_id uuid NOT NULL, tag text NOT NULL, weight double precision DEFAULT 1 NOT NULL ); CREATE TABLE public.tracks ( id uuid DEFAULT gen_random_uuid() NOT NULL, title text NOT NULL, album_id uuid NOT NULL, artist_id uuid NOT NULL, track_number integer, disc_number integer, duration_ms integer NOT NULL, file_path text NOT NULL, file_size bigint NOT NULL, file_format text NOT NULL, bitrate integer, mbid text, genre text, added_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, tag_source text, tag_sources_version integer DEFAULT 0 NOT NULL, tag_read_version smallint DEFAULT 0 NOT NULL, missing_since timestamp with time zone, mbid_source text, song_id uuid, song_key uuid GENERATED ALWAYS AS (COALESCE(song_id, id)) STORED, source_verdict text, source_verdict_at timestamp with time zone, emptied_at timestamp with time zone, removed_reason text, removed_by uuid, CONSTRAINT tracks_mbid_source_check CHECK ((mbid_source = ANY (ARRAY['tag'::text, 'acoustid'::text]))), CONSTRAINT tracks_mbid_source_pair CHECK (((mbid IS NULL) = (mbid_source IS NULL))), CONSTRAINT tracks_removed_only_missing CHECK (((missing_since IS NOT NULL) OR ((removed_reason IS NULL) AND (removed_by IS NULL)))), CONSTRAINT tracks_removed_reason_check CHECK ((removed_reason = ANY (ARRAY['report'::text, 'duplicate'::text, 'suspect'::text]))), CONSTRAINT tracks_source_verdict_check CHECK ((source_verdict = ANY (ARRAY['suspect'::text, 'fine'::text]))) ); CREATE TABLE public.user_invites ( token text NOT NULL, invited_by uuid NOT NULL, note text, created_at timestamp with time zone DEFAULT now() NOT NULL, expires_at timestamp with time zone NOT NULL, redeemed_at timestamp with time zone, redeemed_by uuid ); CREATE TABLE public.user_normalization_prefs ( user_id uuid NOT NULL, mode text DEFAULT 'auto'::text NOT NULL, target_lufs smallint DEFAULT '-18'::integer NOT NULL, boost text DEFAULT 'headroom'::text NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT user_normalization_prefs_boost_check CHECK ((boost = ANY (ARRAY['headroom'::text, 'limiter'::text]))), CONSTRAINT user_normalization_prefs_mode_check CHECK ((mode = ANY (ARRAY['off'::text, 'auto'::text, 'track'::text, 'album'::text]))), CONSTRAINT user_normalization_prefs_target_lufs_check CHECK ((target_lufs = ANY (ARRAY['-18'::integer, '-16'::integer, '-14'::integer]))) ); CREATE TABLE public.user_notification_email_state ( user_id uuid NOT NULL, email_group text NOT NULL, batch_opened_at timestamp with time zone, last_sent_at timestamp with time zone, failures integer DEFAULT 0 NOT NULL, retry_after timestamp with time zone, CONSTRAINT user_notification_email_state_email_group_check CHECK ((email_group = ANY (ARRAY['batch'::text, 'summary'::text]))) ); CREATE TABLE public.user_notification_prefs ( user_id uuid NOT NULL, kind text NOT NULL, inbox boolean NOT NULL, phone boolean NOT NULL, email boolean NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT user_notification_prefs_kind_check CHECK ((kind = ANY (ARRAY['new_music'::text, 'quarantine_flagged'::text, 'scan_failed'::text, 'tracks_missing'::text, 'duplicates_found'::text, 'duplicates_resolved'::text, 'playback_errors'::text]))) ); CREATE TABLE public.user_notifications ( id uuid DEFAULT gen_random_uuid() NOT NULL, user_id uuid NOT NULL, kind text NOT NULL, payload jsonb DEFAULT '{}'::jsonb NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, read_at timestamp with time zone, coalesce_key text, emailed_at timestamp with time zone, CONSTRAINT user_notifications_kind_check CHECK ((kind = ANY (ARRAY['new_music'::text, 'quarantine_flagged'::text, 'scan_failed'::text, 'tracks_missing'::text, 'duplicates_found'::text, 'duplicates_resolved'::text, 'playback_errors'::text]))) ); CREATE TABLE public.users ( id uuid DEFAULT gen_random_uuid() NOT NULL, username text NOT NULL, password_hash text NOT NULL, is_admin boolean DEFAULT false NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, subsonic_password text, listenbrainz_token text, listenbrainz_enabled boolean DEFAULT false NOT NULL, display_name text, email text, timezone text DEFAULT 'UTC'::text NOT NULL, timezone_updated_at timestamp with time zone, debug_mode_enabled boolean DEFAULT false NOT NULL, api_token_hash text NOT NULL ); CREATE TABLE public.you_might_like_albums ( user_id uuid NOT NULL, album_id uuid NOT NULL, rank integer NOT NULL, built_at timestamp with time zone DEFAULT now() NOT NULL ); CREATE TABLE public.you_might_like_artists ( user_id uuid NOT NULL, artist_id uuid NOT NULL, rank integer NOT NULL, built_at timestamp with time zone DEFAULT now() NOT NULL ); ALTER TABLE ONLY public.library_changes ALTER COLUMN id SET DEFAULT nextval('public.library_changes_id_seq'::regclass); ALTER TABLE ONLY public.recommendation_tuning_audit ALTER COLUMN id SET DEFAULT nextval('public.recommendation_tuning_audit_id_seq'::regclass); INSERT INTO public.acoustid_settings (id, enabled, api_key, min_score, updated_at) VALUES (true, false, NULL, 0.85, now()); INSERT INTO public.backup_settings (id, enabled, destination, folder, endpoint, bucket, access_key, secret_key, run_hour, upload_limit_kib, updated_at, include_music) VALUES (true, false, 's3', '', '', '', '', '', 3, 4096, now(), false); INSERT INTO public.cover_art_provider_settings (provider_id, enabled, api_key, display_order, created_at, updated_at) VALUES ('mbcaa', true, NULL, 0, now(), now()); INSERT INTO public.cover_art_provider_settings (provider_id, enabled, api_key, display_order, created_at, updated_at) VALUES ('theaudiodb', true, NULL, 1, now(), now()); INSERT INTO public.cover_art_sources_meta (id, current_version, last_registered_providers_hash) VALUES (true, 1, ''); INSERT INTO public.fingerprint_settings (id, enabled, chromaprint_length_sec, acoustic_max_bit_error_rate, backfill_concurrency, sweep_interval_hours, updated_at, auto_resolve) VALUES (true, true, 120, 0.15, 2, 1, now(), true); INSERT INTO public.library_changes_trimmed (id, through) VALUES (true, 0); INSERT INTO public.library_settings (id, scan_paths, updated_at) VALUES (true, NULL, now()); INSERT INTO public.loudness_settings (id, enabled, backfill_concurrency, updated_at, leveled_cache_mb) VALUES (true, true, 2, now(), 2048); INSERT INTO public.network_settings (id, trusted_proxy_hops, public_url) VALUES (true, 1, ''); INSERT INTO public.notification_email_settings (id, summary_hour, batch_window_minutes, updated_at) VALUES (true, 9, 60, now()); INSERT INTO public.registration_settings (id, mode) VALUES (true, 'invite_only'); INSERT INTO public.smtp_config (id, enabled, host, port, username, password, from_address, from_name, use_tls, updated_at) VALUES (true, false, '', 587, '', '', '', 'Minstrel', true, now()); INSERT INTO public.tag_provider_settings (provider_id, enabled, api_key, display_order, created_at, updated_at) VALUES ('musicbrainz', true, NULL, 0, now(), now()); INSERT INTO public.tag_provider_settings (provider_id, enabled, api_key, display_order, created_at, updated_at) VALUES ('lastfm', false, NULL, 1, now(), now()); INSERT INTO public.tag_sources_meta (id, current_version, last_registered_providers_hash) VALUES (true, 1, ''); SELECT pg_catalog.setval('public.library_changes_id_seq', 1, false); SELECT pg_catalog.setval('public.recommendation_tuning_audit_id_seq', 1, false); ALTER TABLE ONLY public.acoustid_settings ADD CONSTRAINT acoustid_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.album_loudness ADD CONSTRAINT album_loudness_pkey PRIMARY KEY (album_id); ALTER TABLE ONLY public.albums ADD CONSTRAINT albums_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.artist_similarity_fetches ADD CONSTRAINT artist_similarity_fetches_pkey PRIMARY KEY (artist_id); ALTER TABLE ONLY public.artist_similarity ADD CONSTRAINT artist_similarity_pkey PRIMARY KEY (artist_a_id, artist_b_id, source); ALTER TABLE ONLY public.artists ADD CONSTRAINT artists_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.audit_log ADD CONSTRAINT audit_log_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.backup_music_files ADD CONSTRAINT backup_music_files_pkey PRIMARY KEY (track_id); ALTER TABLE ONLY public.backup_runs ADD CONSTRAINT backup_runs_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.backup_settings ADD CONSTRAINT backup_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.contextual_likes ADD CONSTRAINT contextual_likes_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.cover_art_provider_settings ADD CONSTRAINT cover_art_provider_settings_pkey PRIMARY KEY (provider_id); ALTER TABLE ONLY public.cover_art_sources_meta ADD CONSTRAINT cover_art_sources_meta_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.diagnostic_events ADD CONSTRAINT diagnostic_events_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.duplicate_group_members ADD CONSTRAINT duplicate_group_members_pkey PRIMARY KEY (group_id, track_id); ALTER TABLE ONLY public.duplicate_groups ADD CONSTRAINT duplicate_groups_member_key_key UNIQUE (member_key); ALTER TABLE ONLY public.duplicate_groups ADD CONSTRAINT duplicate_groups_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.duplicate_sweeps ADD CONSTRAINT duplicate_sweeps_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.fingerprint_settings ADD CONSTRAINT fingerprint_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.general_likes_albums ADD CONSTRAINT general_likes_albums_pkey PRIMARY KEY (user_id, album_id); ALTER TABLE ONLY public.general_likes_artists ADD CONSTRAINT general_likes_artists_pkey PRIMARY KEY (user_id, artist_id); ALTER TABLE ONLY public.general_likes ADD CONSTRAINT general_likes_pkey PRIMARY KEY (user_id, track_id); ALTER TABLE ONLY public.library_changes ADD CONSTRAINT library_changes_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.library_changes_trimmed ADD CONSTRAINT library_changes_trimmed_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.library_settings ADD CONSTRAINT library_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.listenbrainz_similar_recordings ADD CONSTRAINT listenbrainz_similar_recordings_pkey PRIMARY KEY (seed_track_id, recording_mbid); ALTER TABLE ONLY public.loudness_settings ADD CONSTRAINT loudness_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.mb_release_groups ADD CONSTRAINT mb_release_groups_pkey PRIMARY KEY (mbid); ALTER TABLE ONLY public.mb_release_tracks ADD CONSTRAINT mb_release_tracks_pkey PRIMARY KEY (release_mbid, medium_position, "position"); ALTER TABLE ONLY public.mb_releases ADD CONSTRAINT mb_releases_pkey PRIMARY KEY (mbid); ALTER TABLE ONLY public.network_settings ADD CONSTRAINT network_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.notification_email_settings ADD CONSTRAINT notification_email_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.password_resets ADD CONSTRAINT password_resets_pkey PRIMARY KEY (token); ALTER TABLE ONLY public.play_events ADD CONSTRAINT play_events_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.play_sessions ADD CONSTRAINT play_sessions_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.playback_errors ADD CONSTRAINT playback_errors_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.playlist_tracks ADD CONSTRAINT playlist_tracks_pkey PRIMARY KEY (playlist_id, "position"); ALTER TABLE ONLY public.playlists ADD CONSTRAINT playlists_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.quarantine_actions ADD CONSTRAINT quarantine_actions_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.quarantine ADD CONSTRAINT quarantine_pkey PRIMARY KEY (user_id, track_id); ALTER TABLE ONLY public.recommendation_tuning_audit ADD CONSTRAINT recommendation_tuning_audit_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.recommendation_weight_profiles ADD CONSTRAINT recommendation_weight_profiles_pkey PRIMARY KEY (profile); ALTER TABLE ONLY public.registration_settings ADD CONSTRAINT registration_settings_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.scan_runs ADD CONSTRAINT scan_runs_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.scrobble_queue ADD CONSTRAINT scrobble_queue_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.scrobble_queue ADD CONSTRAINT scrobble_queue_play_event_id_key UNIQUE (play_event_id); ALTER TABLE ONLY public.sessions ADD CONSTRAINT sessions_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.sessions ADD CONSTRAINT sessions_token_hash_key UNIQUE (token_hash); ALTER TABLE ONLY public.skip_events ADD CONSTRAINT skip_events_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.smtp_config ADD CONSTRAINT smtp_config_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.system_playlist_rotation_state ADD CONSTRAINT system_playlist_rotation_state_pkey PRIMARY KEY (user_id, playlist_kind); ALTER TABLE ONLY public.system_playlist_runs ADD CONSTRAINT system_playlist_runs_pkey PRIMARY KEY (user_id); ALTER TABLE ONLY public.tag_provider_settings ADD CONSTRAINT tag_provider_settings_pkey PRIMARY KEY (provider_id); ALTER TABLE ONLY public.tag_sources_meta ADD CONSTRAINT tag_sources_meta_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.taste_profile_artists ADD CONSTRAINT taste_profile_artists_pkey PRIMARY KEY (user_id, artist_id); ALTER TABLE ONLY public.taste_profile_eras ADD CONSTRAINT taste_profile_eras_pkey PRIMARY KEY (user_id, era); ALTER TABLE ONLY public.taste_profile_moods ADD CONSTRAINT taste_profile_moods_pkey PRIMARY KEY (user_id, mood); ALTER TABLE ONLY public.taste_profile_tags ADD CONSTRAINT taste_profile_tags_pkey PRIMARY KEY (user_id, tag); ALTER TABLE ONLY public.taste_tuning ADD CONSTRAINT taste_tuning_pkey PRIMARY KEY (singleton); ALTER TABLE ONLY public.track_acoustid_lookups ADD CONSTRAINT track_acoustid_lookups_pkey PRIMARY KEY (track_id); ALTER TABLE ONLY public.track_fingerprints ADD CONSTRAINT track_fingerprints_pkey PRIMARY KEY (track_id); ALTER TABLE ONLY public.track_loudness ADD CONSTRAINT track_loudness_pkey PRIMARY KEY (track_id); ALTER TABLE ONLY public.track_similarity_fetches ADD CONSTRAINT track_similarity_fetches_pkey PRIMARY KEY (track_id); ALTER TABLE ONLY public.track_similarity ADD CONSTRAINT track_similarity_pkey PRIMARY KEY (track_a_id, track_b_id, source); ALTER TABLE ONLY public.track_tags ADD CONSTRAINT track_tags_pkey PRIMARY KEY (track_id, tag); ALTER TABLE ONLY public.tracks ADD CONSTRAINT tracks_file_path_key UNIQUE (file_path); ALTER TABLE ONLY public.tracks ADD CONSTRAINT tracks_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.user_invites ADD CONSTRAINT user_invites_pkey PRIMARY KEY (token); ALTER TABLE ONLY public.user_normalization_prefs ADD CONSTRAINT user_normalization_prefs_pkey PRIMARY KEY (user_id); ALTER TABLE ONLY public.user_notification_email_state ADD CONSTRAINT user_notification_email_state_pkey PRIMARY KEY (user_id, email_group); ALTER TABLE ONLY public.user_notification_prefs ADD CONSTRAINT user_notification_prefs_pkey PRIMARY KEY (user_id, kind); ALTER TABLE ONLY public.user_notifications ADD CONSTRAINT user_notifications_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.users ADD CONSTRAINT users_api_token_hash_key UNIQUE (api_token_hash); ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.users ADD CONSTRAINT users_username_key UNIQUE (username); ALTER TABLE ONLY public.you_might_like_albums ADD CONSTRAINT you_might_like_albums_pkey PRIMARY KEY (user_id, album_id); ALTER TABLE ONLY public.you_might_like_artists ADD CONSTRAINT you_might_like_artists_pkey PRIMARY KEY (user_id, artist_id); CREATE INDEX albums_artist ON public.albums USING btree (artist_id); CREATE INDEX albums_cover_art_source_idx ON public.albums USING btree (cover_art_source); CREATE UNIQUE INDEX albums_mbid_unique ON public.albums USING btree (mbid) WHERE (mbid IS NOT NULL); CREATE INDEX albums_release_group_mbid_idx ON public.albums USING btree (release_group_mbid) WHERE (release_group_mbid IS NOT NULL); CREATE INDEX albums_sort_title ON public.albums USING btree (sort_title); CREATE INDEX artist_similarity_a_score_idx ON public.artist_similarity USING btree (artist_a_id, score DESC); CREATE INDEX artists_artist_art_source_idx ON public.artists USING btree (artist_art_source); CREATE UNIQUE INDEX artists_mbid_unique ON public.artists USING btree (mbid) WHERE (mbid IS NOT NULL); CREATE INDEX artists_sort_name ON public.artists USING btree (sort_name); CREATE INDEX audit_log_actor_id_idx ON public.audit_log USING btree (actor_id); CREATE INDEX audit_log_created_at_idx ON public.audit_log USING btree (created_at DESC); CREATE INDEX audit_log_target_id_idx ON public.audit_log USING btree (target_id); CREATE INDEX backup_runs_started_at_idx ON public.backup_runs USING btree (started_at DESC); CREATE INDEX contextual_likes_active_idx ON public.contextual_likes USING btree (user_id, track_id) WHERE (deleted_at IS NULL); CREATE INDEX contextual_likes_vector_idx ON public.contextual_likes USING gin (session_vector); CREATE INDEX duplicate_group_members_track_idx ON public.duplicate_group_members USING btree (track_id); CREATE INDEX duplicate_groups_status_idx ON public.duplicate_groups USING btree (status); CREATE INDEX duplicate_sweeps_started_at_idx ON public.duplicate_sweeps USING btree (started_at DESC); CREATE INDEX general_likes_albums_user_liked_at_idx ON public.general_likes_albums USING btree (user_id, liked_at DESC); CREATE INDEX general_likes_artists_user_liked_at_idx ON public.general_likes_artists USING btree (user_id, liked_at DESC); CREATE INDEX general_likes_user_liked_at_idx ON public.general_likes USING btree (user_id, liked_at DESC); CREATE INDEX idx_diagnostic_events_client ON public.diagnostic_events USING btree (client_id, occurred_at DESC); CREATE INDEX idx_diagnostic_events_received ON public.diagnostic_events USING btree (received_at); CREATE INDEX idx_diagnostic_events_user ON public.diagnostic_events USING btree (user_id, occurred_at DESC); CREATE INDEX idx_library_changes_entity ON public.library_changes USING btree (entity_type, entity_id, id DESC); CREATE INDEX idx_library_changes_id ON public.library_changes USING btree (id); CREATE INDEX idx_playback_errors_track ON public.playback_errors USING btree (track_id); CREATE INDEX idx_playback_errors_unresolved ON public.playback_errors USING btree (occurred_at DESC) WHERE (resolved_at IS NULL); CREATE INDEX listenbrainz_similar_recordings_mbid_idx ON public.listenbrainz_similar_recordings USING btree (recording_mbid); CREATE INDEX mb_release_tracks_recording_idx ON public.mb_release_tracks USING btree (recording_mbid) WHERE (recording_mbid <> ''::text); CREATE INDEX mb_releases_group_idx ON public.mb_releases USING btree (release_group_mbid); CREATE INDEX password_resets_active_idx ON public.password_resets USING btree (expires_at) WHERE (used_at IS NULL); CREATE INDEX password_resets_user_id_idx ON public.password_resets USING btree (user_id); CREATE INDEX play_events_started_idx ON public.play_events USING btree (started_at); CREATE INDEX play_events_track_idx ON public.play_events USING btree (track_id); CREATE INDEX play_events_user_started_idx ON public.play_events USING btree (user_id, started_at DESC); CREATE INDEX play_events_user_track_idx ON public.play_events USING btree (user_id, track_id); CREATE INDEX play_sessions_user_last_event_idx ON public.play_sessions USING btree (user_id, last_event_at DESC); CREATE INDEX playlist_tracks_track_idx ON public.playlist_tracks USING btree (track_id) WHERE (track_id IS NOT NULL); CREATE INDEX playlists_public_idx ON public.playlists USING btree (is_public, updated_at DESC) WHERE is_public; CREATE INDEX playlists_user_idx ON public.playlists USING btree (user_id, updated_at DESC); CREATE INDEX playlists_user_kind_idx ON public.playlists USING btree (user_id, kind); CREATE INDEX quarantine_actions_created_idx ON public.quarantine_actions USING btree (created_at DESC); CREATE INDEX quarantine_actions_track_idx ON public.quarantine_actions USING btree (track_id); CREATE INDEX quarantine_track_idx ON public.quarantine USING btree (track_id); CREATE INDEX recommendation_tuning_audit_changed_at_idx ON public.recommendation_tuning_audit USING btree (changed_at DESC); CREATE INDEX scan_runs_started_at_idx ON public.scan_runs USING btree (started_at DESC); CREATE INDEX scrobble_queue_pending_idx ON public.scrobble_queue USING btree (next_attempt_at) WHERE (status = 'pending'::text); CREATE INDEX sessions_user_id_idx ON public.sessions USING btree (user_id); CREATE INDEX skip_events_user_skipped_idx ON public.skip_events USING btree (user_id, skipped_at DESC); CREATE INDEX taste_profile_artists_user_weight_idx ON public.taste_profile_artists USING btree (user_id, weight DESC); CREATE INDEX taste_profile_eras_user_weight_idx ON public.taste_profile_eras USING btree (user_id, weight DESC); CREATE INDEX taste_profile_moods_user_weight_idx ON public.taste_profile_moods USING btree (user_id, weight DESC); CREATE INDEX taste_profile_tags_user_weight_idx ON public.taste_profile_tags USING btree (user_id, weight DESC); CREATE INDEX track_acoustid_lookups_state ON public.track_acoustid_lookups USING btree (state); CREATE INDEX track_fingerprints_audio_stream_sha256 ON public.track_fingerprints USING btree (audio_stream_sha256) WHERE (audio_stream_sha256 IS NOT NULL); CREATE INDEX track_similarity_a_score_idx ON public.track_similarity USING btree (track_a_id, score DESC); CREATE INDEX track_tags_track_idx ON public.track_tags USING btree (track_id); CREATE INDEX tracks_album ON public.tracks USING btree (album_id, disc_number, track_number); CREATE INDEX tracks_artist ON public.tracks USING btree (artist_id); CREATE INDEX tracks_duration_id_idx ON public.tracks USING btree (duration_ms, id); CREATE INDEX tracks_mbid_idx ON public.tracks USING btree (mbid) WHERE (mbid IS NOT NULL); CREATE INDEX tracks_missing_since_idx ON public.tracks USING btree (missing_since) WHERE (missing_since IS NOT NULL); CREATE INDEX tracks_song_key_idx ON public.tracks USING btree (song_key); CREATE INDEX tracks_source_suspect_idx ON public.tracks USING btree (id) WHERE (source_verdict = 'suspect'::text); CREATE INDEX tracks_tag_source_idx ON public.tracks USING btree (tag_source, tag_sources_version); CREATE INDEX user_invites_active_idx ON public.user_invites USING btree (expires_at) WHERE (redeemed_at IS NULL); CREATE UNIQUE INDEX user_notifications_coalesce_idx ON public.user_notifications USING btree (user_id, coalesce_key) WHERE ((read_at IS NULL) AND (coalesce_key IS NOT NULL)); CREATE INDEX user_notifications_email_pending_idx ON public.user_notifications USING btree (user_id, created_at) WHERE ((read_at IS NULL) AND (emailed_at IS NULL)); CREATE INDEX user_notifications_unread_idx ON public.user_notifications USING btree (user_id, created_at DESC) WHERE (read_at IS NULL); CREATE INDEX user_notifications_user_created_idx ON public.user_notifications USING btree (user_id, created_at DESC); CREATE UNIQUE INDEX users_email_unique ON public.users USING btree (lower(email)) WHERE (email IS NOT NULL); CREATE INDEX you_might_like_albums_user_rank_idx ON public.you_might_like_albums USING btree (user_id, rank); CREATE INDEX you_might_like_artists_user_rank_idx ON public.you_might_like_artists USING btree (user_id, rank); ALTER TABLE ONLY public.album_loudness ADD CONSTRAINT album_loudness_album_id_fkey FOREIGN KEY (album_id) REFERENCES public.albums(id) ON DELETE CASCADE; ALTER TABLE ONLY public.albums ADD CONSTRAINT albums_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.artist_similarity ADD CONSTRAINT artist_similarity_artist_a_id_fkey FOREIGN KEY (artist_a_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.artist_similarity ADD CONSTRAINT artist_similarity_artist_b_id_fkey FOREIGN KEY (artist_b_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.artist_similarity_fetches ADD CONSTRAINT artist_similarity_fetches_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.audit_log ADD CONSTRAINT audit_log_actor_id_fkey FOREIGN KEY (actor_id) REFERENCES public.users(id) ON DELETE SET NULL; ALTER TABLE ONLY public.audit_log ADD CONSTRAINT audit_log_target_id_fkey FOREIGN KEY (target_id) REFERENCES public.users(id) ON DELETE SET NULL; ALTER TABLE ONLY public.backup_music_files ADD CONSTRAINT backup_music_files_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.contextual_likes ADD CONSTRAINT contextual_likes_session_id_fkey FOREIGN KEY (session_id) REFERENCES public.play_sessions(id) ON DELETE SET NULL; ALTER TABLE ONLY public.contextual_likes ADD CONSTRAINT contextual_likes_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.contextual_likes ADD CONSTRAINT contextual_likes_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.diagnostic_events ADD CONSTRAINT diagnostic_events_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.duplicate_group_members ADD CONSTRAINT duplicate_group_members_group_id_fkey FOREIGN KEY (group_id) REFERENCES public.duplicate_groups(id) ON DELETE CASCADE; ALTER TABLE ONLY public.duplicate_group_members ADD CONSTRAINT duplicate_group_members_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.duplicate_groups ADD CONSTRAINT duplicate_groups_last_seen_sweep_id_fkey FOREIGN KEY (last_seen_sweep_id) REFERENCES public.duplicate_sweeps(id) ON DELETE SET NULL; ALTER TABLE ONLY public.general_likes_albums ADD CONSTRAINT general_likes_albums_album_id_fkey FOREIGN KEY (album_id) REFERENCES public.albums(id) ON DELETE CASCADE; ALTER TABLE ONLY public.general_likes_albums ADD CONSTRAINT general_likes_albums_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.general_likes_artists ADD CONSTRAINT general_likes_artists_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.general_likes_artists ADD CONSTRAINT general_likes_artists_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.general_likes ADD CONSTRAINT general_likes_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.general_likes ADD CONSTRAINT general_likes_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.listenbrainz_similar_recordings ADD CONSTRAINT listenbrainz_similar_recordings_seed_track_id_fkey FOREIGN KEY (seed_track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.mb_release_tracks ADD CONSTRAINT mb_release_tracks_release_mbid_fkey FOREIGN KEY (release_mbid) REFERENCES public.mb_releases(mbid) ON DELETE CASCADE; ALTER TABLE ONLY public.mb_releases ADD CONSTRAINT mb_releases_release_group_mbid_fkey FOREIGN KEY (release_group_mbid) REFERENCES public.mb_release_groups(mbid) ON DELETE CASCADE; ALTER TABLE ONLY public.password_resets ADD CONSTRAINT password_resets_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.play_events ADD CONSTRAINT play_events_session_id_fkey FOREIGN KEY (session_id) REFERENCES public.play_sessions(id) ON DELETE CASCADE; ALTER TABLE ONLY public.play_events ADD CONSTRAINT play_events_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.play_events ADD CONSTRAINT play_events_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.play_sessions ADD CONSTRAINT play_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.playback_errors ADD CONSTRAINT playback_errors_resolved_by_fkey FOREIGN KEY (resolved_by) REFERENCES public.users(id); ALTER TABLE ONLY public.playback_errors ADD CONSTRAINT playback_errors_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.playback_errors ADD CONSTRAINT playback_errors_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.playlist_tracks ADD CONSTRAINT playlist_tracks_playlist_id_fkey FOREIGN KEY (playlist_id) REFERENCES public.playlists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.playlist_tracks ADD CONSTRAINT playlist_tracks_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE SET NULL; ALTER TABLE ONLY public.playlists ADD CONSTRAINT playlists_seed_artist_id_fkey FOREIGN KEY (seed_artist_id) REFERENCES public.artists(id) ON DELETE SET NULL; ALTER TABLE ONLY public.playlists ADD CONSTRAINT playlists_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.quarantine_actions ADD CONSTRAINT quarantine_actions_admin_id_fkey FOREIGN KEY (admin_id) REFERENCES public.users(id) ON DELETE SET NULL; ALTER TABLE ONLY public.quarantine ADD CONSTRAINT quarantine_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.quarantine ADD CONSTRAINT quarantine_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.scrobble_queue ADD CONSTRAINT scrobble_queue_play_event_id_fkey FOREIGN KEY (play_event_id) REFERENCES public.play_events(id) ON DELETE CASCADE; ALTER TABLE ONLY public.scrobble_queue ADD CONSTRAINT scrobble_queue_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.sessions ADD CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.skip_events ADD CONSTRAINT skip_events_session_id_fkey FOREIGN KEY (session_id) REFERENCES public.play_sessions(id) ON DELETE CASCADE; ALTER TABLE ONLY public.skip_events ADD CONSTRAINT skip_events_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.skip_events ADD CONSTRAINT skip_events_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.system_playlist_rotation_state ADD CONSTRAINT system_playlist_rotation_state_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.system_playlist_runs ADD CONSTRAINT system_playlist_runs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.taste_profile_artists ADD CONSTRAINT taste_profile_artists_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.taste_profile_artists ADD CONSTRAINT taste_profile_artists_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.taste_profile_eras ADD CONSTRAINT taste_profile_eras_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.taste_profile_moods ADD CONSTRAINT taste_profile_moods_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.taste_profile_tags ADD CONSTRAINT taste_profile_tags_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_acoustid_lookups ADD CONSTRAINT track_acoustid_lookups_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_fingerprints ADD CONSTRAINT track_fingerprints_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_loudness ADD CONSTRAINT track_loudness_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_similarity_fetches ADD CONSTRAINT track_similarity_fetches_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_similarity ADD CONSTRAINT track_similarity_track_a_id_fkey FOREIGN KEY (track_a_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_similarity ADD CONSTRAINT track_similarity_track_b_id_fkey FOREIGN KEY (track_b_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.track_tags ADD CONSTRAINT track_tags_track_id_fkey FOREIGN KEY (track_id) REFERENCES public.tracks(id) ON DELETE CASCADE; ALTER TABLE ONLY public.tracks ADD CONSTRAINT tracks_album_id_fkey FOREIGN KEY (album_id) REFERENCES public.albums(id) ON DELETE CASCADE; ALTER TABLE ONLY public.tracks ADD CONSTRAINT tracks_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.tracks ADD CONSTRAINT tracks_removed_by_fkey FOREIGN KEY (removed_by) REFERENCES public.users(id) ON DELETE SET NULL; ALTER TABLE ONLY public.user_invites ADD CONSTRAINT user_invites_invited_by_fkey FOREIGN KEY (invited_by) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.user_invites ADD CONSTRAINT user_invites_redeemed_by_fkey FOREIGN KEY (redeemed_by) REFERENCES public.users(id) ON DELETE SET NULL; ALTER TABLE ONLY public.user_normalization_prefs ADD CONSTRAINT user_normalization_prefs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.user_notification_email_state ADD CONSTRAINT user_notification_email_state_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.user_notification_prefs ADD CONSTRAINT user_notification_prefs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.user_notifications ADD CONSTRAINT user_notifications_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.you_might_like_albums ADD CONSTRAINT you_might_like_albums_album_id_fkey FOREIGN KEY (album_id) REFERENCES public.albums(id) ON DELETE CASCADE; ALTER TABLE ONLY public.you_might_like_albums ADD CONSTRAINT you_might_like_albums_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; ALTER TABLE ONLY public.you_might_like_artists ADD CONSTRAINT you_might_like_artists_artist_id_fkey FOREIGN KEY (artist_id) REFERENCES public.artists(id) ON DELETE CASCADE; ALTER TABLE ONLY public.you_might_like_artists ADD CONSTRAINT you_might_like_artists_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;