forked from mtusi2026/mtusi2026lab6
68 lines
3.3 KiB
Python
68 lines
3.3 KiB
Python
"""Asynchronous image pipeline, durable jobs/outbox and replay audit."""
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision = "009"
|
|
down_revision = "008"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
op.add_column("attachments", sa.Column("status", sa.String(16), nullable=False, server_default="ready"))
|
|
op.add_column("attachments", sa.Column("error_code", sa.String(64)))
|
|
op.add_column("attachments", sa.Column("staging_key", sa.Text()))
|
|
op.add_column("attachments", sa.Column("variants", sa.JSON(), nullable=False, server_default="[]"))
|
|
op.create_check_constraint("attachments_status_check", "attachments", "status IN ('queued', 'processing', 'ready', 'failed')")
|
|
op.create_table(
|
|
"attachment_jobs",
|
|
sa.Column("id", sa.Uuid(), primary_key=True),
|
|
sa.Column("attachment_id", sa.Uuid(), sa.ForeignKey("attachments.id"), nullable=False),
|
|
sa.Column("pipeline_version", sa.Integer(), nullable=False),
|
|
sa.Column("state", sa.String(16), nullable=False),
|
|
sa.Column("attempt", sa.Integer(), nullable=False),
|
|
sa.Column("cycle_attempt", sa.Integer(), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("next_run_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("published_at", sa.DateTime(timezone=True)),
|
|
sa.Column("lease_until", sa.DateTime(timezone=True)),
|
|
sa.Column("claim_token", sa.Uuid()),
|
|
sa.Column("error_code", sa.String(64)),
|
|
sa.Column("fault", sa.String(32)),
|
|
sa.UniqueConstraint("attachment_id", "pipeline_version"),
|
|
sa.CheckConstraint("state IN ('queued', 'processing', 'ready', 'failed')"),
|
|
sa.CheckConstraint("attempt >= 1 AND cycle_attempt >= 1"),
|
|
sa.CheckConstraint("pipeline_version = 1"),
|
|
)
|
|
op.create_index("attachment_jobs_dispatch_idx", "attachment_jobs", ["state", "next_run_at"])
|
|
op.create_table(
|
|
"job_replays",
|
|
sa.Column("id", sa.Uuid(), primary_key=True),
|
|
sa.Column("job_id", sa.Uuid(), sa.ForeignKey("attachment_jobs.id"), nullable=False),
|
|
sa.Column("operator", sa.String(100), nullable=False),
|
|
sa.Column("reason", sa.String(500), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("attempt", sa.Integer(), nullable=False),
|
|
)
|
|
|
|
# Existing lab4 originals remain readable by the worker as their staging
|
|
# source. Retention must never delete this shared source/final key.
|
|
op.execute("""
|
|
INSERT INTO attachment_jobs
|
|
(id, attachment_id, pipeline_version, state, attempt, cycle_attempt,
|
|
created_at, updated_at, next_run_at)
|
|
SELECT gen_random_uuid(), id, 1, 'queued', 1, 1,
|
|
clock_timestamp(), clock_timestamp(), clock_timestamp()
|
|
FROM attachments
|
|
""")
|
|
op.execute("UPDATE attachments SET staging_key=object_key, status='queued', updated_at=clock_timestamp()")
|
|
|
|
|
|
def downgrade():
|
|
op.drop_table("job_replays")
|
|
op.drop_table("attachment_jobs")
|
|
op.drop_constraint("attachments_status_check", "attachments")
|
|
for name in ("variants", "staging_key", "error_code", "status"):
|
|
op.drop_column("attachments", name)
|