"""create dataset versions

Revision ID: 18b0dde1883b
Revises: 5e13ff24289d
Create Date: 2025-10-13 13:34:50.346814

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "18b0dde1883b"
down_revision = "5e13ff24289d"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "dataset_versions",
        sa.Column("version_number", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.id"],
        ),
        sa.PrimaryKeyConstraint("version_number", "dataset_id"),
    )
    op.create_table(
        "dataset_version_rows",
        sa.Column("version_number", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("data", postgresql.JSON(astext_type=sa.Text()), nullable=False),
        sa.ForeignKeyConstraint(
            ["version_number", "dataset_id"],
            ["dataset_versions.version_number", "dataset_versions.dataset_id"],
        ),
        sa.PrimaryKeyConstraint("version_number", "id"),
    )
    op.create_index(
        "ix_dataset_version_rows_dataset_version",
        "dataset_version_rows",
        ["dataset_id", "version_number"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        "ix_dataset_version_rows_dataset_version",
        table_name="dataset_version_rows",
    )
    op.drop_table("dataset_version_rows")
    op.drop_table("dataset_versions")
    # ### end Alembic commands ###
