"""machine type refactor

Revision ID: 0ad803c51325
Revises: fdcb5aa33b76
Create Date: 2025-12-22 13:46:51.857049

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '0ad803c51325'
down_revision: Union[str, Sequence[str], None] = 'fdcb5aa33b76'
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.add_column('k8s_tcp_proxies', sa.Column('is_k8s', sa.Boolean(), nullable=False, server_default='true'))
    op.add_column('kea_reservations', sa.Column('machine_type', sa.String(length=50), nullable=False, server_default='k8s_worker'))
    
    # remove defaults
    op.alter_column('k8s_tcp_proxies', 'is_k8s', server_default=None)
    op.alter_column('kea_reservations', 'machine_type', server_default=None)

    # set master type
    op.execute("""
        UPDATE kea_reservations kr
        SET machine_type = 'k8s_master'
        FROM k8s_masters km
        WHERE kr.ip = km.ip
    """)

    op.drop_table('k8s_masters')
    op.drop_table('k8s_workers')

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('kea_reservations', 'machine_type')
    op.drop_column('k8s_tcp_proxies', 'is_k8s')
    op.create_table('k8s_workers',
    sa.Column('ip', postgresql.INET(), autoincrement=False, nullable=False),
    sa.Column('hostname', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.Column('stack_fqdn', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.Column('proxy_stack_fqdn', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('ip', name=op.f('k8s_workers_pkey'))
    )
    op.create_table('k8s_masters',
    sa.Column('ip', postgresql.INET(), autoincrement=False, nullable=False),
    sa.Column('hostname', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.Column('stack_fqdn', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.Column('proxy_stack_fqdn', sa.VARCHAR(length=253), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('ip', name=op.f('k8s_masters_pkey'))
    )
    # ### end Alembic commands ###
