"""initial migration

Revision ID: 712d42956a47
Revises:
Create Date: 2021-12-10 12:00:00.848561

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection
from sqlalchemy.engine.reflection import Inspector

conn = op.get_bind()
inspector = Inspector.from_engine(conn)
tables = inspector.get_table_names()

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

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    if 'charxml' not in tables:
        op.create_table('charxml',
            sa.Column('id', mysql.BIGINT(), nullable=False),
            sa.Column('xml_data', sa.Text(length=4294000000), nullable=False),
            sa.PrimaryKeyConstraint('id')
        )
    if 'object_id_tracker' not in tables:
        op.create_table('object_id_tracker',
            sa.Column('last_object_id', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.PrimaryKeyConstraint('last_object_id')
        )
    if 'pet_names' not in tables:
        op.create_table('pet_names',
            sa.Column('id', mysql.BIGINT(), nullable=False),
            sa.Column('pet_name', mysql.TEXT(), nullable=False),
            sa.Column('approved', mysql.INTEGER(unsigned=True), server_default='0', nullable=False),
            sa.PrimaryKeyConstraint('id')
        )
    if 'play_keys' not in tables:
        op.create_table('play_keys',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('key_string', mysql.CHAR(length=19), nullable=False),
            sa.Column('key_uses', mysql.INTEGER(), server_default='1', nullable=False),
            sa.Column('created_at', mysql.TIMESTAMP(), server_default=sa.text('now()'), nullable=False),
            sa.Column('active', sa.BOOLEAN(), server_default='1', nullable=False),
            sa.PrimaryKeyConstraint('id'),
            sa.UniqueConstraint('key_string')
        )

    op.add_column('play_keys', sa.Column('notes', mysql.TEXT(), nullable=True))

    if 'servers' not in tables:
        op.create_table('servers',
            sa.Column('id', mysql.INTEGER(), nullable=False),
            sa.Column('name', mysql.TEXT(), nullable=False),
            sa.Column('ip', mysql.TEXT(), nullable=False),
            sa.Column('port', mysql.INTEGER(), nullable=False),
            sa.Column('state', mysql.INTEGER(), nullable=False),
            sa.Column('version', mysql.INTEGER(), server_default='0', nullable=False),
            sa.PrimaryKeyConstraint('id')
        )

    if 'accounts' not in tables:
        op.create_table('accounts',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('name', sa.VARCHAR(length=35), nullable=False),
            sa.Column('password', sa.Text(), server_default='', nullable=False),
            sa.Column('gm_level', mysql.INTEGER(unsigned=True), server_default='0', nullable=False),
            sa.Column('locked', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('banned', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('created_at', mysql.TIMESTAMP(), server_default=sa.text('now()'), nullable=False),
            sa.Column('play_key_id', mysql.INTEGER(), nullable=True),
            sa.Column('mute_expire', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.ForeignKeyConstraint(['play_key_id'], ['play_keys.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id'),
            sa.UniqueConstraint('name')
        )

    op.add_column('accounts', sa.Column('active', sa.BOOLEAN(), server_default='1', nullable=False))
    op.add_column('accounts', sa.Column('email_confirmed_at', sa.DateTime(), nullable=True))
    op.add_column('accounts', sa.Column('email', sa.Unicode(length=255), server_default='', nullable=True))

    op.create_table('account_invites',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('email', sa.String(length=255), nullable=False),
        sa.Column('invited_by_user_id', sa.Integer(), nullable=True),
        sa.Column('token', sa.String(length=100), server_default='', nullable=False),
        sa.ForeignKeyConstraint(['invited_by_user_id'], ['accounts.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id')
    )

    if 'bug_reports' not in tables:
        op.create_table('bug_reports',
            sa.Column('id', mysql.INTEGER(), nullable=False),
            sa.Column('body', mysql.TEXT(), nullable=False),
            sa.Column('client_version', mysql.TEXT(), nullable=False),
            sa.Column('other_player_id', mysql.TEXT(), nullable=False),
            sa.Column('selection', mysql.TEXT(), nullable=False),
            sa.Column('submitted', mysql.TIMESTAMP(), server_default=sa.text('now()'), nullable=False),
            sa.ForeignKeyConstraint(['resoleved_by_id'], ['accounts.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    op.add_column('bug_reports', sa.Column('resolved_time', mysql.TIMESTAMP(), nullable=True))
    op.add_column('bug_reports', sa.Column('resoleved_by_id', sa.Integer(), nullable=True))
    op.add_column('bug_reports', sa.Column('resolution', mysql.TEXT(), nullable=True))
    op.create_foreign_key(None, 'bug_reports', 'accounts', ['resoleved_by_id'], ['id'])

    if 'charinfo' not in tables:
        op.create_table('charinfo',
            sa.Column('id', mysql.BIGINT(), autoincrement=False, nullable=False),
            sa.Column('account_id', sa.Integer(), nullable=False),
            sa.Column('name', mysql.VARCHAR(length=35), nullable=False),
            sa.Column('pending_name', mysql.VARCHAR(length=35), nullable=False),
            sa.Column('needs_rename', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('prop_clone_id', mysql.BIGINT(unsigned=True), autoincrement=True, nullable=False),
            sa.Column('last_login', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.Column('permission_map', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id', 'prop_clone_id'),
            sa.UniqueConstraint('prop_clone_id')
        )

        # drop the primary key contstrain on prop_clone_id
        # see the model for an explanation
        op.execute('ALTER TABLE `charinfo` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`) USING BTREE; ')

    if 'activity_log' not in tables:
        op.create_table('activity_log',
            sa.Column('id', mysql.INTEGER(), nullable=False),
            sa.Column('character_id', mysql.BIGINT(), nullable=False),
            sa.Column('activity', mysql.INTEGER(), nullable=False),
            sa.Column('time', mysql.BIGINT(unsigned=True), nullable=False),
            sa.Column('map_id', mysql.INTEGER(), nullable=False),
            sa.ForeignKeyConstraint(['character_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'command_log' not in tables:
        op.create_table('command_log',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('character_id', mysql.BIGINT(), nullable=False),
            sa.Column('command', mysql.VARCHAR(length=256), nullable=False),
            sa.ForeignKeyConstraint(['character_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'friends' not in tables:
        op.create_table('friends',
            sa.Column('player_id', mysql.BIGINT(), nullable=False),
            sa.Column('friend_id', mysql.BIGINT(), nullable=False),
            sa.Column('best_friend', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.ForeignKeyConstraint(['friend_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['player_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('player_id', 'friend_id')
        )

    if 'leaderboard' not in tables:
        op.create_table('leaderboard',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('game_id', mysql.INTEGER(unsigned=True), server_default='0', nullable=False),
            sa.Column('last_played', mysql.TIMESTAMP(), server_default=sa.text('now()'), nullable=False),
            sa.Column('character_id', mysql.BIGINT(), nullable=False),
            sa.Column('time', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.Column('score', mysql.BIGINT(unsigned=True), server_default='0', nullable=False),
            sa.ForeignKeyConstraint(['character_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'mail' not in tables:
        op.create_table('mail',
            sa.Column('id', mysql.INTEGER(), nullable=False),
            sa.Column('sender_id', mysql.INTEGER(), nullable=False),
            sa.Column('sender_name', mysql.VARCHAR(length=35), nullable=False),
            sa.Column('receiver_id', mysql.BIGINT(), nullable=False),
            sa.Column('receiver_name', mysql.VARCHAR(length=35), nullable=False),
            sa.Column('time_sent', mysql.BIGINT(unsigned=True), nullable=False),
            sa.Column('subject', mysql.TEXT(), nullable=False),
            sa.Column('body', mysql.TEXT(), nullable=False),
            sa.Column('attachment_id', mysql.BIGINT(), server_default='0', nullable=False),
            sa.Column('attachment_lot', mysql.INTEGER(), server_default='0', nullable=False),
            sa.Column('attachment_subkey', mysql.BIGINT(), server_default='0', nullable=False),
            sa.Column('attachment_count', mysql.INTEGER(), server_default='0', nullable=False),
            sa.Column('was_read', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.ForeignKeyConstraint(['receiver_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'properties' not in tables:
        op.create_table('properties',
            sa.Column('id', mysql.BIGINT(), autoincrement=False, nullable=False),
            sa.Column('owner_id', mysql.BIGINT(), nullable=False),
            sa.Column('template_id', mysql.INTEGER(unsigned=True), nullable=False),
            sa.Column('clone_id', mysql.BIGINT(unsigned=True), nullable=True),
            sa.Column('name', mysql.TEXT(), nullable=False),
            sa.Column('description', mysql.TEXT(), nullable=False),
            sa.Column('rent_amount', mysql.INTEGER(), nullable=False),
            sa.Column('rent_due', mysql.BIGINT(), nullable=False),
            sa.Column('privacy_option', mysql.INTEGER(), nullable=False),
            sa.Column('mod_approved', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('last_updated', mysql.BIGINT(), nullable=False),
            sa.Column('time_claimed', mysql.BIGINT(), nullable=False),
            sa.Column('rejection_reason', mysql.TEXT(), nullable=False),
            sa.Column('reputation', mysql.BIGINT(unsigned=True), nullable=False),
            sa.Column('zone_id', mysql.INTEGER(), nullable=False),
            sa.ForeignKeyConstraint(['clone_id'], ['charinfo.prop_clone_id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['owner_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'ugc' not in tables:
        op.create_table('ugc',
            sa.Column('id', mysql.INTEGER(), nullable=False),
            sa.Column('account_id', sa.Integer(), nullable=False),
            sa.Column('character_id', mysql.BIGINT(), nullable=False),
            sa.Column('is_optimized', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('lxfml', mysql.MEDIUMBLOB(), nullable=False),
            sa.Column('bake_ao', sa.BOOLEAN(), server_default='0', nullable=False),
            sa.Column('filename', mysql.TEXT(), server_default='', nullable=False),
            sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['character_id'], ['charinfo.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )

    if 'properties_contents' not in tables:
        op.create_table('properties_contents',
            sa.Column('id', mysql.BIGINT(), autoincrement=False, nullable=False),
            sa.Column('property_id', sa.BIGINT(), nullable=False),
            sa.Column('ugc_id', sa.INTEGER(), nullable=True),
            sa.Column('lot', mysql.INTEGER(), nullable=False),
            sa.Column('x', mysql.FLOAT(), nullable=False),
            sa.Column('y', mysql.FLOAT(), nullable=False),
            sa.Column('z', mysql.FLOAT(), nullable=False),
            sa.Column('rx', mysql.FLOAT(), nullable=False),
            sa.Column('ry', mysql.FLOAT(), nullable=False),
            sa.Column('rz', mysql.FLOAT(), nullable=False),
            sa.Column('rw', mysql.FLOAT(), nullable=False),
            sa.ForeignKeyConstraint(['property_id'], ['properties.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['ugc_id'], ['ugc.id'], ondelete='CASCADE'),
            sa.PrimaryKeyConstraint('id')
        )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('properties_contents')
    op.drop_table('ugc')
    op.drop_table('properties')
    op.drop_table('mail')
    op.drop_table('leaderboard')
    op.drop_table('friends')
    op.drop_table('command_log')
    op.drop_table('activity_log')
    op.drop_table('charinfo')
    op.drop_table('bug_reports')
    op.drop_table('account_invites')
    op.drop_table('accounts')
    op.drop_table('servers')
    op.drop_table('play_keys')
    op.drop_table('pet_names')
    op.drop_table('object_id_tracker')
    op.drop_table('charxml')
    # ### end Alembic commands ###
