primary key for ft_billing should use notification_type

This commit is contained in:
venusbb
2018-03-21 14:14:16 +00:00
parent b59a59f45a
commit af975b89ec
3 changed files with 56 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ from sqlalchemy import func, desc, case
from app.dao.dao_utils import transactional
from notifications_utils.statsd_decorators import statsd
from app import notify_celery
from flask import current_app
def get_rate(non_letter_rates, letter_rates, notification_type, date, crown=None, rate_multiplier=None):
@@ -76,13 +77,16 @@ def create_nightly_billing(day_start=None):
'day_created'
).all()
updated_records = 0
inserted_records = 0
for data in transit_data:
update_count = FactBilling.query.filter(
FactBilling.bst_date == data.day_created,
FactBilling.template_id == data.template_id,
FactBilling.provider == data.sent_by, # This could be zero - this is a bug that needs to be fixed.
FactBilling.rate_multiplier == data.rate_multiplier,
FactBilling.international == data.international,
FactBilling.notification_type == data.notification_type,
).update(
{"notifications_sent": data.notifications_sent,
"billable_units": data.billable_units},
@@ -106,3 +110,9 @@ def create_nightly_billing(day_start=None):
data.rate_multiplier)
)
db.session.add(billing_record)
inserted_records += 1
updated_records += update_count
current_app.logger.info('ft_billing: {} rows updated, {} rows inserted'
.format(updated_records, inserted_records))

View File

@@ -1774,10 +1774,10 @@ class FactBilling(db.Model):
bst_date = db.Column(db.Date, nullable=False, primary_key=True, index=True)
template_id = db.Column(UUID(as_uuid=True), nullable=False, primary_key=True, index=True)
service_id = db.Column(UUID(as_uuid=True), nullable=False, index=True)
notification_type = db.Column(db.Text, nullable=True)
notification_type = db.Column(db.Text, nullable=False, primary_key=True)
provider = db.Column(db.Text, nullable=True, primary_key=True)
rate_multiplier = db.Column(db.Numeric(), nullable=True, primary_key=True)
international = db.Column(db.Boolean, nullable=True, primary_key=True)
international = db.Column(db.Boolean, nullable=False, primary_key=False)
rate = db.Column(db.Numeric(), nullable=True)
billable_units = db.Column(db.Numeric(), nullable=True)
notifications_sent = db.Column(db.Integer(), nullable=True)

View File

@@ -0,0 +1,43 @@
"""
Revision ID: 0181_billing_primary_key
Revises: 0180_another_letter_org
Create Date: 2018-03-21 13:41:26.203712
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision = '0181_billing_primary_key'
down_revision = '0180_another_letter_org'
def upgrade():
op.alter_column('ft_billing', 'service_id',
existing_type=postgresql.UUID(),
nullable=False)
op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary')
op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date',
'template_id',
'rate_multiplier',
'provider',
'notification_type'])
op.create_index(op.f('ix_ft_billing_template_id'), 'ft_billing', ['template_id'], unique=False)
def downgrade():
op.alter_column('ft_billing', 'service_id',
existing_type=postgresql.UUID(),
nullable=True)
op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary')
op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date',
'template_id',
'rate_multiplier',
'provider',
'international'])
op.drop_index(op.f('ix_ft_billing_template_id'), table_name='ft_billing')