"""empty message

Revision ID: 0028_add_default_permissions
Revises: 0027_add_service_permission
Create Date: 2016-02-26 10:33:20.536362

"""

# revision identifiers, used by Alembic.
revision = '0028_add_default_permissions'
down_revision = '0027_add_service_permission'
import uuid
from datetime import datetime
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    user_services = conn.execute("SELECT * FROM user_to_service").fetchall()
    for entry in user_services:
        id_ = uuid.uuid4()
        created_at = datetime.now().isoformat().replace('T', ' ')
        conn.execute((
            "INSERT INTO permissions (id, user_id, service_id, permission, created_at)"
            " VALUES ('{}', '{}', '{}', 'manage_service', '{}')").format(id_, entry[0], entry[1], created_at))
        id_ = uuid.uuid4()
        conn.execute((
            "INSERT INTO permissions (id, user_id, service_id, permission, created_at)"
            " VALUES ('{}', '{}', '{}', 'send_messages', '{}')").format(id_, entry[0], entry[1], created_at))
        id_ = uuid.uuid4()
        conn.execute((
            "INSERT INTO permissions (id, user_id, service_id, permission, created_at)"
            " VALUES ('{}', '{}', '{}', 'manage_api_keys', '{}')").format(id_, entry[0], entry[1], created_at))
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    conn.execute("DELETE FROM permissions")

    ### end Alembic commands ###
Add default permissions for existing services.
Add default permissions for existing services.
