Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added indexes on invoice ref table #1096

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions pay-api/migrations/README

This file was deleted.

7 changes: 7 additions & 0 deletions pay-api/migrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Migrating Pay-db

Run `python manage.py db migrate`

If you are updating a large table (i.e. invoices, invoice_references, etc.) add `op.execute("set statement_timeout=20000;")` to the top of your new migration scripts for upgrade/downgrade. This will prevent the deployment from causing errors in prod when it takes too long to complete (> 20 seconds).

_Note: If it takes too long this will cause the prehook to fail in the deployment so you will need to watch it and redeploy at a less busy time if necessary_
2 changes: 1 addition & 1 deletion pay-api/migrations/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[alembic]
script_location = .
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
Expand Down
33 changes: 33 additions & 0 deletions pay-api/migrations/versions/2023_02_08_inv_ref_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""empty message

Revision ID: e296910623cd
Revises: 286acad5d366
Create Date: 2023-02-07 12:17:04.806485

"""
from alembic import op
import sqlalchemy as sa
from time import sleep


# revision identifiers, used by Alembic.
revision = 'e296910623cd'
down_revision = '286acad5d366'
branch_labels = None
depends_on = None


def upgrade():
op.execute("set statement_timeout=20000;")
# ### commands auto generated by Alembic - please adjust! ###
op.create_index(op.f('ix_invoice_references_invoice_id'), 'invoice_references', ['invoice_id'], unique=False)
op.create_index(op.f('ix_invoice_references_status_code'), 'invoice_references', ['status_code'], unique=False)
# ### end Alembic commands ###


def downgrade():
op.execute("set statement_timeout=20000;")
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_invoice_references_status_code'), table_name='invoice_references')
op.drop_index(op.f('ix_invoice_references_invoice_id'), table_name='invoice_references')
# ### end Alembic commands ###
5 changes: 3 additions & 2 deletions pay-api/src/pay_api/models/invoice_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class InvoiceReference(BaseModel): # pylint: disable=too-many-instance-attribut
__tablename__ = 'invoice_references'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
invoice_id = db.Column(db.Integer, ForeignKey('invoices.id'), nullable=False)
invoice_id = db.Column(db.Integer, ForeignKey('invoices.id'), nullable=False, index=True)

invoice_number = db.Column(db.String(50), nullable=True, index=True)
reference_number = db.Column(db.String(50), nullable=True)
status_code = db.Column(db.String(20), ForeignKey('invoice_reference_status_codes.code'), nullable=False)
status_code = db.Column(db.String(20), ForeignKey(
'invoice_reference_status_codes.code'), nullable=False, index=True)

@classmethod
def find_by_invoice_id_and_status(cls, invoice_id: int, status_code: str):
Expand Down