"""assign legacy source ids to measured token events

Revision ID: w0r1s2t3u4v5
Revises: v9q0r1s2t3u4
Create Date: 2026-05-23
"""
from alembic import op

revision = "w0r1s2t3u4v5"
down_revision = "v9q0r1s2t3u4"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute(
        """
        UPDATE token_events
        SET source_id = source_provider || ':legacy:' || id::text
        WHERE measurement_kind = 'measured'
          AND source_id IS NULL
        """
    )


def downgrade() -> None:
    op.execute(
        """
        UPDATE token_events
        SET source_id = NULL
        WHERE source_id = source_provider || ':legacy:' || id::text
        """
    )
