diff --git a/alembic/versions/0008_fc2d_vii_c_artist_deconfliction.py b/alembic/versions/0008_fc2d_vii_c_artist_deconfliction.py new file mode 100644 index 0000000..4019e2d --- /dev/null +++ b/alembic/versions/0008_fc2d_vii_c_artist_deconfliction.py @@ -0,0 +1,52 @@ +"""fc2d-vii-c: image_record.artist_id + backfill + drop artist tags + +Revision ID: 0008 +Revises: 0007 +Create Date: 2026-05-18 + +Internal forward-correctness migration (the big legacy-import migration +stays deferred). downgrade() does NOT recreate deleted artist tags; +downgrade is dev-only and the data is reconstructable by re-import. +""" +from typing import Sequence, Union + +import sqlalchemy as sa +from alembic import op + +from backend.app.utils.artist_backfill import ( + BACKFILL_PRIMARY_SQL, + BACKFILL_PROVENANCE_SQL, + BACKFILL_TAG_SQL, + DELETE_ARTIST_TAGS_SQL, +) + +revision: str = "0008" +down_revision: Union[str, None] = "0007" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.add_column( + "image_record", + sa.Column("artist_id", sa.Integer(), nullable=True), + ) + op.create_foreign_key( + "fk_image_record_artist_id", "image_record", "artist", + ["artist_id"], ["id"], ondelete="SET NULL", + ) + op.create_index( + "ix_image_record_artist_id", "image_record", ["artist_id"], + ) + op.execute(BACKFILL_PRIMARY_SQL) + op.execute(BACKFILL_PROVENANCE_SQL) + op.execute(BACKFILL_TAG_SQL) + op.execute(DELETE_ARTIST_TAGS_SQL) + + +def downgrade() -> None: + op.drop_index("ix_image_record_artist_id", table_name="image_record") + op.drop_constraint( + "fk_image_record_artist_id", "image_record", type_="foreignkey" + ) + op.drop_column("image_record", "artist_id") diff --git a/backend/app/models/image_record.py b/backend/app/models/image_record.py index b303e7c..f43fd60 100644 --- a/backend/app/models/image_record.py +++ b/backend/app/models/image_record.py @@ -54,6 +54,11 @@ class ImageRecord(Base): primary_post_id: Mapped[int | None] = mapped_column( ForeignKey("post.id", ondelete="SET NULL"), nullable=True, index=True ) + # FC-2d-vii-c: canonical per-image artist (the single source of truth + # for attribution; provenance posts remain lineage detail). + artist_id: Mapped[int | None] = mapped_column( + ForeignKey("artist.id", ondelete="SET NULL"), nullable=True, index=True + ) # ML fields (populated by FC-2's ml-worker) tagger_predictions: Mapped[dict | None] = mapped_column(JSON, nullable=True) diff --git a/backend/app/utils/artist_backfill.py b/backend/app/utils/artist_backfill.py new file mode 100644 index 0000000..3979b24 --- /dev/null +++ b/backend/app/utils/artist_backfill.py @@ -0,0 +1,44 @@ +"""Literal SQL for the FC-2d-vii-c artist backfill / artist-tag delete. + +Intentionally pure string constants — NO model/slug imports, NO logic — +so migration 0008 and its test share one drift-proof source of truth. +Backfill steps are ordered primary -> provenance -> artist-tag and each +only touches rows still NULL (idempotent, first match wins). The +artist-tag step matches Artist.name = Tag.name: the importer always +created both from the same artist_name string. +""" + +BACKFILL_PRIMARY_SQL = """ +UPDATE image_record AS ir +SET artist_id = s.artist_id +FROM post p +JOIN source s ON s.id = p.source_id +WHERE ir.primary_post_id = p.id + AND ir.artist_id IS NULL +""" + +BACKFILL_PROVENANCE_SQL = """ +UPDATE image_record AS ir +SET artist_id = s.artist_id +FROM ( + SELECT DISTINCT ON (ip.image_record_id) + ip.image_record_id, src.artist_id + FROM image_provenance ip + JOIN source src ON src.id = ip.source_id + ORDER BY ip.image_record_id, ip.id +) AS s +WHERE ir.id = s.image_record_id + AND ir.artist_id IS NULL +""" + +BACKFILL_TAG_SQL = """ +UPDATE image_record AS ir +SET artist_id = a.id +FROM image_tag it +JOIN tag t ON t.id = it.tag_id AND t.kind = 'artist' +JOIN artist a ON a.name = t.name +WHERE it.image_record_id = ir.id + AND ir.artist_id IS NULL +""" + +DELETE_ARTIST_TAGS_SQL = "DELETE FROM tag WHERE kind = 'artist'" diff --git a/tests/test_migration_0008.py b/tests/test_migration_0008.py new file mode 100644 index 0000000..d3261b5 --- /dev/null +++ b/tests/test_migration_0008.py @@ -0,0 +1,137 @@ +"""FC-2d-vii-c: image_record.artist_id + backfill + artist-tag delete.""" + +from datetime import UTC, datetime, timedelta + +import pytest +from sqlalchemy import func, select, text + +from backend.app.models import ( + Artist, + ImageProvenance, + ImageRecord, + Post, + Source, + Tag, + TagKind, +) +from backend.app.models.tag import image_tag +from backend.app.utils.artist_backfill import ( + BACKFILL_PRIMARY_SQL, + BACKFILL_PROVENANCE_SQL, + BACKFILL_TAG_SQL, + DELETE_ARTIST_TAGS_SQL, +) + +pytestmark = pytest.mark.integration + + +def test_image_record_has_artist_id_column(): + assert "artist_id" in {c.name for c in ImageRecord.__table__.columns} + + +async def _img(db, n): + rec = ImageRecord( + path=f"/images/bf/{n}.jpg", sha256=f"bf{n:062d}", + size_bytes=1, mime="image/jpeg", width=1, height=1, + origin="imported_filesystem", integrity_status="unknown", + ) + rec.created_at = datetime.now(UTC) - timedelta(minutes=n) + db.add(rec) + await db.flush() + return rec + + +async def _artist_source(db, name, slug): + a = Artist(name=name, slug=slug) + db.add(a) + await db.flush() + s = Source(artist_id=a.id, platform="patreon", + url=f"https://p.test/{slug}") + db.add(s) + await db.flush() + return a, s + + +async def _run_backfill(db): + await db.execute(text(BACKFILL_PRIMARY_SQL)) + await db.execute(text(BACKFILL_PROVENANCE_SQL)) + await db.execute(text(BACKFILL_TAG_SQL)) + + +@pytest.mark.asyncio +async def test_backfill_primary_post(db): + rec = await _img(db, 1) + a, s = await _artist_source(db, "Alice", "alice") + post = Post(source_id=s.id, external_post_id="1") + db.add(post) + await db.flush() + rec.primary_post_id = post.id + await db.flush() + await _run_backfill(db) + got = await db.scalar( + select(ImageRecord.artist_id).where(ImageRecord.id == rec.id) + ) + assert got == a.id + + +@pytest.mark.asyncio +async def test_backfill_provenance_fallback(db): + rec = await _img(db, 1) + a, s = await _artist_source(db, "Bob", "bob") + post = Post(source_id=s.id, external_post_id="2") + db.add(post) + await db.flush() + db.add(ImageProvenance(image_record_id=rec.id, post_id=post.id, + source_id=s.id)) + await db.flush() + await _run_backfill(db) + got = await db.scalar( + select(ImageRecord.artist_id).where(ImageRecord.id == rec.id) + ) + assert got == a.id + + +@pytest.mark.asyncio +async def test_backfill_artist_tag_by_name(db): + rec = await _img(db, 1) + a = Artist(name="Carol", slug="carol") + db.add(a) + await db.flush() + tag = Tag(name="Carol", kind=TagKind.artist) + db.add(tag) + await db.flush() + await db.execute(image_tag.insert().values( + image_record_id=rec.id, tag_id=tag.id, source="auto")) + await db.flush() + await _run_backfill(db) + got = await db.scalar( + select(ImageRecord.artist_id).where(ImageRecord.id == rec.id) + ) + assert got == a.id + + +@pytest.mark.asyncio +async def test_no_signal_stays_null(db): + rec = await _img(db, 1) + await _run_backfill(db) + got = await db.scalar( + select(ImageRecord.artist_id).where(ImageRecord.id == rec.id) + ) + assert got is None + + +@pytest.mark.asyncio +async def test_delete_removes_only_artist_tags(db): + artist_tag = Tag(name="Dave", kind=TagKind.artist) + general_tag = Tag(name="forest", kind=TagKind.general) + db.add_all([artist_tag, general_tag]) + await db.flush() + await db.execute(text(DELETE_ARTIST_TAGS_SQL)) + remaining = await db.scalar( + select(func.count()).select_from(Tag).where(Tag.kind == TagKind.artist) + ) + assert remaining == 0 + survived = await db.scalar( + select(func.count()).select_from(Tag).where(Tag.kind == TagKind.general) + ) + assert survived >= 1