diff --git a/app/models.py b/app/models.py index 9d0db0563..2b91d9395 100644 --- a/app/models.py +++ b/app/models.py @@ -579,6 +579,9 @@ class AnnualBilling(db.Model): UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id') service = db.relationship(Service, backref=db.backref("annual_billing", uselist=True)) + __table_args__ = (UniqueConstraint( + 'service_id', 'financial_year_start', name='uix_service_id_financial_year_start'),) + def serialize_free_sms_items(self): return { 'free_sms_fragment_limit': self.free_sms_fragment_limit, diff --git a/migrations/versions/0351_unique_key_annual_billing.py b/migrations/versions/0351_unique_key_annual_billing.py new file mode 100644 index 000000000..288b18395 --- /dev/null +++ b/migrations/versions/0351_unique_key_annual_billing.py @@ -0,0 +1,34 @@ +""" + +Revision ID: 0351_unique_key_annual_billing +Revises: 0350_update_rates +Create Date: 2021-04-12 09:02:45.098875 + +""" +import os + +from alembic import op + +revision = '0351_unique_key_annual_billing' +down_revision = '0350_update_rates' + +environment = os.environ['NOTIFY_ENVIRONMENT'] + + +def upgrade(): + index = """ + CREATE UNIQUE INDEX CONCURRENTLY uix_service_id_financial_year_start + ON annual_billing (service_id, financial_year_start) + """ + constraint = """ + ALTER TABLE annual_BILLING add constraint uix_service_id_financial_year_start + UNIQUE USING INDEX uix_service_id_financial_year_start + """ + op.execute('COMMIT') + op.execute(index) + op.execute(constraint) + + +def downgrade(): + op.drop_constraint('uix_service_id_financial_year_start', 'annual_billing', type_='unique') +