e05e0b9f37
The gallery cursored on COALESCE(post.post_date, image_record.created_at) across the Post outer join — an expression spanning two tables that no index can serve, so every /scroll sorted a large slice of the library (and the old frontend fired ten serially). Materialize it: - image_record.effective_date column + ix_image_record_effective_date (effective_date DESC, id DESC); alembic 0035 backfills COALESCE(primary post's post_date, created_at) for existing rows. - gallery_service._effective_date_col() now returns the column, so scroll / timeline / jump / neighbors all order off the index instead of re-deriving the COALESCE. _neighbors reads record.effective_date directly (drops an extra Post lookup). - importer._apply_sidecar maintains it: when a primary post with a date is linked, effective_date = post.post_date; plain inserts keep the created_at-equivalent server default. Tests: sidecar import asserts effective_date == post.post_date; gallery ordering/timeline/jump test seeds set effective_date alongside created_at. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from datetime import UTC, datetime, timedelta
|
|
|
|
import pytest
|
|
|
|
from backend.app.models import Artist, ImageRecord, Source
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
async def _img(db, n):
|
|
rec = ImageRecord(
|
|
path=f"/images/ap/{n}.jpg", sha256=f"ap{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)
|
|
rec.effective_date = rec.created_at # no post → tracks created_at (0035)
|
|
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://patreon.test/{slug}")
|
|
db.add(s)
|
|
await db.flush()
|
|
return a, s
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_scroll_artist_null_when_none(client, db):
|
|
rec = await _img(db, 1)
|
|
await db.commit()
|
|
resp = await client.get("/api/gallery/scroll?limit=10")
|
|
body = await resp.get_json()
|
|
item = next(i for i in body["images"] if i["id"] == rec.id)
|
|
assert item["artist"] is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_scroll_artist_via_artist_id(client, db):
|
|
# FC-2d-vii-c: the canonical resolution path is image_record.artist_id
|
|
# (covers folder-style images with no provenance too).
|
|
rec = await _img(db, 1)
|
|
a, _ = await _artist_source(db, "Folder", "folder")
|
|
rec.artist_id = a.id
|
|
await db.flush()
|
|
await db.commit()
|
|
|
|
resp = await client.get("/api/gallery/scroll?limit=10")
|
|
assert resp.status_code == 200
|
|
body = await resp.get_json()
|
|
item = next(i for i in body["images"] if i["id"] == rec.id)
|
|
assert item["artist"] == {"name": "Folder", "slug": "folder"}
|