"""Statistics Refactoring

Revision ID: 5413cb96c44d
Revises: 2ab0bbb8b49d
Create Date: 2020-12-13 13:13:26.242853

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '5413cb96c44d'
down_revision = '2ab0bbb8b49d'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('coverage_statistics', sa.Column('max_signal_quality', sa.Float(precision=2), nullable=True))
    op.add_column('coverage_statistics', sa.Column('min_altitude', sa.Float(precision=2), nullable=True))
    op.add_column('coverage_statistics', sa.Column('max_altitude', sa.Float(precision=2), nullable=True))

    op.create_table('sender_coverage_statistics',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('date', sa.Date(), nullable=True),
        sa.Column('location_mgrs_short', sa.String(length=9), nullable=True),
        sa.Column('is_trustworthy', sa.Boolean(), nullable=True),
        sa.Column('messages_count', sa.Integer(), nullable=True),
        sa.Column('max_distance', sa.Float(precision=2), nullable=True),
        sa.Column('max_normalized_quality', sa.Float(precision=2), nullable=True),
        sa.Column('max_signal_quality', sa.Float(precision=2), nullable=True),
        sa.Column('min_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('max_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('receivers_count', sa.Integer(), nullable=True),
        sa.Column('sender_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['sender_id'], ['senders.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_sender_coverage_statistics_uc', 'sender_coverage_statistics', ['date', 'sender_id', 'location_mgrs_short', 'is_trustworthy'], unique=True)
    op.create_index(op.f('ix_sender_coverage_statistics_sender_id'), 'sender_coverage_statistics', ['sender_id'], unique=False)
    
    op.create_table('receiver_coverage_statistics',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('date', sa.Date(), nullable=True),
        sa.Column('location_mgrs_short', sa.String(length=9), nullable=True),
        sa.Column('is_trustworthy', sa.Boolean(), nullable=True),
        sa.Column('messages_count', sa.Integer(), nullable=True),
        sa.Column('max_distance', sa.Float(precision=2), nullable=True),
        sa.Column('max_normalized_quality', sa.Float(precision=2), nullable=True),
        sa.Column('max_signal_quality', sa.Float(precision=2), nullable=True),
        sa.Column('min_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('max_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('senders_count', sa.Integer(), nullable=True),
        sa.Column('receiver_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['receiver_id'], ['receivers.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_receiver_coverage_statistics_uc', 'receiver_coverage_statistics', ['date', 'receiver_id', 'location_mgrs_short', 'is_trustworthy'], unique=True)
    op.create_index(op.f('ix_receiver_coverage_statistics_receiver_id'), 'receiver_coverage_statistics', ['receiver_id'], unique=False)

    op.create_table('aggregate_coverage_statistics',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('date', sa.Date(), nullable=True),
        sa.Column('location_mgrs_short', sa.String(length=9), nullable=True),
        sa.Column('is_trustworthy', sa.Boolean(), nullable=True),
        sa.Column('messages_count', sa.Integer(), nullable=True),
        sa.Column('max_distance', sa.Float(precision=2), nullable=True),
        sa.Column('max_normalized_quality', sa.Float(precision=2), nullable=True),
        sa.Column('max_signal_quality', sa.Float(precision=2), nullable=True),
        sa.Column('min_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('max_altitude', sa.Float(precision=2), nullable=True),
        sa.Column('senders_count', sa.Integer(), nullable=True),
        sa.Column('receivers_count', sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_aggregate_coverage_statistics_uc', 'aggregate_coverage_statistics', ['date', 'location_mgrs_short', 'is_trustworthy'], unique=True)

    op.drop_index('idx_relation_statistics_uc', table_name='relation_statistics')
    op.drop_index('ix_relation_statistics_receiver_id', table_name='relation_statistics')
    op.drop_index('ix_relation_statistics_sender_id', table_name='relation_statistics')
    op.drop_table('relation_statistics')

    op.rename_table('sender_direction_statistics', 'direction_statistics')
    op.drop_index('idx_sender_direction_statistics_uc')
    op.create_index('idx_direction_statistics_uc', 'direction_statistics', ['sender_id', 'receiver_id'], unique=True)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.rename_table('direction_statistics', 'sender_direction_statistics')
    op.drop_index('idx_direction_statistics_uc')
    op.create_index('idx_sender_direction_statistics_uc', 'sender_direction_statistics', ['sender_id', 'receiver_id'], unique=True)

    op.create_table('relation_statistics',
        sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column('date', sa.DATE(), autoincrement=False, nullable=True),
        sa.Column('is_trustworthy', sa.BOOLEAN(), autoincrement=False, nullable=True),
        sa.Column('messages_count', sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column('max_distance', sa.REAL(), autoincrement=False, nullable=True),
        sa.Column('max_normalized_quality', sa.REAL(), autoincrement=False, nullable=True),
        sa.Column('sender_id', sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column('receiver_id', sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column('coverages_count', sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(['receiver_id'], ['receivers.id'], name='relation_statistics_receiver_id_fkey', ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['sender_id'], ['senders.id'], name='relation_statistics_sender_id_fkey', ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name='relation_statistics_pkey')
    )
    op.create_index('ix_relation_statistics_sender_id', 'relation_statistics', ['sender_id'], unique=False)
    op.create_index('ix_relation_statistics_receiver_id', 'relation_statistics', ['receiver_id'], unique=False)
    op.create_index('idx_relation_statistics_uc', 'relation_statistics', ['date', 'sender_id', 'receiver_id', 'is_trustworthy'], unique=True)

    op.drop_index('idx_aggregate_coverage_statistics_uc', table_name='aggregate_coverage_statistics')
    op.drop_table('aggregate_coverage_statistics')

    op.drop_index(op.f('ix_receiver_coverage_statistics_receiver_id'), table_name='receiver_coverage_statistics')
    op.drop_index('idx_receiver_coverage_statistics_uc', table_name='receiver_coverage_statistics')
    op.drop_table('receiver_coverage_statistics')

    op.drop_index(op.f('ix_sender_coverage_statistics_sender_id'), table_name='sender_coverage_statistics')
    op.drop_index('idx_sender_coverage_statistics_uc', table_name='sender_coverage_statistics')
    op.drop_table('sender_coverage_statistics')

    op.drop_column('coverage_statistics', 'max_altitude')
    op.drop_column('coverage_statistics', 'min_altitude')
    op.drop_column('coverage_statistics', 'max_signal_quality')
    # ### end Alembic commands ###
