"""add_unix_permissions_and_acl_support

Revision ID: 777350ff28ce
Revises: 9c0780bb05c1
Create Date: 2025-10-19 09:42:02.290532

"""

from collections.abc import Sequence
from typing import Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "777350ff28ce"
down_revision: Union[str, Sequence[str], None] = "9c0780bb05c1"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "acl_entries",
        sa.Column("acl_id", sa.String(length=36), nullable=False),
        sa.Column("path_id", sa.String(length=36), nullable=False),
        sa.Column("entry_type", sa.String(length=20), nullable=False),
        sa.Column("identifier", sa.String(length=255), nullable=True),
        sa.Column("permissions", sa.String(length=10), nullable=False),
        sa.Column("deny", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["path_id"], ["file_paths.path_id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("acl_id"),
    )
    op.create_index("idx_acl_entries_path_id", "acl_entries", ["path_id"], unique=False)
    op.create_index(
        "idx_acl_entries_type_id", "acl_entries", ["entry_type", "identifier"], unique=False
    )
    op.add_column("file_paths", sa.Column("owner", sa.String(length=255), nullable=True))
    op.add_column("file_paths", sa.Column("group", sa.String(length=255), nullable=True))
    op.add_column("file_paths", sa.Column("mode", sa.Integer(), nullable=True))
    op.create_index("idx_file_paths_group", "file_paths", ["group"], unique=False)
    op.create_index("idx_file_paths_owner", "file_paths", ["owner"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("idx_file_paths_owner", table_name="file_paths")
    op.drop_index("idx_file_paths_group", table_name="file_paths")
    op.drop_column("file_paths", "mode")
    op.drop_column("file_paths", "group")
    op.drop_column("file_paths", "owner")
    op.drop_index("idx_acl_entries_type_id", table_name="acl_entries")
    op.drop_index("idx_acl_entries_path_id", table_name="acl_entries")
    op.drop_table("acl_entries")
    # ### end Alembic commands ###
