"""Added trainingplan modul

Revision ID: 2bb2f8c5b059
Revises: bf00e371aecb
Create Date: 2016-11-30 21:35:38.637284

"""

# revision identifiers, used by Alembic.
revision = '2bb2f8c5b059'
down_revision = 'bf00e371aecb'

from alembic import op
import sqlalchemy as sa


INSERTS = """
INSERT INTO modules (id, name, description, label, label_plural, clazzpath, display, str_repr, uuid) VALUES (1001, 'trainingplans', '', 'Trainingplan', 'Trainingplans', 'trainable.model.trainingplan.Trainingplan', 'header-menu', '%s|id', '107381f8-3186-4c0e-aee0-1741cba9a6da');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (47, 1001, '5a25cc91-bc6f-4ccd-be19-1ed4e7c566df', 'List', 'list', 'icon-list-alt', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (48, 1001, '22add2cd-9ded-4a9c-ac7e-a5497123a200', 'Create', 'create', 'icon-plus', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (49, 1001, 'b0e2e62e-4194-4d83-a33d-abe787686bde', 'Read', 'read/{id}', 'icon-eye-open', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (50, 1001, '66fe3784-e163-419a-96bc-521f62ed0049', 'Update', 'update/{id}', 'icon-edit', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (51, 1001, 'f6346fb5-b938-4af6-b6ef-a5c5b9666c84', 'Delete', 'delete/{id}', 'icon-eye-delete', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (52, 1001, '42d70853-acb5-4787-aea0-abd0af661b42', 'Import', 'import', 'icon-import', 'False', '', '', '');
INSERT INTO actions (id, mid, uuid, name, url, icon, bundle, description, display, permission) VALUES (53, 1001, '53c1d4be-067b-483b-8ef9-c585eb958dc0', 'Export', 'export/{id}', 'icon-export', 'True', '', '', '');
INSERT INTO nm_action_roles (aid, rid) VALUES (48, 1);
INSERT INTO nm_action_roles (aid, rid) VALUES (49, 1);
INSERT INTO nm_action_roles (aid, rid) VALUES (50, 1);
INSERT INTO nm_action_roles (aid, rid) VALUES (47, 1);
INSERT INTO nm_action_roles (aid, rid) VALUES (51, 1);
"""
DELETES = """
DELETE FROM nm_action_roles WHERE aid = 48;
DELETE FROM nm_action_roles WHERE aid = 49;
DELETE FROM nm_action_roles WHERE aid = 50;
DELETE FROM nm_action_roles WHERE aid = 47;
DELETE FROM nm_action_roles WHERE aid = 51;
DELETE FROM actions where mid = 1001;
DELETE FROM modules where id = 1001;
"""


def iter_statements(stmts):
    for st in [x for x in stmts.split('\n') if x]:
        op.execute(st)


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('trainingplans',
    sa.Column('uuid', sa.CHAR(length=36), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('uid', sa.Integer(), nullable=True),
    sa.Column('gid', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['gid'], ['usergroups.id'], name='fk_trainingplans_gid_usergroups', use_alter=True),
    sa.ForeignKeyConstraint(['uid'], ['users.id'], name='fk_trainingplans_uid_users', use_alter=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('uuid')
    )
    ### end Alembic commands ###
    iter_statements(INSERTS)


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('trainingplans')
    ### end Alembic commands ###
    iter_statements(DELETES)
