"""v0.8.0 migration

Revision ID: 0158771b9f18
Revises: 85f474ec5db2
Create Date: 2025-01-13 15:01:01.131242

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '0158771b9f18'
down_revision: Union[str, None] = '85f474ec5db2'
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.add_column('users', sa.Column('units', sa.Integer(), comment='User units (one digit)(1 - metric, 2 - imperial)'))
    op.execute("UPDATE users SET units = 1 WHERE units IS NULL")
    op.alter_column('users', 'units', existing_type=sa.Integer(), nullable=False)
    op.execute("""
    INSERT INTO health_targets (user_id, weight)
    SELECT 1, NULL WHERE EXISTS (SELECT 1 FROM users WHERE id = 1);
    """)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("""
    DELETE FROM health_targets 
    WHERE user_id = 1;
    """)
    op.drop_column('users', 'units')
    # ### end Alembic commands ###
