114 lines
4.6 KiB
Python
114 lines
4.6 KiB
Python
"""Состояния, варианты и устойчивые jobs с намерением публикации."""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
revision = "c35e91a2f706"
|
|
down_revision = "8f31b94a2d10"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.drop_constraint(op.f("ck_attachments_status"), "attachments", type_="check")
|
|
op.create_check_constraint(
|
|
op.f("ck_attachments_status"),
|
|
"attachments",
|
|
"status IN ('queued', 'processing', 'ready', 'failed')",
|
|
)
|
|
op.alter_column("attachments", "status", server_default="queued")
|
|
op.add_column("attachments", sa.Column("error_code", sa.String(64), nullable=True))
|
|
op.add_column(
|
|
"attachments", sa.Column("thumbnail_key", sa.String(128), nullable=True)
|
|
)
|
|
op.add_column(
|
|
"attachments",
|
|
sa.Column(
|
|
"variants",
|
|
postgresql.JSONB(),
|
|
nullable=False,
|
|
server_default=sa.text("'[]'::jsonb"),
|
|
),
|
|
)
|
|
op.create_check_constraint(
|
|
op.f("ck_attachments_error_code"),
|
|
"attachments",
|
|
"(status = 'failed' AND error_code IS NOT NULL) OR (status <> 'failed' AND error_code IS NULL)",
|
|
)
|
|
op.create_table(
|
|
"attachment_jobs",
|
|
sa.Column("id", sa.UUID(), nullable=False),
|
|
sa.Column("attachment_id", sa.UUID(), nullable=False),
|
|
sa.Column("staging_key", sa.String(128), nullable=True),
|
|
sa.Column("pipeline_version", sa.Integer(), server_default="1", nullable=False),
|
|
sa.Column("attempt", sa.Integer(), server_default="0", nullable=False),
|
|
sa.Column(
|
|
"published_attempt", sa.Integer(), server_default="0", nullable=False
|
|
),
|
|
sa.Column(
|
|
"next_attempt_at",
|
|
sa.DateTime(timezone=True),
|
|
server_default=sa.text("clock_timestamp()"),
|
|
nullable=False,
|
|
),
|
|
sa.Column("lease_token", sa.UUID(), nullable=True),
|
|
sa.Column("lease_until", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("last_error_code", sa.String(64), nullable=True),
|
|
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
server_default=sa.text("clock_timestamp()"),
|
|
nullable=False,
|
|
),
|
|
sa.PrimaryKeyConstraint("id", name=op.f("pk_attachment_jobs")),
|
|
sa.ForeignKeyConstraint(
|
|
["attachment_id"],
|
|
["attachments.id"],
|
|
name=op.f("fk_attachment_jobs_attachment_id_attachments"),
|
|
),
|
|
sa.UniqueConstraint(
|
|
"attachment_id", name=op.f("uq_attachment_jobs_attachment_id")
|
|
),
|
|
sa.CheckConstraint(
|
|
"pipeline_version = 1", name=op.f("ck_attachment_jobs_pipeline_version")
|
|
),
|
|
sa.CheckConstraint(
|
|
"attempt >= 0 AND published_attempt >= 0",
|
|
name=op.f("ck_attachment_jobs_attempt"),
|
|
),
|
|
)
|
|
op.create_index("ix_attachment_jobs_due", "attachment_jobs", ["next_attempt_at"])
|
|
op.create_index("ix_attachment_jobs_lease", "attachment_jobs", ["lease_until"])
|
|
# Прежние оригиналы остаются источником; worker создаст отсутствующий thumbnail.
|
|
op.execute(
|
|
"INSERT INTO attachment_jobs (id, attachment_id, staging_key) SELECT id, id, object_key FROM attachments"
|
|
)
|
|
op.execute(
|
|
"UPDATE attachments SET status = 'queued', updated_at = clock_timestamp()"
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
connection = op.get_bind()
|
|
unfinished = connection.scalar(
|
|
sa.text("SELECT count(*) FROM attachments WHERE status <> 'ready'")
|
|
)
|
|
if unfinished:
|
|
raise RuntimeError(
|
|
"Перед downgrade нужно завершить или вручную разобрать все queued/processing/failed вложения"
|
|
)
|
|
op.drop_index("ix_attachment_jobs_lease", table_name="attachment_jobs")
|
|
op.drop_index("ix_attachment_jobs_due", table_name="attachment_jobs")
|
|
op.drop_table("attachment_jobs")
|
|
op.drop_constraint(op.f("ck_attachments_error_code"), "attachments", type_="check")
|
|
op.drop_column("attachments", "variants")
|
|
op.drop_column("attachments", "thumbnail_key")
|
|
op.drop_column("attachments", "error_code")
|
|
op.drop_constraint(op.f("ck_attachments_status"), "attachments", type_="check")
|
|
op.create_check_constraint(
|
|
op.f("ck_attachments_status"), "attachments", "status = 'ready'"
|
|
)
|
|
op.alter_column("attachments", "status", server_default="ready")
|