"""Adds user roles

Revision ID: 94e8139261e4
Revises: e62d063f87e7
Create Date: 2019-09-19 09:15:15.220988

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '94e8139261e4'
down_revision = 'e62d063f87e7'
branch_labels = None
depends_on = None


def upgrade():
  roles_table = op.create_table('roles',
  sa.Column('id', sa.String(length=255), nullable=False),
  sa.PrimaryKeyConstraint('id')
  )

  op.bulk_insert(
    roles_table,
    [
      {"id": "super_admin"},
      {"id": "admin"},
      {"id": "user"},
      {"id": "end_user"},
    ],
  )

  op.add_column('users', sa.Column('role', sa.String(length=255), server_default=sa.text("'user'"), nullable=True))
  op.create_foreign_key('fk_user_role', 'users', 'roles', ['role'], ['id'])
  op.alter_column("users", "role", server_default=None)
  op.alter_column("users", "role", nullable=False)


def downgrade():
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_constraint('fk_user_role', 'users', type_='foreignkey')
  op.drop_column('users', 'role')
  op.drop_table('roles')
  # ### end Alembic commands ###
