From 26617a921e37d58d74f31d32339c1878297b19ba Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Tue, 7 Nov 2017 14:57:31 +0000 Subject: [PATCH] Added database migration file Currently the 'See templates used by month' report is timing out for some services due to the query time of the data from notification_history This is a stats table which will hold the data and will be updated by a scheduled celery task once a day. This data will be combined with the 'live' data from notifications tables (which will be considerably less) to form the data of the new report. --- .../versions/0134_stats_template_usage.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 migrations/versions/0134_stats_template_usage.py diff --git a/migrations/versions/0134_stats_template_usage.py b/migrations/versions/0134_stats_template_usage.py new file mode 100644 index 000000000..7e1b26837 --- /dev/null +++ b/migrations/versions/0134_stats_template_usage.py @@ -0,0 +1,65 @@ +""" + +Revision ID: 0134_stats_template_usage +Revises: 0133_set_services_sms_prefix +Create Date: 2017-11-07 14:35:04.798561 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0134_stats_template_usage' +down_revision = '0133_set_services_sms_prefix' + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('stats_template_usage_by_month', + sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('month', sa.Integer(), nullable=False), + sa.Column('year', sa.Integer(), nullable=False), + sa.Column('count', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), + sa.PrimaryKeyConstraint('template_id', 'month', 'year') + ) + op.create_index(op.f('ix_stats_template_usage_by_month_month'), 'stats_template_usage_by_month', ['month'], unique=False) + op.create_index(op.f('ix_stats_template_usage_by_month_template_id'), 'stats_template_usage_by_month', ['template_id'], unique=False) + op.create_index(op.f('ix_stats_template_usage_by_month_year'), 'stats_template_usage_by_month', ['year'], unique=False) + op.drop_table('notification_statistics') + op.alter_column('notification_history', 'international', + existing_type=sa.BOOLEAN(), + nullable=False) + op.alter_column('notifications', 'international', + existing_type=sa.BOOLEAN(), + nullable=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('notifications', 'international', + existing_type=sa.BOOLEAN(), + nullable=True) + op.alter_column('notification_history', 'international', + existing_type=sa.BOOLEAN(), + nullable=True) + op.create_table('notification_statistics', + sa.Column('id', postgresql.UUID(), autoincrement=False, nullable=False), + sa.Column('service_id', postgresql.UUID(), autoincrement=False, nullable=False), + sa.Column('emails_requested', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('emails_delivered', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('emails_failed', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('sms_requested', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('sms_delivered', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('sms_failed', sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column('day', sa.DATE(), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='notification_statistics_service_id_fkey'), + sa.PrimaryKeyConstraint('id', name='notification_statistics_pkey'), + sa.UniqueConstraint('service_id', 'day', name='uix_service_to_day') + ) + op.drop_index(op.f('ix_stats_template_usage_by_month_year'), table_name='stats_template_usage_by_month') + op.drop_index(op.f('ix_stats_template_usage_by_month_template_id'), table_name='stats_template_usage_by_month') + op.drop_index(op.f('ix_stats_template_usage_by_month_month'), table_name='stats_template_usage_by_month') + op.drop_table('stats_template_usage_by_month') + # ### end Alembic commands ###