<reponame>kzagorulko/flower-system
"""added roles, more fields for users

Revision ID: <KEY>
Revises: <PASSWORD>
Create Date: 2020-11-01 15:01:10.385506

"""
from alembic import op
import sqlalchemy as sa
from uuid import uuid4


# revision identifiers, used by Alembic.
revision = '<KEY>'
down_revision = '<PASSWORD>'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    t_roles = op.create_table(
        'roles',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('display_name', sa.String(length=64), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('display_name'),
        sa.UniqueConstraint('name')
    )
    op.add_column(
        'users', sa.Column('deactivated', sa.Boolean(), nullable=True)
    )
    op.add_column(
        'users',
        sa.Column('display_name', sa.String(length=50), nullable=True)
    )
    op.add_column(
        'users', sa.Column('email', sa.String(length=50), nullable=True)
    )
    op.add_column(
        'users',
        sa.Column('path_to_image', sa.String(length=120), nullable=True)
    )
    op.add_column(
        'users', sa.Column('role_id', sa.Integer(), nullable=True)
    )
    op.add_column('users', sa.Column('session', sa.String(length=36)))

    t_users = sa.Table(
        'users',
        sa.MetaData(),
        sa.Column('id', sa.Integer),
        sa.Column('username', sa.String),
        sa.Column('deactivated', sa.Boolean),
        sa.Column('display_name', sa.String),
        sa.Column('email', sa.String),
        sa.Column('session', sa.String),
        sa.Column('role_id', sa.Integer)
    )

    connection = op.get_bind()

    connection.execute(
        sa.insert(t_roles).values(
            {'name': 'admin', 'display_name': 'Администрация'}
        )
    )

    users = connection.execute(
        sa.select([t_users.c.id, t_users.c.username])
    ).fetchall()

    for user in users:
        connection.execute(
            sa.update(t_users).where(
                t_users.c.id == user[0]
            ).values(
                email=f'{<EMAIL>',
                deactivated=False,
                display_name='Иван',
                session=str(uuid4()),
                role_id=1
            )
        )

    op.alter_column('users', 'deactivated', nullable=False)
    op.alter_column('users', 'display_name', nullable=False)
    op.alter_column('users', 'email', nullable=False)

    op.create_unique_constraint('uq_email', 'users', ['email'])
    op.create_foreign_key(
        'fk_users_roles', 'users', 'roles', ['role_id'], ['id']
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('fk_users_roles', 'users', type_='foreignkey')
    op.drop_constraint('uq_email', 'users', type_='unique')
    op.drop_column('users', 'role_id')
    op.drop_column('users', 'path_to_image')
    op.drop_column('users', 'email')
    op.drop_column('users', 'display_name')
    op.drop_column('users', 'deactivated')
    op.drop_column('users', 'session')
    op.drop_table('roles')
    # ### end Alembic commands ###

"""added roles, more fields for users

Revision ID: <KEY>
Revises: <PASSWORD>
Create Date: 2020-11-01 15:01:10.385506

"""
from alembic import op
import sqlalchemy as sa
from uuid import uuid4


# revision identifiers, used by Alembic.
revision = '<KEY>'
down_revision = '<PASSWORD>'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    t_roles = op.create_table(
        'roles',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('display_name', sa.String(length=64), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('display_name'),
        sa.UniqueConstraint('name')
    )
    op.add_column(
        'users', sa.Column('deactivated', sa.Boolean(), nullable=True)
    )
    op.add_column(
        'users',
        sa.Column('display_name', sa.String(length=50), nullable=True)
    )
    op.add_column(
        'users', sa.Column('email', sa.String(length=50), nullable=True)
    )
    op.add_column(
        'users',
        sa.Column('path_to_image', sa.String(length=120), nullable=True)
    )
    op.add_column(
        'users', sa.Column('role_id', sa.Integer(), nullable=True)
    )
    op.add_column('users', sa.Column('session', sa.String(length=36)))

    t_users = sa.Table(
        'users',
        sa.MetaData(),
        sa.Column('id', sa.Integer),
        sa.Column('username', sa.String),
        sa.Column('deactivated', sa.Boolean),
        sa.Column('display_name', sa.String),
        sa.Column('email', sa.String),
        sa.Column('session', sa.String),
        sa.Column('role_id', sa.Integer)
    )

    connection = op.get_bind()

    connection.execute(
        sa.insert(t_roles).values(
            {'name': 'admin', 'display_name': 'Администрация'}
        )
    )

    users = connection.execute(
        sa.select([t_users.c.id, t_users.c.username])
    ).fetchall()

    for user in users:
        connection.execute(
            sa.update(t_users).where(
                t_users.c.id == user[0]
            ).values(
                email=f'{<EMAIL>',
                deactivated=False,
                display_name='Иван',
                session=str(uuid4()),
                role_id=1
            )
        )

    op.alter_column('users', 'deactivated', nullable=False)
    op.alter_column('users', 'display_name', nullable=False)
    op.alter_column('users', 'email', nullable=False)

    op.create_unique_constraint('uq_email', 'users', ['email'])
    op.create_foreign_key(
        'fk_users_roles', 'users', 'roles', ['role_id'], ['id']
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('fk_users_roles', 'users', type_='foreignkey')
    op.drop_constraint('uq_email', 'users', type_='unique')
    op.drop_column('users', 'role_id')
    op.drop_column('users', 'path_to_image')
    op.drop_column('users', 'email')
    op.drop_column('users', 'display_name')
    op.drop_column('users', 'deactivated')
    op.drop_column('users', 'session')
    op.drop_table('roles')
    # ### end Alembic commands ###
added roles, more fields for users  Revision ID: <KEY> Revises: <PASSWORD> Create Date: 2020-11-01 15:01:10.385506 # revision identifiers, used by Alembic. # ### commands auto generated by Alembic - please adjust! ### # ### end Alembic commands ### # ### commands auto generated by Alembic - please adjust! ### # ### end Alembic commands ###