Files
PlantGuideScraper/backend/alembic/versions/002_add_cached_stats_and_indexes.py
2026-04-12 09:54:27 -05:00

54 lines
1.5 KiB
Python

"""Add cached_stats table and license index
Revision ID: 002
Revises: 001
Create Date: 2025-01-25
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = '002'
down_revision: Union[str, None] = '001'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Cached stats table for pre-calculated dashboard statistics
op.create_table(
'cached_stats',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('key', sa.String(50), nullable=False, unique=True),
sa.Column('value', sa.Text(), nullable=False),
sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now()),
)
op.create_index('ix_cached_stats_key', 'cached_stats', ['key'])
# Add license index to images table (if not exists)
# Using batch mode for SQLite compatibility
try:
op.create_index('ix_images_license', 'images', ['license'])
except Exception:
pass # Index may already exist
# Add only_without_images column to jobs if it doesn't exist
try:
op.add_column('jobs', sa.Column('only_without_images', sa.Boolean(), default=False))
except Exception:
pass # Column may already exist
def downgrade() -> None:
try:
op.drop_index('ix_images_license', 'images')
except Exception:
pass
try:
op.drop_column('jobs', 'only_without_images')
except Exception:
pass
op.drop_table('cached_stats')