MediaViewer committed these changes to its vendored shared copy
(e83afad, ca8a89e) without upstreaming them, so the next shared sync
would have removed them from MediaViewer and broken its build.
Ported here as a three-way merge against 74942e5, the snapshot
MediaViewer last synced from:
- media.MediaRecord.Rating, read in FindMedia and returned by ToJSON,
with the rating column added in migrate.go
- tagSuggestions counts media items through the library's visibility
rules instead of raw media_tags rows
- MediaPlayer autoplayMuted prop (defaults to false, so existing
callers are unaffected)
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
168 lines
6.2 KiB
Go
168 lines
6.2 KiB
Go
package media
|
|
|
|
import (
|
|
"context"
|
|
|
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/settings"
|
|
)
|
|
|
|
// Migrate creates the settings table, then every table (and index) the media routes read or
|
|
// write. Statements use CREATE TABLE/INDEX IF NOT EXISTS, so whichever service starts first
|
|
// against a fresh database creates them and every other service's own startup is a no-op.
|
|
//
|
|
// users is created because several media tables reference it by foreign key. Identity itself lives
|
|
// in AuthGuard, so the column is kept for foreign-key integrity and is not queried here.
|
|
//
|
|
// A service runs its own migration for any tables it alone owns (for example ContentScanner's
|
|
// media_worker_jobs and CraftStudio's generated_media); those are not run here.
|
|
func (c *Service) Migrate(ctx context.Context) error {
|
|
if err := settings.Migrate(ctx, c.db); err != nil {
|
|
return err
|
|
}
|
|
for _, statement := range migrationStatements {
|
|
if _, err := c.db.Exec(ctx, statement); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var migrationStatements = []string{
|
|
`CREATE EXTENSION IF NOT EXISTS pgcrypto`,
|
|
`CREATE EXTENSION IF NOT EXISTS pg_trgm`,
|
|
`DROP INDEX IF EXISTS media_search_idx`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS users (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
email TEXT NOT NULL UNIQUE,
|
|
username TEXT,
|
|
password_hash TEXT NOT NULL,
|
|
avatar_url TEXT,
|
|
bio TEXT,
|
|
role TEXT NOT NULL DEFAULT 'user',
|
|
permissions JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
disabled BOOLEAN NOT NULL DEFAULT FALSE,
|
|
totp_secret TEXT,
|
|
mfa_enabled BOOLEAN NOT NULL DEFAULT FALSE,
|
|
mfa_backup_codes JSONB,
|
|
password_reset_token TEXT,
|
|
password_reset_expires TIMESTAMPTZ,
|
|
last_login_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
)`,
|
|
`CREATE UNIQUE INDEX IF NOT EXISTS users_username_unique ON users (LOWER(username)) WHERE username IS NOT NULL`,
|
|
`CREATE INDEX IF NOT EXISTS users_email_idx ON users (LOWER(email))`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS media (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
media_key TEXT NOT NULL UNIQUE,
|
|
media_type TEXT NOT NULL DEFAULT 'unknown',
|
|
app TEXT,
|
|
source TEXT NOT NULL DEFAULT 'Filesystem',
|
|
title TEXT,
|
|
description TEXT,
|
|
category TEXT,
|
|
file_path TEXT,
|
|
original_filename TEXT,
|
|
active BOOLEAN NOT NULL DEFAULT TRUE,
|
|
content_scanner_processed BOOLEAN NOT NULL DEFAULT FALSE,
|
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
width INT,
|
|
height INT,
|
|
duration_ms BIGINT,
|
|
size_bytes BIGINT,
|
|
is_vr BOOLEAN NOT NULL DEFAULT FALSE,
|
|
has_funscript BOOLEAN NOT NULL DEFAULT FALSE,
|
|
rating TEXT,
|
|
funscript_action_count INT,
|
|
funscript_avg_speed REAL,
|
|
funscript_max_speed REAL,
|
|
speed REAL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
modified_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
)`,
|
|
`ALTER TABLE media ADD COLUMN IF NOT EXISTS content_scanner_processed BOOLEAN NOT NULL DEFAULT FALSE`,
|
|
`ALTER TABLE media ADD COLUMN IF NOT EXISTS rating TEXT`,
|
|
`ALTER TABLE media ADD COLUMN IF NOT EXISTS funscript_action_count INT`,
|
|
`ALTER TABLE media ADD COLUMN IF NOT EXISTS funscript_avg_speed REAL`,
|
|
`ALTER TABLE media ADD COLUMN IF NOT EXISTS funscript_max_speed REAL`,
|
|
`CREATE INDEX IF NOT EXISTS media_active_idx ON media (active)`,
|
|
`CREATE INDEX IF NOT EXISTS media_file_path_idx ON media (file_path)`,
|
|
`CREATE INDEX IF NOT EXISTS media_active_type_idx ON media (active, media_type)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS media_stats (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
media_id BIGINT REFERENCES media(id) ON DELETE CASCADE,
|
|
media_key TEXT NOT NULL,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
stat_type TEXT NOT NULL,
|
|
app TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
)`,
|
|
`CREATE UNIQUE INDEX IF NOT EXISTS media_stats_like_unique ON media_stats (user_id, media_key) WHERE stat_type = 'like' AND user_id IS NOT NULL`,
|
|
`CREATE INDEX IF NOT EXISTS media_stats_key_idx ON media_stats (media_key, stat_type)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS media_view_sessions (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
media_id BIGINT REFERENCES media(id) ON DELETE CASCADE,
|
|
media_key TEXT NOT NULL,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
duration_ms BIGINT NOT NULL DEFAULT 0,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS media_tags (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
media_id BIGINT REFERENCES media(id) ON DELETE CASCADE,
|
|
media_key TEXT NOT NULL,
|
|
tag TEXT NOT NULL,
|
|
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
UNIQUE (media_key, tag, user_id)
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS media_tags_tag_idx ON media_tags (LOWER(tag))`,
|
|
`CREATE INDEX IF NOT EXISTS media_tags_media_idx ON media_tags (media_key)`,
|
|
// AI-attributed tags are inserted with user_id NULL; a plain UNIQUE(media_key, tag, user_id)
|
|
// constraint does not dedupe NULLs against each other, so a dedicated partial index is
|
|
// required to make repeated analysis runs idempotent instead of piling up duplicate tag rows.
|
|
`CREATE UNIQUE INDEX IF NOT EXISTS media_tags_ai_tag_unique ON media_tags (media_key, tag) WHERE user_id IS NULL`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS media_tag_votes (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
media_key TEXT NOT NULL,
|
|
tag TEXT NOT NULL,
|
|
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
vote SMALLINT NOT NULL CHECK (vote IN (-1, 1)),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
UNIQUE (media_key, tag, user_id)
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS playlists (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
UNIQUE (user_id, name)
|
|
)`,
|
|
`CREATE TABLE IF NOT EXISTS playlist_items (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
playlist_id BIGINT NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
|
|
media_id BIGINT REFERENCES media(id) ON DELETE CASCADE,
|
|
media_key TEXT NOT NULL,
|
|
position INT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
UNIQUE (playlist_id, media_key)
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS thumbnails (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
media_id BIGINT REFERENCES media(id) ON DELETE SET NULL,
|
|
source_path TEXT UNIQUE,
|
|
width INT,
|
|
height INT,
|
|
file_size_bytes BIGINT,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
)`,
|
|
}
|