db: collapse alembic 0001..0087 into one baseline (milestone 328 step 1)
Build images / sign-extension (push) Successful in 4s
CI / lint (push) Successful in 4s
CI / extension-version (push) Successful in 5s
Build images / build-agent (push) Successful in 9s
CI / frontend-build (push) Successful in 24s
Build images / build-ml (push) Successful in 42s
CI / backend-lint-and-test (push) Successful in 53s
Build images / build-web (push) Successful in 33s
CI / integration (push) Failing after 3m47s
Build images / sign-extension (push) Successful in 4s
CI / lint (push) Successful in 4s
CI / extension-version (push) Successful in 5s
Build images / build-agent (push) Successful in 9s
CI / frontend-build (push) Successful in 24s
Build images / build-ml (push) Successful in 42s
CI / backend-lint-and-test (push) Successful in 53s
Build images / build-web (push) Successful in 33s
CI / integration (push) Failing after 3m47s
87 revisions narrating this project's build-out become one file that creates the schema in a single step. They cost nothing at runtime — all 86 upgrade steps ran in 0.2s (note #3260) — so this is a presentation change, not a performance one: a new installer should not inherit our development history to stand up a database. Deleted: 87 revisions (6,052 lines), the 10 tests/test_migration_*.py files (483 lines) that asserted intermediate states and backfills which no longer exist, and backend/app/utils/artist_backfill.py — the only live module a migration imported, with no other consumer anywhere. That last one satisfies the operator's separate request to inline it into 0008 and delete the module; the squash removes both outright. THE REVISION ID IS "0087", NOT "0001", ON PURPOSE. It is the id of the last revision collapsed, so an existing database is already at head and `alembic upgrade head` does nothing. The alternative is `alembic stamp` against live data, and stamp validates NOTHING — it writes a version string whether or not the schema matches, so a wrong baseline surfaces later, via the next real migration, with no clean way back. This removes that operation rather than making it safe. Future revisions run from 0088. Four things are hand-written because SQLAlchemy metadata does not carry them, and none fail at generation time: 1. CREATE EXTENSION vector — the VECTOR columns cannot be created without it, so it is ordered first in upgrade(). 2. CREATE EXTENSION tsm_system_rows — surfaces only when the random sample query runs. 3. the HNSW index on image_record.siglip_embedding, raw SQL because create_index cannot express USING hnsw (... vector_cosine_ops). The quietest of the four: everything works, similarity search just stops using an index. 4. import pgvector.sqlalchemy.vector — autogenerate EMITS pgvector.sqlalchemy.vector.VECTOR references without importing it, so the generated file dies with NameError on first run. The candidate came out of CI (run 4967) as checksummed base64 rather than a plain cat, because run 4964's cat was truncated mid-line inside a column definition with the step still green — 29 tables instead of 42, and it looked entirely plausible. Verified here: 56,582 bytes, sha256 471acfca69c0…, 42 tables, 66 indexes, 42 drops. NOT YET PROVEN against the old chain. baseline.yml does that, and it is step 2's gate; this commit does not claim the schemas match.
This commit is contained in:
@@ -1,137 +0,0 @@
|
||||
"""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, artist_id=a.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, 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,
|
||||
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
|
||||
Reference in New Issue
Block a user