"""add metadata column for datasets

Revision ID: c1a0b0ee712e
Revises: 5a5105707147
Create Date: 2022-05-20 14:37:59.155025

"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "c1a0b0ee712e"
down_revision = "5a5105707147"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("base_dataset", sa.Column("metadata", sa.JSON(), nullable=True))

    op.execute(sa.text("UPDATE base_dataset SET metadata = extra::jsonb->'metadata'"))
    op.execute(sa.text("UPDATE base_dataset SET extra = (extra::jsonb - 'metadata')"))
    op.execute(sa.text("UPDATE base_dataset SET metadata = '{}'::json WHERE metadata IS NULL"))

    op.alter_column("base_dataset", "metadata", nullable=False)

    # ### end Alembic commands ###


def downgrade():
    raise RuntimeError("Cannot downgrade")
