feat(model): nullable Post.source_id + denormalized Post.artist_id; retire sidecar synthetics
Operator-asked 2026-06-01 after the Dymkens orphan investigation (Scribe plan #540). The pre-2030 sidecar-synthetic Source pattern (`sidecar:<platform>:<slug>` enabled=false rows) existed solely to satisfy `Post.source_id NOT NULL`, and leaked into the Subscriptions UI as phantom subscriptions. Now the data model says what's true: filesystem-imported content with no live subscription has NULL source_id, full stop. ## Schema (alembic 0030) - `post.artist_id` — NEW NOT NULL FK to artist (CASCADE). Backfilled from source.artist_id in the migration. Indexed for the artist-filter queries. - `post.source_id` — NOT NULL → nullable; FK ondelete CASCADE → SET NULL. Deleting a Source detaches its Posts instead of destroying archived content (subscription ends, archive stays). - `image_provenance.source_id` — same nullable + SET NULL. - Partial unique index `uq_post_artist_external_id_null_source` on (artist_id, external_post_id) WHERE source_id IS NULL — guards filesystem-import dedup since the existing source-bound unique ignores NULLs (Postgres treats NULL != NULL). - Sidecar synthetic Sources deleted: NULL out FKs in post, image_provenance first, then DELETE FROM source WHERE url LIKE 'sidecar:%'. The Dymkens cleanup. ## Model + service changes - `Post.source_id` → `Mapped[int | None]`; new `Post.artist_id` denormalized. - `ImageProvenance.source_id` → `Mapped[int | None]`. - Importer: `_source_for_sidecar` (synthetic-creating) → `_lookup_source_for_sidecar` (returns None when no subscription). `_find_or_create_post` takes required `artist_id`; matches on (source_id, external_post_id) for source-bound posts or (artist_id, external_post_id) for NULL-source posts. - Service queries switched off the Source detour to use Post.artist_id directly: post_feed_service.scroll/around/get_post (LEFT JOIN to Source so NULL-source posts surface); artist_service date_row/ activity/post_count; provenance_service.for_image/for_post (LEFT JOIN); gallery_service._provenance_exists_where_artist via Post.artist_id instead of ImageProvenance.source_id → Source. - `_to_dict` and provenance dict-builders emit `"source": null` for NULL-source rows. ## Frontend - `ProvenancePanel.vue` + `PostCard.vue`: render `e.source?.platform ?? 'filesystem import'` so NULL-source posts get a clear "filesystem import" affordance instead of a NaN crash. ## Tests - `test_importer_upsert_helpers`: removed the four synthetic-anchor tests; added `_find_or_create_post_idempotent_with_null_source` (dedup via the partial unique index) and `_lookup_source_for_sidecar_returns_*` (existing-subscription + none cases). The existing `_find_or_create_post_idempotent` now also passes `artist_id` and asserts it. - 8 other test files updated: every direct `Post(...)` construction gains `artist_id=<artist>.id`. The `_seed_post` helper in `test_post_feed_service` looks up artist_id from the source row so callsites stay one-arg. ## Verification on deploy After alembic 0030 runs: - `SELECT COUNT(*) FROM source WHERE url LIKE 'sidecar:%'` → 0. - `SELECT COUNT(*) FROM post WHERE source_id IS NULL` → count of filesystem-imported posts (Dymkens + any other historical). - Every `post.artist_id` non-null; consistent with source.artist_id for source-bound rows. - Subscriptions tab: no Dymkens phantom row. - Artist detail → Posts/Gallery: Dymkens's content still reachable via Post.artist_id. - Provenance panel renders "filesystem import" chip for NULL-source posts; PostCard same. ## Out of scope - UI to manage/delete orphan NULL-source Posts. Data model is right; UI follows if operator wants it.
This commit is contained in:
@@ -37,8 +37,8 @@ async def test_artist_overview_post_count(client, db):
|
||||
)
|
||||
db.add(s)
|
||||
await db.flush()
|
||||
db.add(Post(source_id=s.id, external_post_id="p1"))
|
||||
db.add(Post(source_id=s.id, external_post_id="p2"))
|
||||
db.add(Post(source_id=s.id, artist_id=a.id, external_post_id="p1"))
|
||||
db.add(Post(source_id=s.id, artist_id=a.id, external_post_id="p2"))
|
||||
await db.flush()
|
||||
await db.commit()
|
||||
resp = await client.get("/api/artist/lyra")
|
||||
|
||||
@@ -25,7 +25,7 @@ async def seeded_post(db):
|
||||
db.add(source)
|
||||
await db.flush()
|
||||
post = Post(
|
||||
source_id=source.id, external_post_id="API1",
|
||||
source_id=source.id, artist_id=artist.id, external_post_id="API1",
|
||||
post_title="Hello", post_url="https://p/alice-api/1",
|
||||
post_date=datetime.now(UTC),
|
||||
description="<p>hi</p>",
|
||||
@@ -123,7 +123,8 @@ async def post_timeline(db):
|
||||
posts = []
|
||||
for i in range(5):
|
||||
p = Post(
|
||||
source_id=source.id, external_post_id=f"TL{i}",
|
||||
source_id=source.id, artist_id=artist.id,
|
||||
external_post_id=f"TL{i}",
|
||||
post_title=f"post {i}", post_date=base + timedelta(days=i),
|
||||
)
|
||||
db.add(p)
|
||||
@@ -189,7 +190,7 @@ async def test_detail_returns_uncapped_thumbnails(client, db):
|
||||
db.add(s)
|
||||
await db.flush()
|
||||
p = Post(
|
||||
source_id=s.id, external_post_id="DETAIL10",
|
||||
source_id=s.id, artist_id=a.id, external_post_id="DETAIL10",
|
||||
post_title="big post", description="<p>body</p>",
|
||||
)
|
||||
db.add(p)
|
||||
|
||||
@@ -26,7 +26,8 @@ async def _seed_full(db):
|
||||
url="https://patreon.test/alice")
|
||||
db.add(source)
|
||||
await db.flush()
|
||||
post = Post(source_id=source.id, external_post_id="555",
|
||||
post = Post(source_id=source.id, artist_id=artist.id,
|
||||
external_post_id="555",
|
||||
post_url="https://patreon.test/p/555", post_title="Set 1",
|
||||
post_date=datetime(2023, 8, 1, tzinfo=UTC),
|
||||
description="<p>hi</p>", attachment_count=2)
|
||||
|
||||
@@ -25,7 +25,7 @@ async def _fixture(db):
|
||||
db.add(src)
|
||||
await db.flush()
|
||||
post = Post(
|
||||
source_id=src.id, external_post_id="p1",
|
||||
source_id=src.id, artist_id=artist.id, external_post_id="p1",
|
||||
post_date=datetime(2026, 3, 1, tzinfo=UTC),
|
||||
)
|
||||
db.add(post)
|
||||
|
||||
@@ -35,7 +35,7 @@ async def _post(db, artist_name, slug, ext):
|
||||
url=f"https://patreon.test/{slug}")
|
||||
db.add(s)
|
||||
await db.flush()
|
||||
p = Post(source_id=s.id, external_post_id=ext)
|
||||
p = Post(source_id=s.id, artist_id=a.id, external_post_id=ext)
|
||||
db.add(p)
|
||||
await db.flush()
|
||||
return a, s, p
|
||||
|
||||
@@ -152,7 +152,8 @@ async def _seed_image_with_post(
|
||||
db.add(source)
|
||||
await db.flush()
|
||||
post = Post(
|
||||
source_id=source.id, external_post_id=external_post_id,
|
||||
source_id=source.id, artist_id=artist.id,
|
||||
external_post_id=external_post_id,
|
||||
post_title="A Post", post_date=post_date,
|
||||
)
|
||||
db.add(post)
|
||||
|
||||
@@ -88,11 +88,36 @@ def test_find_or_create_post_idempotent(importer, artist_row, db_sync):
|
||||
)
|
||||
p1 = importer._find_or_create_post(
|
||||
source_id=src.id, external_post_id="ext-001",
|
||||
artist_id=artist_row.id,
|
||||
)
|
||||
p2 = importer._find_or_create_post(
|
||||
source_id=src.id, external_post_id="ext-001",
|
||||
artist_id=artist_row.id,
|
||||
)
|
||||
assert p1.id == p2.id
|
||||
assert p1.artist_id == artist_row.id
|
||||
|
||||
|
||||
def test_find_or_create_post_idempotent_with_null_source(
|
||||
importer, artist_row, db_sync,
|
||||
):
|
||||
"""Post.source_id is nullable since alembic 0030 — filesystem-imported
|
||||
posts with no live subscription have NULL source_id. The partial
|
||||
unique index `uq_post_artist_external_id_null_source` on
|
||||
(artist_id, external_post_id) WHERE source_id IS NULL guards the
|
||||
dedup; the helper matches on the same (artist_id, external_post_id)
|
||||
keys when source_id is None."""
|
||||
p1 = importer._find_or_create_post(
|
||||
source_id=None, external_post_id="fs-001",
|
||||
artist_id=artist_row.id,
|
||||
)
|
||||
p2 = importer._find_or_create_post(
|
||||
source_id=None, external_post_id="fs-001",
|
||||
artist_id=artist_row.id,
|
||||
)
|
||||
assert p1.id == p2.id
|
||||
assert p1.source_id is None
|
||||
assert p1.artist_id == artist_row.id
|
||||
|
||||
|
||||
def test_find_or_create_source_recovers_from_integrity_error(
|
||||
@@ -147,13 +172,13 @@ def test_find_or_create_source_recovers_from_integrity_error(
|
||||
assert recovered.id == pre_existing.id
|
||||
|
||||
|
||||
def test_source_for_sidecar_reuses_existing_subscription(
|
||||
def test_lookup_source_for_sidecar_returns_existing_subscription(
|
||||
importer, artist_row, db_sync,
|
||||
):
|
||||
"""The filesystem-import sidecar resolver should attach to whatever
|
||||
Source already exists for (artist, platform) — the canonical subscription
|
||||
Source — regardless of its URL. Without this, every imported post
|
||||
spawned its own Source row.
|
||||
"""The sidecar-import Source lookup returns the existing Source for
|
||||
(artist, platform) when one exists — letting filesystem-imported
|
||||
content attach to the artist's real subscription instead of being
|
||||
orphaned.
|
||||
"""
|
||||
canonical = Source(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
@@ -162,84 +187,30 @@ def test_source_for_sidecar_reuses_existing_subscription(
|
||||
db_sync.add(canonical)
|
||||
db_sync.flush()
|
||||
|
||||
resolved = importer._source_for_sidecar(
|
||||
resolved = importer._lookup_source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
assert resolved is not None
|
||||
assert resolved.id == canonical.id
|
||||
|
||||
|
||||
def test_source_for_sidecar_creates_synthetic_anchor_when_none_exists(
|
||||
def test_lookup_source_for_sidecar_returns_none_when_no_subscription(
|
||||
importer, artist_row, db_sync,
|
||||
):
|
||||
"""No subscription Source for this (artist, platform) yet. The helper
|
||||
creates one synthetic anchor (enabled=False, url='sidecar:<plat>:<slug>')
|
||||
so subsequent imports reuse it instead of spawning per-post Sources.
|
||||
"""Alembic 0030 made Post.source_id nullable; the importer no
|
||||
longer creates synthetic `sidecar:<platform>:<slug>` anchor rows
|
||||
when no real subscription exists. The lookup returns None and the
|
||||
caller carries that through as a NULL source_id on the new Post.
|
||||
"""
|
||||
resolved = importer._source_for_sidecar(
|
||||
resolved = importer._lookup_source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="pixiv",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
assert resolved.url == f"sidecar:pixiv:{artist_row.slug}"
|
||||
assert resolved.enabled is False
|
||||
assert resolved.artist_id == artist_row.id
|
||||
assert resolved.platform == "pixiv"
|
||||
assert resolved is None
|
||||
|
||||
# Second call returns the same row (no new Source spawned).
|
||||
again = importer._source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="pixiv",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
assert again.id == resolved.id
|
||||
|
||||
|
||||
def test_source_for_sidecar_distinct_platforms_distinct_anchors(
|
||||
importer, artist_row, db_sync,
|
||||
):
|
||||
"""One synthetic anchor per (artist, platform). Different platforms get
|
||||
different anchors even when no campaign Source exists for either.
|
||||
"""
|
||||
p = importer._source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
x = importer._source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="pixiv",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
assert p.id != x.id
|
||||
assert p.platform == "patreon"
|
||||
assert x.platform == "pixiv"
|
||||
|
||||
|
||||
def test_source_for_sidecar_prefers_real_over_synthetic_when_both_exist(
|
||||
importer, artist_row, db_sync,
|
||||
):
|
||||
"""When BOTH a synthetic anchor AND a real Source exist for the same
|
||||
(artist, platform), the resolver must return the REAL one. This is the
|
||||
fix for the 2026-05-31 phantom-subscription bug: alembic 0022 had
|
||||
rewritten an older per-post Source row into a sidecar synthetic, and
|
||||
the operator later added the real subscription. The old `ORDER BY
|
||||
id ASC LIMIT 1` lookup picked the older synthetic (lower id),
|
||||
silently attaching every gallery-dl download to the wrong Source.
|
||||
"""
|
||||
synthetic = Source(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
url=f"sidecar:patreon:{artist_row.slug}", enabled=False,
|
||||
)
|
||||
db_sync.add(synthetic)
|
||||
db_sync.flush()
|
||||
real = Source(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
url="https://www.patreon.com/testartist", enabled=True,
|
||||
)
|
||||
db_sync.add(real)
|
||||
db_sync.flush()
|
||||
assert synthetic.id < real.id # ordering precondition
|
||||
|
||||
resolved = importer._source_for_sidecar(
|
||||
artist_id=artist_row.id, platform="patreon",
|
||||
artist_slug=artist_row.slug,
|
||||
)
|
||||
assert resolved.id == real.id
|
||||
assert resolved.url == "https://www.patreon.com/testartist"
|
||||
# Verify no Source row was created as a side effect.
|
||||
count = db_sync.execute(
|
||||
select(Source).where(
|
||||
Source.artist_id == artist_row.id, Source.platform == "pixiv",
|
||||
)
|
||||
).all()
|
||||
assert count == []
|
||||
|
||||
@@ -17,7 +17,7 @@ async def _post(db, **post_kwargs):
|
||||
db.add(src)
|
||||
await db.flush()
|
||||
post = Post(
|
||||
source_id=src.id, external_post_id="p1",
|
||||
source_id=src.id, artist_id=artist.id, external_post_id="p1",
|
||||
post_date=datetime(2026, 3, 1, tzinfo=UTC),
|
||||
**post_kwargs,
|
||||
)
|
||||
|
||||
@@ -62,7 +62,7 @@ async def _run_backfill(db):
|
||||
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")
|
||||
post = Post(source_id=s.id, artist_id=a.id, external_post_id="1")
|
||||
db.add(post)
|
||||
await db.flush()
|
||||
rec.primary_post_id = post.id
|
||||
@@ -78,7 +78,7 @@ async def test_backfill_primary_post(db):
|
||||
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")
|
||||
post = Post(source_id=s.id, artist_id=a.id, external_post_id="2")
|
||||
db.add(post)
|
||||
await db.flush()
|
||||
db.add(ImageProvenance(image_record_id=rec.id, post_id=post.id,
|
||||
|
||||
@@ -7,6 +7,7 @@ boundary, and the thumbnails/attachments composition.
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
|
||||
from backend.app.models import (
|
||||
Artist,
|
||||
@@ -59,8 +60,13 @@ async def _seed_post(
|
||||
db, source_id: int, *, external_id: str,
|
||||
post_date=None, downloaded_at=None, title=None, description=None,
|
||||
):
|
||||
# Post.artist_id is NOT NULL since alembic 0030; look it up from
|
||||
# the source so callsites don't have to thread the artist through.
|
||||
artist_id = (await db.execute(
|
||||
select(Source.artist_id).where(Source.id == source_id)
|
||||
)).scalar_one()
|
||||
p = Post(
|
||||
source_id=source_id, external_post_id=external_id,
|
||||
source_id=source_id, artist_id=artist_id, external_post_id=external_id,
|
||||
post_title=title, post_date=post_date,
|
||||
description=description,
|
||||
)
|
||||
|
||||
@@ -37,7 +37,7 @@ async def _seed_post(db, *, artist_name, slug, platform, ext_id,
|
||||
db.add(source)
|
||||
await db.flush()
|
||||
post = Post(
|
||||
source_id=source.id, external_post_id=ext_id,
|
||||
source_id=source.id, artist_id=artist.id, external_post_id=ext_id,
|
||||
post_url=f"https://{platform}.test/p/{ext_id}",
|
||||
post_title=title, post_date=datetime(2023, 8, 1, tzinfo=UTC),
|
||||
description=desc, attachment_count=count,
|
||||
|
||||
@@ -138,7 +138,7 @@ async def test_update_changes_fields(db):
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_hides_sidecar_synthetic_anchors(db):
|
||||
"""Filesystem-import synthetic Sources (url='sidecar:<platform>:<slug>',
|
||||
enabled=False — see importer._source_for_sidecar) used to leak into the
|
||||
enabled=False — historical pre-alembic-0030 artifact) used to leak into the
|
||||
Subscriptions UI as phantom subscriptions because list() didn't filter
|
||||
them. They aren't pollable feeds; hide by default."""
|
||||
artist = await _artist(db, "Alice")
|
||||
|
||||
Reference in New Issue
Block a user