"""bot_channel 的轮询口子要容忍"空字符串"的 GUC `set_config(..., true)` 提交后会把变量还原成上一次的值;连接池里这条连接如果本来没有 会话级值,还原出来的是空串 `''` 而不是 NULL。`0014` 的策略直接 `current_setting('app.bot_channel', true)::uuid`,空串一 cast 就 `InvalidTextRepresentationError: invalid input syntax for type uuid: ""`, 于是同一连接上下一个请求(比如扫码确认)500。 这里把策略重建成和 `app_ws_id()` 一样的写法:先 `nullif(..., '')` 再 cast。 Revision ID: 0015 Revises: 0014 """ from __future__ import annotations from alembic import op from app.roles import APP_ROLE revision = "0015" down_revision = "0014" branch_labels = None depends_on = None STATEMENTS: tuple[str, ...] = ( "DROP POLICY IF EXISTS bot_channel_worker ON bot_channel", """ CREATE POLICY bot_channel_worker ON bot_channel FOR ALL USING ( id = nullif(current_setting('app.bot_channel', true), '')::uuid ) WITH CHECK ( id = nullif(current_setting('app.bot_channel', true), '')::uuid ) """, f"GRANT SELECT, INSERT, UPDATE, DELETE ON bot_channel TO {APP_ROLE}", ) def upgrade() -> None: for statement in STATEMENTS: op.execute(statement) def downgrade() -> None: op.execute("DROP POLICY IF EXISTS bot_channel_worker ON bot_channel") op.execute( """ CREATE POLICY bot_channel_worker ON bot_channel FOR ALL USING (id = current_setting('app.bot_channel', true)::uuid) WITH CHECK (id = current_setting('app.bot_channel', true)::uuid) """ )