"""Cloud Changes

Revision ID: cb2029aadc2d
Revises: d8bc71595932
Create Date: 2024-08-29 19:24:34.859544

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa # noqa: F401
import sqlmodel # noqa: F401


# revision identifiers, used by Alembic.
revision: str = 'cb2029aadc2d'
down_revision: Union[str, None] = 'd8bc71595932'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('activity', 'course_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.drop_constraint('activity_org_id_fkey', 'activity', type_='foreignkey')
    op.create_foreign_key(None, 'activity', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('block_org_id_fkey', 'block', type_='foreignkey')
    op.create_foreign_key(None, 'block', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.alter_column('collection', 'org_id',
               existing_type=sa.INTEGER(),
               type_=sa.BigInteger(),
               existing_nullable=True)
    op.drop_constraint('collection_org_id_fkey', 'collection', type_='foreignkey')
    op.create_foreign_key(None, 'collection', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.alter_column('collectioncourse', 'collection_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.alter_column('collectioncourse', 'course_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.drop_constraint('course_org_id_fkey', 'course', type_='foreignkey')
    op.create_foreign_key(None, 'course', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.alter_column('coursechapter', 'course_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.alter_column('coursechapter', 'chapter_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.drop_constraint('resourceauthor_user_id_fkey', 'resourceauthor', type_='foreignkey')
    op.create_foreign_key(None, 'resourceauthor', 'user', ['user_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('role_org_id_fkey', 'role', type_='foreignkey')
    op.create_foreign_key(None, 'role', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint('trailrun_user_id_fkey', 'trailrun', type_='foreignkey')
    op.drop_constraint('trailrun_course_id_fkey', 'trailrun', type_='foreignkey')
    op.drop_constraint('trailrun_trail_id_fkey', 'trailrun', type_='foreignkey')
    op.drop_constraint('trailrun_org_id_fkey', 'trailrun', type_='foreignkey')
    op.create_foreign_key(None, 'trailrun', 'user', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailrun', 'course', ['course_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailrun', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailrun', 'trail', ['trail_id'], ['id'], ondelete='CASCADE')
    op.alter_column('trailstep', 'trailrun_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    op.drop_constraint('trailstep_activity_id_fkey', 'trailstep', type_='foreignkey')
    op.drop_constraint('trailstep_org_id_fkey', 'trailstep', type_='foreignkey')
    op.drop_constraint('trailstep_course_id_fkey', 'trailstep', type_='foreignkey')
    op.drop_constraint('trailstep_user_id_fkey', 'trailstep', type_='foreignkey')
    op.drop_constraint('trailstep_trail_id_fkey', 'trailstep', type_='foreignkey')
    op.create_foreign_key(None, 'trailstep', 'organization', ['org_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailstep', 'user', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailstep', 'trail', ['trail_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailstep', 'course', ['course_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'trailstep', 'activity', ['activity_id'], ['id'], ondelete='CASCADE')
    op.alter_column('userorganization', 'org_id',
               existing_type=sa.BIGINT(),
               type_=sa.Integer(),
               existing_nullable=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('userorganization', 'org_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.drop_constraint(None, 'trailstep', type_='foreignkey')
    op.drop_constraint(None, 'trailstep', type_='foreignkey')
    op.drop_constraint(None, 'trailstep', type_='foreignkey')
    op.drop_constraint(None, 'trailstep', type_='foreignkey')
    op.drop_constraint(None, 'trailstep', type_='foreignkey')
    op.create_foreign_key('trailstep_trail_id_fkey', 'trailstep', 'trail', ['trail_id'], ['id'])
    op.create_foreign_key('trailstep_user_id_fkey', 'trailstep', 'user', ['user_id'], ['id'])
    op.create_foreign_key('trailstep_course_id_fkey', 'trailstep', 'course', ['course_id'], ['id'])
    op.create_foreign_key('trailstep_org_id_fkey', 'trailstep', 'organization', ['org_id'], ['id'])
    op.create_foreign_key('trailstep_activity_id_fkey', 'trailstep', 'activity', ['activity_id'], ['id'])
    op.alter_column('trailstep', 'trailrun_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.drop_constraint(None, 'trailrun', type_='foreignkey')
    op.drop_constraint(None, 'trailrun', type_='foreignkey')
    op.drop_constraint(None, 'trailrun', type_='foreignkey')
    op.drop_constraint(None, 'trailrun', type_='foreignkey')
    op.create_foreign_key('trailrun_org_id_fkey', 'trailrun', 'organization', ['org_id'], ['id'])
    op.create_foreign_key('trailrun_trail_id_fkey', 'trailrun', 'trail', ['trail_id'], ['id'])
    op.create_foreign_key('trailrun_course_id_fkey', 'trailrun', 'course', ['course_id'], ['id'])
    op.create_foreign_key('trailrun_user_id_fkey', 'trailrun', 'user', ['user_id'], ['id'])
    op.drop_constraint(None, 'role', type_='foreignkey')
    op.create_foreign_key('role_org_id_fkey', 'role', 'organization', ['org_id'], ['id'])
    op.drop_constraint(None, 'resourceauthor', type_='foreignkey')
    op.create_foreign_key('resourceauthor_user_id_fkey', 'resourceauthor', 'user', ['user_id'], ['id'])
    op.alter_column('coursechapter', 'chapter_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.alter_column('coursechapter', 'course_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.drop_constraint(None, 'course', type_='foreignkey')
    op.create_foreign_key('course_org_id_fkey', 'course', 'organization', ['org_id'], ['id'])
    op.alter_column('collectioncourse', 'course_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.alter_column('collectioncourse', 'collection_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    op.drop_constraint(None, 'collection', type_='foreignkey')
    op.create_foreign_key('collection_org_id_fkey', 'collection', 'organization', ['org_id'], ['id'])
    op.alter_column('collection', 'org_id',
               existing_type=sa.BigInteger(),
               type_=sa.INTEGER(),
               existing_nullable=True)
    op.drop_constraint(None, 'block', type_='foreignkey')
    op.create_foreign_key('block_org_id_fkey', 'block', 'organization', ['org_id'], ['id'])
    op.drop_constraint(None, 'activity', type_='foreignkey')
    op.create_foreign_key('activity_org_id_fkey', 'activity', 'organization', ['org_id'], ['id'])
    op.alter_column('activity', 'course_id',
               existing_type=sa.Integer(),
               type_=sa.BIGINT(),
               existing_nullable=True)
    # ### end Alembic commands ###
