"""Update last_name field in User model

Revision ID: 7a5747dc773f
Revises: c243d6a1843d
Create Date: 2024-03-19 10:15:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
from sqlalchemy.sql import text


# revision identifiers, used by Alembic.
revision = '7a5747dc773f'
down_revision = 'c243d6a1843d'
branch_labels = None
depends_on = None


def upgrade():
    # First, update any null last_name values to '(You)'
    connection = op.get_bind()
    connection.execute(
        text("""
        UPDATE "user"
        SET last_name = '(You)'
        WHERE last_name IS NULL
        """)
    )
    
    # Then make the column non-nullable
    op.alter_column('user', 'last_name',
               existing_type=sa.String(length=150),
               nullable=False,
               server_default='(You)')


def downgrade():
    op.alter_column('user', 'last_name',
               existing_type=sa.String(length=150),
               nullable=True,
               server_default=None)
