"""fix web crawler

Revision ID: 1a9ab9cfabf3
Revises: 06e99854de2c
Create Date: 2025-07-15 17:21:50.482735

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '1a9ab9cfabf3'
down_revision: Union[str, None] = '06e99854de2c'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('web_crawler_files',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('web_crawler_id', sa.Integer(), nullable=False),
    sa.Column('file_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['file_id'], ['files.id'], ),
    sa.ForeignKeyConstraint(['web_crawler_id'], ['web_crawlers.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.alter_column('web_crawlers', 'location_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.create_index(op.f('ix_web_crawler_files_id'), 'web_crawler_files', ['id'], unique=False)
    op.drop_constraint('web_crawlers_file_id_fkey', 'web_crawlers', type_='foreignkey')
    op.drop_constraint('web_crawlers_location_id_fkey', 'web_crawlers', type_='foreignkey')
    op.create_foreign_key(None, 'web_crawlers', 'locations', ['location_id'], ['id'])
    op.drop_column('web_crawlers', 'file_id')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('web_crawlers', sa.Column('file_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'web_crawlers', type_='foreignkey')
    op.create_foreign_key('web_crawlers_location_id_fkey', 'web_crawlers', 'locations', ['location_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key('web_crawlers_file_id_fkey', 'web_crawlers', 'files', ['file_id'], ['id'], ondelete='SET NULL')
    op.drop_index(op.f('ix_web_crawler_files_id'), table_name='web_crawler_files')
    op.alter_column('web_crawlers', 'location_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.drop_table('web_crawler_files')
    # ### end Alembic commands ###
