211 lines
6.0 KiB
Python
211 lines
6.0 KiB
Python
|
|
"""add durable image processing jobs"""
|
|
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision: str = "a9d670bf1c16"
|
|
down_revision: Union[str, None] = "a7c2e91d5f40"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Дополнительные поля для хранения исходника и версии конвейера.
|
|
op.add_column(
|
|
"attachments",
|
|
sa.Column("staging_key", sa.String(length=512), nullable=True),
|
|
)
|
|
op.create_unique_constraint(
|
|
"uq_attachments_staging_key",
|
|
"attachments",
|
|
["staging_key"],
|
|
)
|
|
op.add_column(
|
|
"attachments",
|
|
sa.Column(
|
|
"pipeline_version",
|
|
sa.Integer(),
|
|
nullable=False,
|
|
server_default="1",
|
|
),
|
|
)
|
|
|
|
# Задания на асинхронную обработку.
|
|
op.create_table(
|
|
"processing_jobs",
|
|
sa.Column("id", sa.UUID(), nullable=False),
|
|
sa.Column("attachment_id", sa.UUID(), nullable=False),
|
|
sa.Column(
|
|
"pipeline_version",
|
|
sa.Integer(),
|
|
nullable=False,
|
|
server_default="1",
|
|
),
|
|
sa.Column(
|
|
"attempt",
|
|
sa.Integer(),
|
|
nullable=False,
|
|
server_default="1",
|
|
),
|
|
sa.Column(
|
|
"status",
|
|
sa.String(length=16),
|
|
nullable=False,
|
|
server_default="queued",
|
|
),
|
|
sa.Column(
|
|
"available_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
),
|
|
sa.Column("lease_until", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("worker_id", sa.String(length=128), nullable=True),
|
|
sa.Column(
|
|
"last_error_code",
|
|
sa.String(length=64),
|
|
nullable=True,
|
|
),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
),
|
|
sa.Column(
|
|
"updated_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["attachment_id"],
|
|
["attachments.id"],
|
|
ondelete="CASCADE",
|
|
),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint(
|
|
"attachment_id",
|
|
"pipeline_version",
|
|
name="uq_processing_job_attachment_pipeline",
|
|
),
|
|
)
|
|
op.create_index(
|
|
"ix_processing_jobs_status_available",
|
|
"processing_jobs",
|
|
["status", "available_at"],
|
|
)
|
|
op.create_index(
|
|
"ix_processing_jobs_lease_until",
|
|
"processing_jobs",
|
|
["lease_until"],
|
|
)
|
|
|
|
# Созданные варианты изображений.
|
|
op.create_table(
|
|
"attachment_variants",
|
|
sa.Column("id", sa.UUID(), nullable=False),
|
|
sa.Column("attachment_id", sa.UUID(), nullable=False),
|
|
sa.Column("variant", sa.String(length=32), nullable=False),
|
|
sa.Column("object_key", sa.String(length=512), nullable=False),
|
|
sa.Column("content_type", sa.String(length=32), nullable=False),
|
|
sa.Column("width", sa.Integer(), nullable=False),
|
|
sa.Column("height", sa.Integer(), nullable=False),
|
|
sa.Column("size_bytes", sa.Integer(), nullable=False),
|
|
sa.Column("sha256", sa.String(length=64), nullable=False),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["attachment_id"],
|
|
["attachments.id"],
|
|
ondelete="CASCADE",
|
|
),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint("object_key"),
|
|
sa.UniqueConstraint(
|
|
"attachment_id",
|
|
"variant",
|
|
name="uq_attachment_variant",
|
|
),
|
|
)
|
|
op.create_index(
|
|
"ix_attachment_variants_attachment_id",
|
|
"attachment_variants",
|
|
["attachment_id"],
|
|
)
|
|
|
|
# Transactional outbox: события публикуются в брокер
|
|
# отдельным процессом после фиксации транзакции.
|
|
op.create_table(
|
|
"outbox_events",
|
|
sa.Column("id", sa.UUID(), nullable=False),
|
|
sa.Column("event_type", sa.String(length=128), nullable=False),
|
|
sa.Column("aggregate_id", sa.UUID(), nullable=False),
|
|
sa.Column("payload", sa.JSON(), nullable=False),
|
|
sa.Column(
|
|
"status",
|
|
sa.String(length=16),
|
|
nullable=False,
|
|
server_default="pending",
|
|
),
|
|
sa.Column(
|
|
"attempts",
|
|
sa.Integer(),
|
|
nullable=False,
|
|
server_default="0",
|
|
),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
),
|
|
sa.Column(
|
|
"published_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=True,
|
|
),
|
|
sa.Column("last_error", sa.String(length=256), nullable=True),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
op.create_index(
|
|
"ix_outbox_events_status_created",
|
|
"outbox_events",
|
|
["status", "created_at"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index(
|
|
"ix_outbox_events_status_created",
|
|
table_name="outbox_events",
|
|
)
|
|
op.drop_table("outbox_events")
|
|
|
|
op.drop_index(
|
|
"ix_attachment_variants_attachment_id",
|
|
table_name="attachment_variants",
|
|
)
|
|
op.drop_table("attachment_variants")
|
|
|
|
op.drop_index(
|
|
"ix_processing_jobs_lease_until",
|
|
table_name="processing_jobs",
|
|
)
|
|
op.drop_index(
|
|
"ix_processing_jobs_status_available",
|
|
table_name="processing_jobs",
|
|
)
|
|
op.drop_table("processing_jobs")
|
|
|
|
op.drop_column("attachments", "pipeline_version")
|
|
op.drop_constraint(
|
|
"uq_attachments_staging_key",
|
|
"attachments",
|
|
type_="unique",
|
|
)
|
|
op.drop_column("attachments", "staging_key")
|