"""add_pipeline_info

Revision ID: b35c3db83d09
Revises: d28f2004b072
Create Date: 2025-05-15 15:58:05.179877

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

# revision identifiers, used by Alembic.
revision = 'b35c3db83d09'
down_revision = '2adcbe1f5dfb'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('pipeline_built_in_templates',
    sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('pipeline_id', models.types.StringUUID(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.Text(), nullable=False),
    sa.Column('icon', sa.JSON(), nullable=False),
    sa.Column('copyright', sa.String(length=255), nullable=False),
    sa.Column('privacy_policy', sa.String(length=255), nullable=False),
    sa.Column('position', sa.Integer(), nullable=False),
    sa.Column('install_count', sa.Integer(), nullable=False),
    sa.Column('language', sa.String(length=255), nullable=False),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.PrimaryKeyConstraint('id', name='pipeline_built_in_template_pkey')
    )
    op.create_table('pipeline_customized_templates',
    sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('tenant_id', models.types.StringUUID(), nullable=False),
    sa.Column('pipeline_id', models.types.StringUUID(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.Text(), nullable=False),
    sa.Column('icon', sa.JSON(), nullable=False),
    sa.Column('position', sa.Integer(), nullable=False),
    sa.Column('install_count', sa.Integer(), nullable=False),
    sa.Column('language', sa.String(length=255), nullable=False),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.PrimaryKeyConstraint('id', name='pipeline_customized_template_pkey')
    )
    with op.batch_alter_table('pipeline_customized_templates', schema=None) as batch_op:
        batch_op.create_index('pipeline_customized_template_tenant_idx', ['tenant_id'], unique=False)

    op.create_table('pipelines',
    sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('tenant_id', models.types.StringUUID(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('description', sa.Text(), server_default=sa.text("''::character varying"), nullable=False),
    sa.Column('mode', sa.String(length=255), nullable=False),
    sa.Column('workflow_id', models.types.StringUUID(), nullable=True),
    sa.Column('is_public', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('is_published', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('created_by', models.types.StringUUID(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.Column('updated_by', models.types.StringUUID(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
    sa.PrimaryKeyConstraint('id', name='pipeline_pkey')
    )
    op.create_table('tool_builtin_datasource_providers',
    sa.Column('id', models.types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('tenant_id', models.types.StringUUID(), nullable=True),
    sa.Column('user_id', models.types.StringUUID(), nullable=False),
    sa.Column('provider', sa.String(length=256), nullable=False),
    sa.Column('encrypted_credentials', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP(0)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP(0)'), nullable=False),
    sa.PrimaryKeyConstraint('id', name='tool_builtin_datasource_provider_pkey'),
    sa.UniqueConstraint('tenant_id', 'provider', name='unique_builtin_datasource_provider')
    )

    with op.batch_alter_table('datasets', schema=None) as batch_op:
        batch_op.add_column(sa.Column('keyword_number', sa.Integer(), server_default=sa.text('10'), nullable=True))
        batch_op.add_column(sa.Column('icon_info', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
        batch_op.add_column(sa.Column('runtime_mode', sa.String(length=255), server_default=sa.text("'general'::character varying"), nullable=True))
        batch_op.add_column(sa.Column('pipeline_id', models.types.StringUUID(), nullable=True))
        batch_op.add_column(sa.Column('chunk_structure', sa.String(length=255), nullable=True))

    with op.batch_alter_table('workflows', schema=None) as batch_op:
        batch_op.add_column(sa.Column('rag_pipeline_variables', sa.Text(), server_default='{}', nullable=False))

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('workflows', schema=None) as batch_op:
        batch_op.drop_column('rag_pipeline_variables')

    with op.batch_alter_table('datasets', schema=None) as batch_op:
        batch_op.drop_column('chunk_structure')
        batch_op.drop_column('pipeline_id')
        batch_op.drop_column('runtime_mode')
        batch_op.drop_column('icon_info')
        batch_op.drop_column('keyword_number')

    op.drop_table('tool_builtin_datasource_providers')
    op.drop_table('pipelines')
    with op.batch_alter_table('pipeline_customized_templates', schema=None) as batch_op:
        batch_op.drop_index('pipeline_customized_template_tenant_idx')

    op.drop_table('pipeline_customized_templates')
    op.drop_table('pipeline_built_in_templates')
    # ### end Alembic commands ###
