"""image_record: source_url + source_filehash (inline-image localization)

#830 Phase 2. To render a post body faithfully we serve LOCAL copies of inline
images instead of hotlinking the public CDN. The join key between a body
`<img src=CDN>` and the local file is the CDN's 32-hex filehash (the same
identity extract_media dedups by). Persist it (indexed) plus the full source
URL for provenance/debugging. Both NULL for filesystem-imported / pre-existing
rows — those fall back to hotlinking until re-downloaded.

Revision ID: 0051
Revises: 0050
Create Date: 2026-06-14
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0051"
down_revision: Union[str, None] = "0050"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column("image_record", sa.Column("source_url", sa.Text(), nullable=True))
    op.add_column(
        "image_record", sa.Column("source_filehash", sa.String(length=32), nullable=True)
    )
    op.create_index(
        "ix_image_record_source_filehash", "image_record", ["source_filehash"]
    )


def downgrade() -> None:
    op.drop_index("ix_image_record_source_filehash", table_name="image_record")
    op.drop_column("image_record", "source_filehash")
    op.drop_column("image_record", "source_url")
