"""receipt-id reorganized

Revision ID: f6f97ed9c053
Revises: 4fec22c35530
Create Date: 2023-07-02 14:16:06.260479

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'f6f97ed9c053'
down_revision = '4fec22c35530'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('receipt_item', schema=None) as batch_op:
        batch_op.add_column(sa.Column('accepted', sa.Boolean(), nullable=False))
        batch_op.drop_constraint("item_receipt_receipt_fkey")
        batch_op.alter_column('receipt',
               existing_type=sa.NUMERIC(precision=24, scale=0),
               type_=sa.BigInteger(),
               existing_nullable=False)
        
    with op.batch_alter_table('receipt', schema=None) as batch_op:
        batch_op.add_column(sa.Column('bonid', sa.Numeric(precision=24, scale=0), nullable=True))
        
    with op.batch_alter_table('receipt', schema=None) as batch_op:
        batch_op.execute("UPDATE receipt SET bonid=id;")
        batch_op.alter_column('id',
               existing_type=sa.NUMERIC(precision=24, scale=0),
               type_=sa.BigInteger(),
               existing_nullable=False,
               autoincrement=True)
        batch_op.create_unique_constraint(None, ['bonid'])

    with op.batch_alter_table('receipt_item', schema=None) as batch_op:
        batch_op.create_foreign_key(None, 'receipt', ['receipt'], ['id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('receipt_item', schema=None) as batch_op:
        batch_op.alter_column('receipt',
               existing_type=sa.BigInteger(),
               type_=sa.NUMERIC(precision=24, scale=0),
               existing_nullable=False)
        batch_op.drop_column('accepted')

    with op.batch_alter_table('receipt', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='unique')
        batch_op.alter_column('id',
               existing_type=sa.BigInteger(),
               type_=sa.NUMERIC(precision=24, scale=0),
               existing_nullable=False,
               autoincrement=True)
        batch_op.drop_column('bonid')

    # ### end Alembic commands ###
