"""add cached_path to addons_attachments

Revision ID: y6z7a8b9c0d1
Revises: x5y6z7a8b9c0
Create Date: 2026-03-20
"""
from alembic import op
import sqlalchemy as sa

revision = 'y6z7a8b9c0d1'
down_revision = 'x5y6z7a8b9c0'
branch_labels = None
depends_on = None


def upgrade():
    # Create the table if it doesn't exist yet (fresh installs never had it
    # created via Alembic; existing installs already have it from a manual
    # creation + a prior ADD COLUMN).
    from sqlalchemy import inspect
    bind = op.get_bind()
    if not inspect(bind).has_table('addons_attachments'):
        op.create_table(
            'addons_attachments',
            sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
            sa.Column('message_id', sa.Integer(), sa.ForeignKey('addons_messages.id'), nullable=False),
            sa.Column('provider_attachment_id', sa.String(100), nullable=True),
            sa.Column('filename', sa.String(512), nullable=False),
            sa.Column('mime_type', sa.String(100), nullable=True),
            sa.Column('file_size', sa.Integer(), nullable=True),
            sa.Column('provider_url', sa.Text(), nullable=False),
            sa.Column('created_at', sa.DateTime(), nullable=False),
            sa.Column('cached_path', sa.String(512), nullable=True),
        )
    else:
        op.add_column('addons_attachments', sa.Column('cached_path', sa.String(512), nullable=True))


def downgrade():
    op.drop_column('addons_attachments', 'cached_path')
