"""adding fk on owner_shortname

Revision ID: 91c94250232a
Revises: f7a4949eed19
Create Date: 2025-02-11 15:11:21.817512

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel
import sqlmodel.sql.sqltypes


# revision identifiers, used by Alembic.
revision: str = '91c94250232a'
down_revision: Union[str, None] = 'f7a4949eed19'
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! ###
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_unique_constraint(None, ['shortname'])
        # batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    with op.batch_alter_table('attachments', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    with op.batch_alter_table('entries', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    with op.batch_alter_table('permissions', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    with op.batch_alter_table('roles', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    with op.batch_alter_table('spaces', schema=None) as batch_op:
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.create_foreign_key(None, 'users', ['owner_shortname'], ['shortname'])

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='unique')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('spaces', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('roles', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('permissions', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('entries', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('attachments', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.alter_column('owner_shortname',
               existing_type=sa.VARCHAR(),
               nullable=True)

    # ### end Alembic commands ###
