58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
"""Add detail images to homepage wild archive cases.
|
|
|
|
Revision ID: 0019_home_wild_archive_images
|
|
Revises: 0018_home_wanfa_recommendations
|
|
"""
|
|
|
|
from alembic import context, op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import inspect
|
|
from sqlalchemy import inspect
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
|
|
revision = "0019_home_wild_archive_images"
|
|
down_revision = "0018_home_wanfa_recommendations"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def should_add_images_column(existing_columns: set[str]) -> bool:
|
|
return "images" not in existing_columns
|
|
|
|
|
|
def upgrade() -> None:
|
|
if context.is_offline_mode():
|
|
add_images_column = True
|
|
else:
|
|
bind = op.get_bind()
|
|
existing_columns = {column["name"] for column in inspect(bind).get_columns("HomeWildArchive")}
|
|
add_images_column = should_add_images_column(existing_columns)
|
|
|
|
if add_images_column:
|
|
op.add_column(
|
|
"HomeWildArchive",
|
|
sa.Column(
|
|
"images",
|
|
postgresql.JSONB(astext_type=sa.Text()),
|
|
nullable=False,
|
|
server_default=sa.text("'[]'::jsonb"),
|
|
),
|
|
)
|
|
op.execute(
|
|
sa.text(
|
|
'UPDATE "HomeWildArchive" '
|
|
'SET "images" = jsonb_build_array("image") '
|
|
'WHERE "images" IS NULL OR CASE '
|
|
'WHEN jsonb_typeof("images") = \'array\' '
|
|
'THEN jsonb_array_length("images") = 0 '
|
|
'ELSE TRUE END'
|
|
)
|
|
)
|
|
if add_images_column:
|
|
op.alter_column("HomeWildArchive", "images", server_default=None)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_column("HomeWildArchive", "images")
|