"""Make Checkout.organization_id non-nullable

Revision ID: 08e659ee3871
Revises: 310d1f7742d7
Create Date: 2025-10-29 15:19:35.561721

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "08e659ee3871"
down_revision = "310d1f7742d7"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute(
        """
        UPDATE checkouts
        SET organization_id = products.organization_id
        FROM products
        WHERE checkouts.organization_id IS NULL
        AND checkouts.product_id = products.id
        """
    )
    op.alter_column(
        "checkouts", "organization_id", existing_type=sa.UUID(), nullable=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "checkouts", "organization_id", existing_type=sa.UUID(), nullable=True
    )
    # ### end Alembic commands ###
