2472cfc44a
随本分支一并提交的工作区既有改动(非本次信息流发奖功能): batch_alter_table + 按方言区分 server_default,让迁移在 SQLite 本地也能跑。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
"""cps v2: 活动 payload(淘口令/京东链接) + 群多平台/sid 可空 + 点击 event_type
|
|
|
|
Revision ID: cps_v2_platforms
|
|
Revises: cps_link_tables
|
|
Create Date: 2026-06-17 11:00:00.000000
|
|
|
|
接入淘宝/京东:
|
|
- cps_activity.payload 淘宝整段淘口令文本 / 京东推广链接(美团仍用 act_id)
|
|
- cps_group.platforms 该群发哪些平台(多选);sid 改可空(纯淘宝/京东无 sid)
|
|
- cps_link.sid 可空 + target_url 加长(淘口令较长)
|
|
- cps_click.event_type visit / copy(淘宝落地页"复制口令")
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
|
|
revision: str = "cps_v2_platforms"
|
|
down_revision: Union[str, Sequence[str], None] = "cps_link_tables"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# 活动:淘宝淘口令 / 京东链接
|
|
op.add_column("cps_activity", sa.Column("payload", sa.Text(), nullable=True))
|
|
|
|
# 群:多平台(现有群都是美团,server_default 回填)+ sid 可空
|
|
# server_default 按方言区分:Postgres 用 ::jsonb 转换;SQLite(本地)用纯 JSON 文本字面量
|
|
#(SQLite 不认 ::jsonb,会报 unrecognized token: ":")。
|
|
bind = op.get_bind()
|
|
platforms_default = (
|
|
sa.text("'[\"meituan\"]'::jsonb")
|
|
if bind.dialect.name == "postgresql"
|
|
else sa.text("'[\"meituan\"]'")
|
|
)
|
|
# alter_column 在 SQLite 原生不支持 ALTER COLUMN,必须走 batch(重建表);Postgres 下 batch 直接 ALTER。
|
|
# 用 batch_alter_table 跨方言通吃(render_as_batch 只管 autogenerate 渲染,不会自动包裹手写的 alter_column)。
|
|
with op.batch_alter_table("cps_group") as batch_op:
|
|
batch_op.add_column(
|
|
sa.Column(
|
|
"platforms",
|
|
sa.JSON().with_variant(postgresql.JSONB(), "postgresql"),
|
|
nullable=False,
|
|
server_default=platforms_default,
|
|
)
|
|
)
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=True)
|
|
|
|
# link:sid 可空 + target 加长
|
|
with op.batch_alter_table("cps_link") as batch_op:
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=True)
|
|
batch_op.alter_column("target_url", existing_type=sa.String(1024), type_=sa.String(2048))
|
|
|
|
# click:sid 可空 + 事件类型
|
|
with op.batch_alter_table("cps_click") as batch_op:
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=True)
|
|
batch_op.add_column(
|
|
sa.Column("event_type", sa.String(16), nullable=False, server_default="visit")
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
with op.batch_alter_table("cps_click") as batch_op:
|
|
batch_op.drop_column("event_type")
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=False)
|
|
with op.batch_alter_table("cps_link") as batch_op:
|
|
batch_op.alter_column("target_url", existing_type=sa.String(2048), type_=sa.String(1024))
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=False)
|
|
with op.batch_alter_table("cps_group") as batch_op:
|
|
batch_op.alter_column("sid", existing_type=sa.String(64), nullable=False)
|
|
batch_op.drop_column("platforms")
|
|
op.drop_column("cps_activity", "payload")
|