diff --git a/app/commands.py b/app/commands.py index d55f8a1b5..2eacd7584 100644 --- a/app/commands.py +++ b/app/commands.py @@ -412,10 +412,10 @@ def migrate_data_to_ft_billing(start_date, end_date): sql = \ """ insert into ft_billing (bst_date, template_id, service_id, notification_type, provider, rate_multiplier, - international, billable_units, notifications_sent, rate) + international, billable_units, notifications_sent, rate, created_at) select bst_date, template_id, service_id, notification_type, provider, rate_multiplier, international, sum(billable_units) as billable_units, sum(notifications_sent) as notification_sent, - case when notification_type = 'sms' then sms_rate else letter_rate end as rate + case when notification_type = 'sms' then sms_rate else letter_rate end as rate, created_at from ( select n.id, @@ -441,7 +441,8 @@ def migrate_data_to_ft_billing(start_date, end_date): and s.crown = l.crown and n.notification_type='letter'), 0) as letter_rate, coalesce(n.international, false) as international, n.billable_units, - 1 as notifications_sent + 1 as notifications_sent, + now() as created_at from public.notification_history n left join services s on s.id = n.service_id where n.key_type!='test' @@ -452,12 +453,13 @@ def migrate_data_to_ft_billing(start_date, end_date): and n.created_at < (date :end + time '00:00:00') at time zone 'Europe/London' at time zone 'UTC' ) as individual_record group by bst_date, template_id, service_id, notification_type, provider, rate_multiplier, international, - sms_rate, letter_rate + sms_rate, letter_rate, created_at order by bst_date on conflict on constraint ft_billing_pkey do update set billable_units = excluded.billable_units, notifications_sent = excluded.notifications_sent, - rate = excluded.rate + rate = excluded.rate, + updated_at = now() """ result = db.session.execute(sql, {"start": process_date, "end": process_date + timedelta(days=1)}) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 8e5b95acc..db5758d69 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -229,7 +229,8 @@ def update_fact_billing(data, process_day): stmt = stmt.on_conflict_do_update( constraint="ft_billing_pkey", set_={"notifications_sent": stmt.excluded.notifications_sent, - "billable_units": stmt.excluded.billable_units + "billable_units": stmt.excluded.billable_units, + "updated_at": datetime.utcnow() } ) db.session.connection().execute(stmt) diff --git a/app/models.py b/app/models.py index 1ed954c09..b697ab026 100644 --- a/app/models.py +++ b/app/models.py @@ -1774,7 +1774,7 @@ class FactBilling(db.Model): rate = db.Column(db.Numeric(), nullable=True) billable_units = db.Column(db.Integer(), nullable=True) notifications_sent = db.Column(db.Integer(), nullable=True) - created_at = db.Column(db.DateTime, nullable=True, default=datetime.datetime.utcnow) + created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) diff --git a/migrations/versions/0194_ft_billing_created_at.py b/migrations/versions/0194_ft_billing_created_at.py new file mode 100644 index 000000000..cc2390c59 --- /dev/null +++ b/migrations/versions/0194_ft_billing_created_at.py @@ -0,0 +1,23 @@ +""" + +Revision ID: 0194_ft_billing_created_at +Revises: 0193_add_ft_billing_timestamps +Create Date: 2018-05-22 14:34:27.852096 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0194_ft_billing_created_at' +down_revision = '0193_add_ft_billing_timestamps' + + +def upgrade(): + op.execute("UPDATE ft_billing SET created_at = NOW()") + op.alter_column('ft_billing', 'created_at', nullable=False) + + +def downgrade(): + op.alter_column('ft_billing', 'created_at', nullable=True) + op.execute("UPDATE ft_billing SET created_at = null") diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index 7f181d046..c0f105f7c 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -447,6 +447,7 @@ def test_create_nightly_billing_update_when_record_exists( assert len(records) == 1 assert records[0].bst_date == date(2018, 1, 14) assert records[0].billable_units == 1 + assert not records[0].updated_at sample_notification( notify_db, @@ -465,3 +466,4 @@ def test_create_nightly_billing_update_when_record_exists( create_nightly_billing() assert len(records) == 1 assert records[0].billable_units == 2 + assert records[0].updated_at