"""Extensions for more Catalog Types in Catalog Model

Revision ID: 7b7b566e667f
Revises: 28984b05d396
Create Date: 2024-10-21 07:39:52.260054

"""
from alembic import op
import sqlalchemy as sa
import pgvector
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '7b7b566e667f'
down_revision = '28984b05d396'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('catalog', sa.Column('parent_id', sa.Integer(), nullable=True))

    op.add_column('catalog', sa.Column('type', sa.String(length=50), nullable=False, server_default='DEFAULT'))
    # Update all existing rows to have the 'type' set to 'DEFAULT'
    op.execute("UPDATE catalog SET type='DEFAULT' WHERE type IS NULL")
    # Remove the server default (optional but recommended)
    op.alter_column('catalog', 'type', server_default=None)

    op.add_column('catalog', sa.Column('user_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.add_column('catalog', sa.Column('system_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.add_column('catalog', sa.Column('configuration', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.create_foreign_key(None, 'catalog', 'catalog', ['parent_id'], ['id'])

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'catalog', type_='foreignkey')
    op.drop_column('catalog', 'configuration')
    op.drop_column('catalog', 'system_metadata')
    op.drop_column('catalog', 'user_metadata')
    op.drop_column('catalog', 'type')
    op.drop_column('catalog', 'parent_id')
    # ### end Alembic commands ###
