"""Create the customer browse history table for public record pages.""" from alembic import context, op import sqlalchemy as sa from sqlalchemy import inspect from app.migration_compat import baseline_table_names revision = "0036_customer_browse_history" down_revision = "0035_concierge_details_jsonb" branch_labels = None depends_on = None TABLE_NAME = "CustomerBrowseHistory" CUSTOMER_HISTORY_INDEXES = { "ix_CustomerBrowseHistory_customerId", "ix_CustomerBrowseHistory_customer_visitedAt", } def is_offline_mode() -> bool: try: return context.is_offline_mode() except (NameError, AttributeError): return False def table_names() -> set[str]: if is_offline_mode(): return baseline_table_names() return set(inspect(op.get_bind()).get_table_names()) def should_create_table(existing_tables: set[str]) -> bool: return TABLE_NAME not in existing_tables def upgrade() -> None: if not should_create_table(table_names()): return op.create_table( TABLE_NAME, sa.Column("id", sa.String(), nullable=False), sa.Column("customerId", sa.String(), nullable=False), sa.Column("itemType", sa.String(), nullable=False), sa.Column("itemId", sa.String(), nullable=False), sa.Column("title", sa.String(), nullable=False), sa.Column("image", sa.String(), nullable=False), sa.Column("visitedAt", sa.DateTime(), nullable=False), sa.Column("createdAt", sa.DateTime(), nullable=False), sa.Column("updatedAt", sa.DateTime(), nullable=False), sa.ForeignKeyConstraint( ["customerId"], ["Customer.id"], name="fk_CustomerBrowseHistory_customerId_Customer", ondelete="CASCADE", ), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint( "customerId", "itemType", "itemId", name="uq_CustomerBrowseHistory_customer_item", ), ) op.create_index( "ix_CustomerBrowseHistory_customerId", TABLE_NAME, ["customerId"], ) op.create_index( "ix_CustomerBrowseHistory_customer_visitedAt", TABLE_NAME, ["customerId", "visitedAt"], ) def downgrade() -> None: if should_create_table(table_names()): return op.drop_index( "ix_CustomerBrowseHistory_customer_visitedAt", table_name=TABLE_NAME, ) op.drop_index( "ix_CustomerBrowseHistory_customerId", table_name=TABLE_NAME, ) op.drop_table(TABLE_NAME)