diff --git a/alembic/versions/cps_v2_platforms.py b/alembic/versions/cps_v2_platforms.py index e80c1a6..75b53a1 100644 --- a/alembic/versions/cps_v2_platforms.py +++ b/alembic/versions/cps_v2_platforms.py @@ -28,34 +28,48 @@ def upgrade() -> None: op.add_column("cps_activity", sa.Column("payload", sa.Text(), nullable=True)) # 群:多平台(现有群都是美团,server_default 回填)+ sid 可空 - op.add_column( - "cps_group", - sa.Column( - "platforms", - sa.JSON().with_variant(postgresql.JSONB(), "postgresql"), - nullable=False, - server_default=sa.text("'[\"meituan\"]'::jsonb"), - ), + # 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\"]'") ) - op.alter_column("cps_group", "sid", existing_type=sa.String(64), nullable=True) + # 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 加长 - op.alter_column("cps_link", "sid", existing_type=sa.String(64), nullable=True) - op.alter_column("cps_link", "target_url", existing_type=sa.String(1024), type_=sa.String(2048)) + 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 可空 + 事件类型 - op.alter_column("cps_click", "sid", existing_type=sa.String(64), nullable=True) - op.add_column( - "cps_click", - sa.Column("event_type", sa.String(16), nullable=False, server_default="visit"), - ) + 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: - op.drop_column("cps_click", "event_type") - op.alter_column("cps_click", "sid", existing_type=sa.String(64), nullable=False) - op.alter_column("cps_link", "target_url", existing_type=sa.String(2048), type_=sa.String(1024)) - op.alter_column("cps_link", "sid", existing_type=sa.String(64), nullable=False) - op.drop_column("cps_group", "platforms") - op.alter_column("cps_group", "sid", existing_type=sa.String(64), nullable=False) + 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")