generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added counter field and updated confirmation_id (#473)
Closes #472 Closes #460 Removed microseconds from timestamp Added counter field to Submission table/DAO/DTO Added alembic script to add field, add unique constraint for filing id and counter, and set existing submission counters Updated add_submission to get the latest count and add 1, or start from 1 if new for the filing period Updated pytests to test counter
- Loading branch information
Showing
10 changed files
with
209 additions
and
65 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
db_revisions/versions/6ec12afa5b37_add_counter_to_submission_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""add counter to submission table | ||
Revision ID: 6ec12afa5b37 | ||
Revises: 26f11ac15b3c | ||
Create Date: 2024-10-28 10:52:22.353469 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "6ec12afa5b37" | ||
down_revision: Union[str, None] = "63138f5cf036" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# create the new column as nullable so it doesn't error with existing data | ||
with op.batch_alter_table("submission", schema=None) as batch_op: | ||
batch_op.add_column(sa.Column("counter", sa.Integer, nullable=True)) | ||
batch_op.create_unique_constraint("unique_filing_counter", ["filing", "counter"]) | ||
|
||
# run a counter of each submission in a given filing id, ordered by the submission id, | ||
# which is the PK incrementor. This will give us accurate counts of 1, 2, 3, ... for | ||
# each submission per filing id. | ||
conn = op.get_bind() | ||
conn.execute( | ||
sa.text( | ||
""" | ||
WITH counts AS ( | ||
SELECT id, filing, ROW_NUMBER() OVER (PARTITION BY filing ORDER BY id) AS row_num FROM submission | ||
) | ||
UPDATE submission SET counter = counts.row_num | ||
FROM counts | ||
WHERE submission.id = counts.id | ||
""" | ||
) | ||
) | ||
|
||
# set the counter column to required now that existing data is set. | ||
with op.batch_alter_table("submission", schema=None) as batch_op: | ||
batch_op.alter_column("counter", nullable=False) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_constraint(constraint_name="unique_filing_counter", table_name="submission") | ||
op.drop_column("submission", "counter") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.