"""change keys structure with iv, salt, version and cypher

Revision ID: 1098b7a5eabc
Revises: 661ff8ef4425
Create Date: 2025-09-04 19:48:43.467378

"""
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 = '1098b7a5eabc'
down_revision: Union[str, None] = '661ff8ef4425'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    op.execute("TRUNCATE TABLE keys")
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('keys', sa.Column('ciphertext', sa.LargeBinary(), nullable=False))
    op.add_column('keys', sa.Column('iv', sa.LargeBinary(), nullable=False))
    op.add_column('keys', sa.Column('salt', sa.LargeBinary(), nullable=False))
    op.add_column('keys', sa.Column('key_version', sa.String(), nullable=False))
    op.alter_column('keys', 'owner_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.alter_column('keys', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.drop_column('keys', 'encrypted_key')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('keys', sa.Column('encrypted_key', sa.VARCHAR(), autoincrement=False, nullable=False))
    op.alter_column('keys', 'created_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('keys', 'owner_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_column('keys', 'key_version')
    op.drop_column('keys', 'salt')
    op.drop_column('keys', 'iv')
    op.drop_column('keys', 'ciphertext')
    # ### end Alembic commands ###
