goon/alembic/versions/20260608_0022_device_scoped_user_state.py
jtrzupek c8baa11604 feat(api): device-scope user state (favorites/progress/blacklists)
Public instance has no accounts, so all user state was GLOBAL in DB — new users
saw/overwrote each other's (and Jan's) favorites, watched badges and blacklists
(bug 2026-06-10). Add device_id (VARCHAR 64) to 9 state tables with composite PK
(device_id, entity_id); app sends X-Device-Id header (get_device_id dep). All
favorites/scene-favorites/blacklist/watch + scene&movie list/detail (is_favorite,
watched, blacklist-hide) now filter by device. Existing rows backfilled to
'legacy-shared'; POST /me/adopt-legacy reassigns them to the caller once. Old
clients (no header) map to legacy-shared so they keep working until OTA updates.

Migration 0022: add col, backfill, composite PK. Verified on prod: 967 progress
rows preserved, device isolation holds (new device sees none of legacy state).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 08:58:01 +02:00

58 lines
2.3 KiB
Python

"""device-scope user state: favorites / play-progress / blacklists
Revision ID: 0022_device_scoped_user_state
Revises: 0021_scene_tags_tag_id_stats
Create Date: 2026-06-08
Publiczna instancja nie ma kont → stan usera był GLOBALNY, nowi użytkownicy
nadpisywali ulubione/blacklisty/progress Jana (bug 2026-06-08). Dodajemy `device_id`
(VARCHAR 64) do 9 tabel stanu i przerabiamy PK na composite `(device_id, <entity>)`.
Istniejące wiersze → `device_id = 'legacy-shared'` (sentinel). Apka po update wysyła
`X-Device-Id`; `/me/adopt-legacy` przepina legacy na docelowe device. Zero utraty
danych (backfill + composite PK, nie drop).
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "0022_device_scoped_user_state"
down_revision: str | None = "0021_scene_tags_tag_id_stats"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
LEGACY = "legacy-shared"
# (table, entity_pk_column)
_TABLES: list[tuple[str, str]] = [
("favorite_performers", "performer_id"),
("favorite_studios", "studio_id"),
("favorite_scenes", "scene_id"),
("favorite_movies", "movie_id"),
("scene_play_progress", "scene_id"),
("movie_play_progress", "movie_id"),
("blacklisted_performers", "performer_id"),
("blacklisted_studios", "studio_id"),
("blacklisted_tags", "tag_id"),
]
def upgrade() -> None:
for table, entity in _TABLES:
# 1. dodaj nullable, 2. backfill legacy, 3. NOT NULL, 4. composite PK
op.add_column(table, sa.Column("device_id", sa.String(length=64), nullable=True))
op.execute(sa.text(f"UPDATE {table} SET device_id = :d").bindparams(d=LEGACY))
op.alter_column(table, "device_id", nullable=False)
op.drop_constraint(f"pk_{table}", table, type_="primary")
op.create_primary_key(f"pk_{table}", table, ["device_id", entity])
def downgrade() -> None:
for table, entity in _TABLES:
op.drop_constraint(f"pk_{table}", table, type_="primary")
# przywróć single-col PK (zakłada brak duplikatów entity po dropie device_id)
op.execute(sa.text(f"DELETE FROM {table} a USING {table} b "
f"WHERE a.ctid < b.ctid AND a.{entity} = b.{entity}"))
op.create_primary_key(f"pk_{table}", table, [entity])
op.drop_column(table, "device_id")