#
# This file is part of Invenio.
# Copyright (C) 2016-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""PropertyValueACL."""

import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op

# revision identifiers, used by Alembic.
from invenio_explicit_acls.acls.propertyvalue_acls import BoolOperation, \
    MatchOperation

revision = 'd179cf16d686'
down_revision = '21ab0d49a6ca'
branch_labels = ()
depends_on = None


def upgrade():
    """Upgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('explicit_acls_propertyvalue',
    sa.Column('created', sa.DateTime(), nullable=False),
    sa.Column('updated', sa.DateTime(), nullable=False),
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('acl_id', sa.String(length=36), nullable=True),
    sa.Column('name', sa.String(length=64), nullable=True),
    sa.Column('value', sa.String(length=128), nullable=True),
    sa.Column('match_operation', sqlalchemy_utils.types.choice.ChoiceType(MatchOperation, impl=sa.String(length=10)), nullable=True),
    sa.Column('bool_operation', sqlalchemy_utils.types.choice.ChoiceType(BoolOperation, impl=sa.String(length=10)), nullable=True),
    sa.Column('originator_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['acl_id'], ['explicit_acls_propertyvalueacl.id'], name='fk_explicit_acls_propertyvalue_acl_id'),
    sa.ForeignKeyConstraint(['originator_id'], ['accounts_user.id'], name=op.f('fk_explicit_acls_propertyvalue_originator_id_accounts_user'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_propertyvalue'))
    )
    op.create_index(op.f('ix_explicit_acls_propertyvalue_originator_id'), 'explicit_acls_propertyvalue', ['originator_id'], unique=False)
    op.drop_column('explicit_acls_propertyvalueacl', 'property_name')
    op.drop_column('explicit_acls_propertyvalueacl', 'property_value')
    op.drop_column('explicit_acls_propertyvalueacl', 'match_operation')
    # ### end Alembic commands ###


def downgrade():
    """Downgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('explicit_acls_propertyvalueacl', sa.Column('match_operation', sa.VARCHAR(length=10), autoincrement=False, nullable=True))
    op.add_column('explicit_acls_propertyvalueacl', sa.Column('property_value', sa.VARCHAR(length=128), autoincrement=False, nullable=True))
    op.add_column('explicit_acls_propertyvalueacl', sa.Column('property_name', sa.VARCHAR(length=64), autoincrement=False, nullable=True))
    op.drop_index(op.f('ix_explicit_acls_propertyvalue_originator_id'), table_name='explicit_acls_propertyvalue')
    op.drop_table('explicit_acls_propertyvalue')
    # ### end Alembic commands ###
