Skip to content

Commit

Permalink
Add migration to add loan_identifier columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Aug 22, 2024
1 parent 608cdac commit 7938839
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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"],
)
4 changes: 1 addition & 3 deletions src/palace/manager/scripts/playtime_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit 7938839

Please sign in to comment.