From 793883992ba45394dd0fcee0c55a7896f372404b Mon Sep 17 00:00:00 2001 From: Daniel Bernstein Date: Wed, 21 Aug 2024 16:36:30 -0700 Subject: [PATCH] Add migration to add loan_identifier columns. --- ...an_identifier_column_to_playtime_tables.py | 102 ++++++++++++++++++ .../manager/scripts/playtime_entries.py | 4 +- 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 alembic/versions/20240821_7a2fcaac8b63_add_loan_identifier_column_to_playtime_tables.py diff --git a/alembic/versions/20240821_7a2fcaac8b63_add_loan_identifier_column_to_playtime_tables.py b/alembic/versions/20240821_7a2fcaac8b63_add_loan_identifier_column_to_playtime_tables.py new file mode 100644 index 0000000000..a960b80382 --- /dev/null +++ b/alembic/versions/20240821_7a2fcaac8b63_add_loan_identifier_column_to_playtime_tables.py @@ -0,0 +1,102 @@ +"""Add loan_identifier column to playtime tables. + +Revision ID: 7a2fcaac8b63 +Revises: 7ba553f3f80d +Create Date: 2024-08-21 23:23:48.085451+00:00 + +""" +import sqlalchemy as sa +from alembic import op +from sqlalchemy.orm.session import Session + +# revision identifiers, used by Alembic. +revision = "7a2fcaac8b63" +down_revision = "7ba553f3f80d" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + session = Session(bind=op.get_bind()) + conn = session.connection() + + op.add_column( + "playtime_entries", + sa.Column("loan_identifier", sa.String(length=50), nullable=False, default=""), + ) + + op.drop_constraint( + "unique_playtime_entry", + "playtime_entries", + ) + + op.create_unique_constraint( + "unique_playtime_entry", + "playtime_entries", + [ + "tracking_id", + "identifier_str", + "collection_name", + "library_name", + "loan_identifier", + ], + ) + + op.add_column( + "playtime_summaries", + sa.Column("loan_identifier", sa.String(length=50), nullable=False, default=""), + ) + + op.drop_constraint( + "unique_playtime_summary", + "playtime_summaries", + ) + + op.create_unique_constraint( + "unique_playtime_summary", + "playtime_summaries", + [ + "timestamp", + "identifier_str", + "collection_name", + "library_name", + "loan_identifier", + ], + ) + + +def downgrade() -> None: + session = Session(bind=op.get_bind()) + conn = session.connection() + + op.drop_constraint( + "unique_playtime_entry", + "playtime_entries", + ) + + op.drop_column("playtime_entries", "loan_identifier") + + op.create_unique_constraint( + "unique_playtime_entry", + "playtime_entries", + [ + "tracking_id", + "timestamp", + "identifier_str", + "collection_name", + "library_name", + ], + ) + + op.drop_constraint( + "unique_playtime_summary", + "playtime_summaries", + ) + + op.drop_column("playtime_summaries", "loan_identifier") + + op.create_unique_constraint( + "unique_playtime_summary", + "playtime_summaries", + ["timestamp", "identifier_str", "collection_name", "library_name"], + ) diff --git a/src/palace/manager/scripts/playtime_entries.py b/src/palace/manager/scripts/playtime_entries.py index 3ff4c34c1b..279383081c 100644 --- a/src/palace/manager/scripts/playtime_entries.py +++ b/src/palace/manager/scripts/playtime_entries.py @@ -272,7 +272,7 @@ def _fetch_report_records(self, start: datetime, until: datetime) -> Query: ) combined_sq = combined.subquery() - query4 = self._db.query( + return self._db.query( combined_sq.c.identifier_str, combined_sq.c.collection_name, combined_sq.c.library_name, @@ -285,8 +285,6 @@ def _fetch_report_records(self, start: datetime, until: datetime) -> Query: combined_sq.c.library_name, combined_sq.c.identifier_str, ) - results = query4.all() - return results def _produce_report(writer: Writer, date_label, records=None) -> None: