"""Add archive_git_path column to module provider

Revision ID: f9a80ea383cc
Revises: 9dbcb4240c55
Create Date: 2024-03-14 06:05:26.867657

"""
from alembic import op
import sqlalchemy as sa
from terrareg.alembic.versions import Enum, update_enum

# revision identifiers, used by Alembic.
revision = 'f9a80ea383cc'
down_revision = '9dbcb4240c55'
branch_labels = None
depends_on = None

old_audit_values = [
    'NAMESPACE_CREATE', 'NAMESPACE_MODIFY_NAME', 'NAMESPACE_MODIFY_DISPLAY_NAME', 'MODULE_PROVIDER_CREATE', 'MODULE_PROVIDER_DELETE', 'MODULE_PROVIDER_UPDATE_GIT_TAG_FORMAT',
    'MODULE_PROVIDER_UPDATE_GIT_PROVIDER', 'MODULE_PROVIDER_UPDATE_GIT_PATH', 'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_BASE_URL',
    'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_CLONE_URL', 'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_BROWSE_URL', 'MODULE_PROVIDER_UPDATE_VERIFIED',
    'MODULE_VERSION_INDEX', 'MODULE_VERSION_PUBLISH', 'MODULE_VERSION_DELETE', 'USER_GROUP_CREATE', 'USER_GROUP_DELETE', 'USER_GROUP_NAMESPACE_PERMISSION_ADD',
    'USER_GROUP_NAMESPACE_PERMISSION_MODIFY', 'USER_GROUP_NAMESPACE_PERMISSION_DELETE', 'USER_LOGIN',
    'MODULE_PROVIDER_UPDATE_NAMESPACE', 'MODULE_PROVIDER_UPDATE_MODULE_NAME', 'MODULE_PROVIDER_UPDATE_PROVIDER_NAME', 'NAMESPACE_DELETE', 'MODULE_PROVIDER_REDIRECT_DELETE',
    'GPG_KEY_CREATE', 'GPG_KEY_DELETE', 'PROVIDER_CREATE', 'PROVIDER_DELETE', 'PROVIDER_VERSION_INDEX', 'PROVIDER_VERSION_DELETE', 'REPOSITORY_CREATE', 'REPOSITORY_UPDATE', 'REPOSITORY_DELETE',
]

new_audit_values = [
    'MODULE_PROVIDER_UPDATE_ARCHIVE_GIT_PATH',
]

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('module_provider', schema=None) as batch_op:
        batch_op.add_column(sa.Column('archive_git_path', sa.Boolean(), nullable=True))
    with op.batch_alter_table('module_version', schema=None) as batch_op:
        batch_op.add_column(sa.Column('archive_git_path', sa.Boolean(), nullable=True))
        batch_op.add_column(sa.Column('git_path', sa.String(length=1024), nullable=True))

    bind = op.get_bind()
    module_provider_paths = bind.execute("""SELECT id, git_path FROM module_provider""")
    for module_provider_id, git_path in module_provider_paths:
        bind.execute(
            sa.sql.text(
                """
                UPDATE module_version
                SET git_path=:git_path
                WHERE module_provider_id=:module_provider_id"""
            ),
            git_path=git_path,
            module_provider_id=module_provider_id,
        )

    update_enum(
        'audit_history', 'action', 'auditaction',
        old_audit_values,
        old_audit_values + new_audit_values
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    update_enum(
        'audit_history', 'action', 'auditaction',
        old_audit_values + new_audit_values,
        old_audit_values
    )

    with op.batch_alter_table('module_provider', schema=None) as batch_op:
        batch_op.drop_column('archive_git_path')
    with op.batch_alter_table('module_version', schema=None) as batch_op:
        batch_op.drop_column('archive_git_path')
        batch_op.drop_column('git_path')
    # ### end Alembic commands ###
