diff --git a/app/models.py b/app/models.py index 63fdc7480..a6f8c9d0f 100644 --- a/app/models.py +++ b/app/models.py @@ -115,12 +115,12 @@ class NotificationStatistics(db.Model): day = db.Column(db.String(255), nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False) service = db.relationship('Service', backref=db.backref('service_notification_stats', lazy='dynamic')) - emails_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False) - emails_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=True) - emails_error = db.Column(db.BigInteger, index=False, unique=False, nullable=True) - sms_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False) - sms_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=True) - sms_error = db.Column(db.BigInteger, index=False, unique=False, nullable=True) + emails_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + emails_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + emails_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + sms_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + sms_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + sms_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) __table_args__ = ( UniqueConstraint('service_id', 'day', name='uix_service_to_day'), diff --git a/migrations/versions/0042_default_stats_to_zero.py b/migrations/versions/0042_default_stats_to_zero.py new file mode 100644 index 000000000..4d862c903 --- /dev/null +++ b/migrations/versions/0042_default_stats_to_zero.py @@ -0,0 +1,45 @@ +"""empty message + +Revision ID: 0042_default_stats_to_zero +Revises: 0041_platform_admin +Create Date: 2016-03-17 11:09:17.906910 + +""" + +# revision identifiers, used by Alembic. +revision = '0042_default_stats_to_zero' +down_revision = '0041_platform_admin' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.get_bind() + op.execute('update notification_statistics set emails_delivered = 0 where emails_delivered is Null') + op.execute('update notification_statistics set emails_error = 0 where emails_error is Null') + op.execute('update notification_statistics set sms_delivered = 0 where sms_delivered is Null') + op.execute('update notification_statistics set sms_error = 0 where sms_error is Null') + op.alter_column('notification_statistics', 'emails_requested', server_default='0') + op.alter_column('notification_statistics', 'emails_delivered', server_default='0', nullable=False) + op.alter_column('notification_statistics', 'emails_error', server_default='0', nullable=False) + op.alter_column('notification_statistics', 'sms_requested', server_default='0') + op.alter_column('notification_statistics', 'sms_delivered', server_default='0', nullable=False) + op.alter_column('notification_statistics', 'sms_error', server_default='0', nullable=False) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.alter_column('notification_statistics', 'emails_requested', server_default=None) + op.alter_column('notification_statistics', 'emails_delivered', server_default=None, nullable=True) + op.alter_column('notification_statistics', 'emails_error', server_default=None, nullable=True) + op.alter_column('notification_statistics', 'sms_requested', server_default=None) + op.alter_column('notification_statistics', 'sms_delivered', server_default=None, nullable=True) + op.alter_column('notification_statistics', 'sms_error', server_default=None, nullable=True) + op.execute('update notification_statistics set emails_delivered = Null where emails_delivered = 0') + op.execute('update notification_statistics set emails_error = Null where emails_error = 0') + op.execute('update notification_statistics set sms_delivered = Null where sms_delivered = 0') + op.execute('update notification_statistics set sms_error = Null where sms_error = 0') + ### end Alembic commands ### diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index d6a5a95b5..badb93b57 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -70,8 +70,13 @@ def test_should_be_able_to_get_statistics_for_a_service(sample_template): assert len(stats) == 1 assert stats[0].emails_requested == 0 assert stats[0].sms_requested == 1 + assert stats[0].sms_delivered == 0 + assert stats[0].sms_error == 0 assert stats[0].day == notification.created_at.strftime(DATE_FORMAT) assert stats[0].service_id == notification.service_id + assert stats[0].emails_requested == 0 + assert stats[0].emails_delivered == 0 + assert stats[0].emails_error == 0 def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template): @@ -90,7 +95,11 @@ def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_templat sample_template.service.id, now.strftime(DATE_FORMAT) ) assert stat.emails_requested == 0 + assert stat.emails_error == 0 + assert stat.emails_delivered == 0 assert stat.sms_requested == 1 + assert stat.sms_error == 0 + assert stat.sms_delivered == 0 assert stat.day == notification.created_at.strftime(DATE_FORMAT) assert stat.service_id == notification.service_id