feat: 统一自动化任务调度配置并增强聊天流恢复
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
"""move automation schedule fields into config
|
||||
|
||||
Revision ID: 202603240001
|
||||
Revises: 202603230003
|
||||
Create Date: 2026-03-24 18:20:00
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "202603240001"
|
||||
down_revision: Union[str, Sequence[str], None] = "202603230003"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE public.automation_jobs aj
|
||||
SET config = jsonb_set(
|
||||
coalesce(aj.config, '{}'::jsonb),
|
||||
'{schedule}',
|
||||
jsonb_build_object(
|
||||
'type', coalesce(aj.config->'schedule'->>'type', aj.schedule_type),
|
||||
'run_at', jsonb_build_object(
|
||||
'hour', extract(hour from (aj.run_at AT TIME ZONE aj.timezone))::int,
|
||||
'minute', extract(minute from (aj.run_at AT TIME ZONE aj.timezone))::int
|
||||
)
|
||||
) || CASE
|
||||
WHEN coalesce(aj.config->'schedule'->>'type', aj.schedule_type) = 'weekly'
|
||||
THEN jsonb_build_object(
|
||||
'weekdays',
|
||||
coalesce(
|
||||
aj.config->'schedule'->'weekdays',
|
||||
jsonb_build_array(
|
||||
extract(isodow from (aj.run_at AT TIME ZONE aj.timezone))::int
|
||||
)
|
||||
)
|
||||
)
|
||||
ELSE '{}'::jsonb
|
||||
END,
|
||||
true
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
ALTER TABLE public.automation_jobs
|
||||
DROP CONSTRAINT IF EXISTS chk_automation_job_schedule_type
|
||||
"""
|
||||
)
|
||||
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
columns = {column["name"] for column in inspector.get_columns("automation_jobs")}
|
||||
|
||||
if "schedule_type" in columns:
|
||||
op.drop_column("automation_jobs", "schedule_type")
|
||||
if "run_at" in columns:
|
||||
op.drop_column("automation_jobs", "run_at")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
columns = {column["name"] for column in inspector.get_columns("automation_jobs")}
|
||||
|
||||
if "schedule_type" not in columns:
|
||||
op.add_column(
|
||||
"automation_jobs",
|
||||
sa.Column("schedule_type", sa.String(length=20), nullable=True),
|
||||
)
|
||||
if "run_at" not in columns:
|
||||
op.add_column(
|
||||
"automation_jobs",
|
||||
sa.Column("run_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE public.automation_jobs aj
|
||||
SET schedule_type = coalesce(aj.config->'schedule'->>'type', 'daily'),
|
||||
run_at = (
|
||||
date_trunc('day', timezone(aj.timezone, now()))
|
||||
+ make_interval(
|
||||
hours => coalesce((aj.config->'schedule'->'run_at'->>'hour')::int, 8),
|
||||
mins => coalesce((aj.config->'schedule'->'run_at'->>'minute')::int, 0)
|
||||
)
|
||||
) AT TIME ZONE aj.timezone
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
ALTER TABLE public.automation_jobs
|
||||
ALTER COLUMN schedule_type SET NOT NULL,
|
||||
ALTER COLUMN run_at SET NOT NULL
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
ALTER TABLE public.automation_jobs
|
||||
ADD CONSTRAINT chk_automation_job_schedule_type
|
||||
CHECK (schedule_type IN ('daily', 'weekly'))
|
||||
"""
|
||||
)
|
||||
Reference in New Issue
Block a user