"""0.7.0

Revision ID: 52da2632399e
Revises: 1397d9025bd0
Create Date: 2013-11-06 18:34:14.734039

"""

# revision identifiers, used by Alembic.
revision = '52da2632399e'
down_revision = '1397d9025bd0'

from alembic import op
import sqlalchemy as sa

# Create new new role which is allowed to maintain the news.
# User should all be allowed to read all news.
INSERTS = """
INSERT INTO "modules" VALUES(9,'news','ringo.model.news.News','News','News',NULL,NULL,'admin-menu');
INSERT INTO "actions" VALUES(38,9,'List','list','icon-list-alt',NULL);
INSERT INTO "actions" VALUES(39,9,'Create','create',' icon-plus',NULL);
INSERT INTO "actions" VALUES(40,9,'Read','read/{id}','icon-eye-open',NULL);
INSERT INTO "actions" VALUES(41,9,'Update','update/{id}','icon-edit',NULL);
INSERT INTO "actions" VALUES(42,9,'Delete','delete/{id}','icon-trash',NULL);
INSERT INTO "nm_action_roles" VALUES(40,2);
"""
DELETES = """
DELETE FROM modules where id = 9;
DELETE FROM actions where id = 38;
DELETE FROM actions where id = 39;
DELETE FROM actions where id = 40;
DELETE FROM actions where id = 41;
DELETE FROM actions where id = 42;
DELETE FROM "nm_action_roles" where aid=40;
"""


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


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('news',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('subject', sa.String(), nullable=True),
    sa.Column('text', sa.Text(), nullable=True),
    sa.Column('gid', sa.Integer(), nullable=True),
    sa.Column('uid', sa.Integer(), nullable=True),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('updated', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['gid'], ['usergroups.id'], ),
    sa.ForeignKeyConstraint(['uid'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('nm_news_user',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('uid', sa.Integer(), nullable=True),
    sa.Column('nid', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['nid'], ['news.id'], ),
    sa.ForeignKeyConstraint(['uid'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ###
    iter_statements(INSERTS)


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