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:
@@ -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