#
# Copyright (c) 2019 UCT Prague.
# 
# ec5cbec43ec8_initial_layout.py is part of Invenio Explicit ACLs 
# (see https://github.com/oarepo/invenio-explicit-acls).
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

"""Initial layout."""

import sqlalchemy as sa
from alembic import op

try:
    from psycopg2 import apilevel
    from sqlalchemy.dialects.postgresql import JSONB as JSON
    from sqlalchemy.dialects.postgresql import ARRAY
except:
    from sqlalchemy.types import JSON
    from invenio_explicit_acls.utils import ArrayType as ARRAY


# revision identifiers, used by Alembic.
revision = 'ec5cbec43ec8'
down_revision = None
branch_labels = ('invenio_explicit_acls',)
depends_on = None


def upgrade():
    """Upgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('explicit_acls_acl',
    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('name', sa.String(), nullable=False),
    sa.Column('priority', sa.Integer(), nullable=True),
    sa.Column('schemas', ARRAY(sa.String()), nullable=True),
    sa.Column('originator_id', sa.Integer(), nullable=False),
    sa.Column('type', sa.String(length=50), nullable=True),
    sa.Column('operation', sa.String(length=50), nullable=True),
    sa.ForeignKeyConstraint(['originator_id'], ['accounts_user.id'], name=op.f('fk_explicit_acls_acl_originator_id_accounts_user'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_acl'))
    )
    op.create_index(op.f('ix_explicit_acls_acl_originator_id'), 'explicit_acls_acl', ['originator_id'], unique=False)
    op.create_table('explicit_acls_actor',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('type', sa.String(length=50), nullable=True),
    sa.Column('acl_id', sa.String(length=36), nullable=True),
    sa.ForeignKeyConstraint(['acl_id'], ['explicit_acls_acl.id'], name=op.f('fk_explicit_acls_actor_acl_id_explicit_acls_acl')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_actor'))
    )
    op.create_table('explicit_acls_defaultacl',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_acl.id'], name=op.f('fk_explicit_acls_defaultacl_id_explicit_acls_acl')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_defaultacl'))
    )
    op.create_table('explicit_acls_elasticsearchacl',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('record_selector', JSON(astext_type=sa.Text()), nullable=True),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_acl.id'], name=op.f('fk_explicit_acls_elasticsearchacl_id_explicit_acls_acl')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_elasticsearchacl'))
    )
    op.create_table('explicit_acls_idacl',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('record_id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_acl.id'], name=op.f('fk_explicit_acls_idacl_id_explicit_acls_acl')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_idacl'))
    )
    op.create_table('explicit_acls_system_role',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('authenticated', sa.Boolean(), nullable=True),
    sa.Column('anonymous', sa.Boolean(), nullable=True),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_actor.id'], name=op.f('fk_explicit_acls_system_role_id_explicit_acls_actor')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_system_role'))
    )
    op.create_table('explicit_acls_roleactor',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_actor.id'], name=op.f('fk_explicit_acls_roleactor_id_explicit_acls_actor')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_roleactor'))
    )
    op.create_table('explicit_acls_useractor',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['id'], ['explicit_acls_actor.id'], name=op.f('fk_explicit_acls_useractor_id_explicit_acls_actor')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_explicit_acls_useractor'))
    )
    op.create_table('explicit_acls_roles_roleactors',
    sa.Column('role_id', sa.Integer(), nullable=False),
    sa.Column('actor_id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['actor_id'], ['explicit_acls_roleactor.id'], name=op.f('fk_explicit_acls_roles_roleactors_actor_id_explicit_acls_roleactor')),
    sa.ForeignKeyConstraint(['role_id'], ['accounts_role.id'], name=op.f('fk_explicit_acls_roles_roleactors_role_id_accounts_role')),
    sa.PrimaryKeyConstraint('role_id', 'actor_id', name=op.f('pk_explicit_acls_roles_roleactors'))
    )
    op.create_table('explicit_acls_users_useractors',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('actor_id', sa.String(length=36), nullable=False),
    sa.ForeignKeyConstraint(['actor_id'], ['explicit_acls_useractor.id'], name=op.f('fk_explicit_acls_users_useractors_actor_id_explicit_acls_useractor')),
    sa.ForeignKeyConstraint(['user_id'], ['accounts_user.id'], name=op.f('fk_explicit_acls_users_useractors_user_id_accounts_user')),
    sa.PrimaryKeyConstraint('user_id', 'actor_id', name=op.f('pk_explicit_acls_users_useractors'))
    )
    # ### end Alembic commands ###


def downgrade():
    """Downgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('explicit_acls_users_useractors')
    op.drop_table('explicit_acls_roles_roleactors')
    op.drop_table('explicit_acls_useractor')
    op.drop_table('explicit_acls_roleactor')
    op.drop_table('explicit_acls_system_role')
    op.drop_table('explicit_acls_idacl')
    op.drop_table('explicit_acls_elasticsearchacl')
    op.drop_table('explicit_acls_defaultacl')
    op.drop_table('explicit_acls_actor')
    op.drop_index(op.f('ix_explicit_acls_acl_originator_id'), table_name='explicit_acls_acl')
    op.drop_table('explicit_acls_acl')
    # ### end Alembic commands ###
