"""batched invitation send

Revision ID: e1f4a90c73bd
Revises: d5e8b31047af
Create Date: 2026-08-16 10:20:00.000000

Groups the message jobs produced by one batched send from the guest list
(add-bulk-invitation-send D5).

One nullable column and its index, and every row that already exists reads correctly under
the new code without being touched:

* `batch_id` null means "not part of a batch", which is true of every job ever inserted
  before this: scheduled waves, the audience bulk send, and single manual sends. Nothing is
  backfilled, and nothing needs to be — a batch is only ever read by asking for one specific
  id, so rows without one are simply never returned.
* No foreign key and no batch table. A batch is a set of jobs sharing this value; giving it a
  parent row would mean a second source of truth for something the jobs already say, and the
  first thing that would go wrong is a batch row surviving jobs that CASCADE away with their
  invitation.
* The value also rides inside `idempotency_key` (`invitation:batch-<id>:channel`), so the
  unique constraint that already exists is what makes a duplicated send request insert
  nothing twice. This column adds grouping, not the guarantee.

The deploy window is safe in both directions: an old API against the new schema simply never
writes the column, and a new API against the old schema is not a state this ordering
produces (migrate first, as every deploy here does).

The downgrade drops the grouping of past batches. The jobs themselves survive with their
status, provider ids, stored content and delivery receipts, so the message log stays
truthful about what was sent and when; what is lost is the ability to ask "how did that one
send go?" as a single question, and any progress view of a batch still in flight.
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "e1f4a90c73bd"
down_revision: str | None = "d5e8b31047af"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.add_column("message_job", sa.Column("batch_id", sa.Uuid(), nullable=True))
    op.create_index("ix_message_job_batch", "message_job", ["batch_id"])


def downgrade() -> None:
    op.drop_index("ix_message_job_batch", table_name="message_job")
    op.drop_column("message_job", "batch_id")
