"""Backfill label + metadata for calendar relations.

Removes calendar relations with no metadata slug and no label,
which are either orphaned or were created before metadata was tracked.

Revision ID: relations_0003
Revises: relations_0002
Create Date: 2026-02-28
"""

import sqlalchemy as sa
from alembic import op

revision = "relations_0003"
down_revision = "relations_0002"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("""
        DELETE FROM container_relations
        WHERE child_type IN ('calendar', 'market')
          AND (metadata IS NULL OR metadata->>'slug' IS NULL)
          AND label IS NULL
    """)


def downgrade() -> None:
    pass
