a01dc09faec0_upgrade_user.py 3.14 KB
"""upgrade user

Revision ID: a01dc09faec0
Revises: 
Create Date: 2023-03-06 20:23:58.698148

"""

# revision identifiers, used by Alembic.
revision = 'a01dc09faec0'
down_revision = None
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    src_schema = 'app'
    tables = ['apps', 'app_status',
              'users', 'groups', "modules", "group_modules",  "user_groups",
              'user_uptd']
    context = op.get_context()
    helpers = context.opts['helpers']
    for t in tables:
        if helpers.has_table(t, src_schema):
            context.execute(f'alter table {src_schema}.{t} set schema public')

    schema = 'public'
    if not helpers.table_has_column('users', 'user_name', schema):
        op.add_column('users',
                      sa.Column('user_name', sa.String(128)), schema=schema)
        
    if not helpers.table_has_column('users', 'user_password', schema):
        op.add_column('users',
                      sa.Column('user_password', sa.String(256)), schema=schema)

    if not helpers.table_has_column('users', 'email', schema):
        op.add_column('users',
                      sa.Column('email', sa.String(100)), schema=schema)
    if not helpers.table_has_column('users', 'status', schema):
        op.add_column('users',
                      sa.Column('status', sa.SmallInteger), schema=schema)

    if not helpers.table_has_column('users', 'security_code', schema):
        op.add_column('users',
                      sa.Column('security_code', sa.String(256)), schema=schema)
    if not helpers.table_has_column('users', 'last_login_date', schema):
        op.add_column('users',
                      sa.Column('last_login_date', sa.TIMESTAMP(timezone=True)), schema=schema)
    if not helpers.table_has_column('users', 'registered_date', schema):
        op.add_column('users',
                      sa.Column('registered_date', sa.TIMESTAMP(timezone=True)), schema=schema)
    if not helpers.table_has_column('users', 'security_code_date', schema):
        op.add_column('users',
                      sa.Column('security_code_date', 
                                sa.TIMESTAMP(timezone=True)), 
                    schema=schema)
    if not helpers.table_has_column('users', 'api_key', schema):
        op.add_column(
            'users', sa.Column('api_key', sa.String(256)), schema=schema)
    if not helpers.table_has_column('users', 'partner_id', schema):
        op.add_column(
            'users', sa.Column('partner_id', sa.Integer), schema=schema)
    
    if not helpers.table_has_column('users', 'company_id', schema):
        op.add_column(
            'users', sa.Column('company_id', sa.Integer), schema=schema)
    
    if not helpers.has_table("alembic_ziggurat_foundations_version", "public"):
        table = op.create_table(
            "alembic_ziggurat_foundations_version", sa.Column(
                "version_num", sa.String(64)),
            schema="public")
    else:
        table = sa.sql.table(
            "alembic_ziggurat_foundations_version", sa.sql.column("version_num", sa.String(64)))
    op.bulk_insert(table, [{"version_num": "613e7c11dead"}])

def downgrade():
    pass