"""add-quota_scope_id

Revision ID: 5fbd368d12a2
Revises: 85984c98b90f
Create Date: 2023-03-29 07:29:27.592211

"""

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

# revision identifiers, used by Alembic.
revision = "5fbd368d12a2"
down_revision = "85984c98b90f"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("vfolders", sa.Column("quota_scope_id", sa.String(length=64), nullable=True))
    op.execute(
        text(
            "UPDATE vfolders "
            "SET quota_scope_id = REPLACE(\"group\"::text, '-', '') "
            "WHERE ownership_type = 'group';"
        )
    )
    op.execute(
        text(
            "UPDATE vfolders "
            "SET quota_scope_id = REPLACE(\"user\"::text, '-', '') "
            "WHERE ownership_type = 'user';"
        )
    )
    op.alter_column("vfolders", "quota_scope_id", nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("vfolders", "quota_scope_id")
    # ### end Alembic commands ###
