perf(gallery): materialize indexed effective_date sort key
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>
This commit is contained in:
@@ -17,6 +17,7 @@ async def _seed(db, count: int = 3):
|
||||
origin="imported_filesystem", integrity_status="unknown",
|
||||
)
|
||||
r.created_at = base - timedelta(minutes=i)
|
||||
r.effective_date = r.created_at # no post → tracks created_at (0035)
|
||||
db.add(r)
|
||||
await db.flush()
|
||||
await db.commit()
|
||||
|
||||
@@ -14,6 +14,7 @@ async def _img(db, n):
|
||||
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
|
||||
|
||||
@@ -22,6 +22,7 @@ async def _img(db, n):
|
||||
origin="imported_filesystem", integrity_status="unknown",
|
||||
)
|
||||
rec.created_at = base - timedelta(minutes=n)
|
||||
rec.effective_date = rec.created_at # no primary post → tracks created_at (0035)
|
||||
db.add(rec)
|
||||
await db.flush()
|
||||
return rec
|
||||
|
||||
@@ -32,6 +32,8 @@ async def _seed_images(db, count: int, sha_prefix: str = "0") -> list[ImageRecor
|
||||
integrity_status="unknown",
|
||||
)
|
||||
r.created_at = base - timedelta(minutes=i)
|
||||
# No post → effective_date == created_at (alembic 0035 denorm).
|
||||
r.effective_date = r.created_at
|
||||
db.add(r)
|
||||
records.append(r)
|
||||
await db.flush()
|
||||
@@ -166,6 +168,9 @@ async def _seed_image_with_post(
|
||||
primary_post_id=post.id,
|
||||
)
|
||||
img.created_at = image_created_at
|
||||
# Mirror the importer's denorm (alembic 0035): a linked post with a date
|
||||
# sets effective_date to that date, else it falls back to created_at.
|
||||
img.effective_date = post_date or image_created_at
|
||||
db.add(img)
|
||||
await db.flush()
|
||||
return img, post
|
||||
@@ -201,6 +206,7 @@ async def test_scroll_sorts_by_post_date_when_available(db):
|
||||
origin="imported_filesystem", integrity_status="unknown",
|
||||
)
|
||||
img_c.created_at = base_import - timedelta(days=5)
|
||||
img_c.effective_date = img_c.created_at # no post → tracks created_at
|
||||
db.add(img_c)
|
||||
await db.flush()
|
||||
|
||||
|
||||
@@ -94,6 +94,9 @@ def test_sidecar_creates_provenance(importer, import_layout):
|
||||
prov = importer.session.execute(select(ImageProvenance)).scalar_one()
|
||||
assert prov.image_record_id == rec.id and prov.post_id == post.id
|
||||
assert rec.primary_post_id == post.id
|
||||
# Denormalized gallery sort key (alembic 0035) tracks the primary post's
|
||||
# publish date so /scroll orders off ix_image_record_effective_date.
|
||||
assert rec.effective_date == post.post_date
|
||||
|
||||
|
||||
def test_reimport_same_post_idempotent(importer, import_layout):
|
||||
|
||||
Reference in New Issue
Block a user