mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-21 07:51:13 -05:00
Update created_at column of ft_billing to be non-nullable
`created_at` was added previously and made nullable temporarily. This commit now populates the column, ensures that it will always have a value, and makes `created_at` non-nullable.
This commit is contained in:
@@ -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)})
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
23
migrations/versions/0194_ft_billing_created_at.py
Normal file
23
migrations/versions/0194_ft_billing_created_at.py
Normal file
@@ -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")
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user