147 lines
6.5 KiB
Python
147 lines
6.5 KiB
Python
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
revision = "0004_jobs"
|
|
down_revision = "0003_attachments"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.drop_constraint("attachments_status_check", "attachments", type_="check")
|
|
op.create_check_constraint(
|
|
"attachments_status_check",
|
|
"attachments",
|
|
"status IN ('pending', 'queued', 'processing', 'ready', 'failed')",
|
|
)
|
|
for name in ("staging_key", "original_key"):
|
|
op.add_column("attachments", sa.Column(name, sa.Text(collation="C"), nullable=True))
|
|
op.create_unique_constraint(f"attachments_{name}_key", "attachments", [name])
|
|
op.add_column(
|
|
"attachments",
|
|
sa.Column("published_version", sa.Integer(), nullable=False, server_default="0"),
|
|
)
|
|
op.create_check_constraint(
|
|
"attachments_published_version_check", "attachments", "published_version >= 0"
|
|
)
|
|
op.add_column("attachments", sa.Column("error_code", sa.Text(), nullable=True))
|
|
op.create_check_constraint(
|
|
"attachments_error_code_check",
|
|
"attachments",
|
|
"error_code IN ('invalid_image', 'processing_failed', 'retry_exhausted')",
|
|
)
|
|
op.alter_column("attachments", "object_key", nullable=True)
|
|
attachments = sa.table(
|
|
"attachments",
|
|
sa.column("status"),
|
|
sa.column("object_key"),
|
|
sa.column("original_key"),
|
|
sa.column("staging_key"),
|
|
sa.column("published_version"),
|
|
)
|
|
op.execute(
|
|
attachments.update()
|
|
.where(attachments.c.status == "ready")
|
|
.values(original_key=attachments.c.object_key, published_version=1)
|
|
)
|
|
op.execute(
|
|
attachments.update()
|
|
.where(attachments.c.status == "pending")
|
|
.values(staging_key=attachments.c.object_key)
|
|
)
|
|
op.create_index(
|
|
"attachments_staged_idx",
|
|
"attachments",
|
|
["updated_at"],
|
|
postgresql_where=sa.column("staging_key").is_not(None),
|
|
)
|
|
op.create_table(
|
|
"attachment_variants",
|
|
sa.Column("attachment_id", sa.Uuid(), sa.ForeignKey("attachments.id"), nullable=False),
|
|
sa.Column("pipeline_version", sa.Integer(), nullable=False),
|
|
sa.Column("name", sa.Text(), nullable=False),
|
|
sa.Column("object_key", sa.Text(collation="C"), nullable=False),
|
|
sa.Column("content_type", sa.Text(), nullable=False),
|
|
sa.Column("size_bytes", sa.BigInteger(), nullable=False),
|
|
sa.Column("width", sa.Integer(), nullable=False),
|
|
sa.Column("height", sa.Integer(), nullable=False),
|
|
sa.Column("sha256", sa.Text(), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.PrimaryKeyConstraint("attachment_id", "pipeline_version", "name"),
|
|
sa.UniqueConstraint("object_key", name="attachment_variants_object_key_key"),
|
|
sa.CheckConstraint(
|
|
"pipeline_version >= 1", name="attachment_variants_pipeline_version_check"
|
|
),
|
|
sa.CheckConstraint(
|
|
"name IN ('thumbnail', 'crop', 'optimized')", name="attachment_variants_name_check"
|
|
),
|
|
sa.CheckConstraint(
|
|
"content_type IN ('image/jpeg', 'image/png', 'image/webp')",
|
|
name="attachment_variants_content_type_check",
|
|
),
|
|
sa.CheckConstraint("size_bytes >= 1", name="attachment_variants_size_bytes_check"),
|
|
sa.CheckConstraint("width >= 1", name="attachment_variants_width_check"),
|
|
sa.CheckConstraint("height >= 1", name="attachment_variants_height_check"),
|
|
sa.CheckConstraint("sha256 ~ '^[0-9a-f]{64}$'", name="attachment_variants_sha256_check"),
|
|
)
|
|
op.create_table(
|
|
"jobs",
|
|
sa.Column("job_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("status", sa.Text(), nullable=False),
|
|
sa.Column("attempt", sa.Integer(), nullable=False),
|
|
sa.Column("claims", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("lease_token", sa.Uuid(), nullable=True),
|
|
sa.Column("lease_until", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("next_attempt_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("last_error", sa.Text(), nullable=True),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.UniqueConstraint("attachment_id", "pipeline_version", name="jobs_effect_key"),
|
|
sa.CheckConstraint("pipeline_version >= 1", name="jobs_pipeline_version_check"),
|
|
sa.CheckConstraint(
|
|
"status IN ('queued', 'processing', 'done', 'failed', 'superseded')",
|
|
name="jobs_status_check",
|
|
),
|
|
sa.CheckConstraint("attempt >= 1", name="jobs_attempt_check"),
|
|
sa.CheckConstraint("claims >= 0", name="jobs_claims_check"),
|
|
)
|
|
op.create_index(
|
|
"jobs_lease_idx",
|
|
"jobs",
|
|
["lease_until"],
|
|
postgresql_where=sa.column("status") == "processing",
|
|
)
|
|
op.create_table(
|
|
"outbox",
|
|
sa.Column("id", sa.BigInteger(), sa.Identity(always=True), primary_key=True),
|
|
sa.Column("job_id", sa.Uuid(), sa.ForeignKey("jobs.job_id"), nullable=False),
|
|
sa.Column("payload", postgresql.JSONB(), nullable=False),
|
|
sa.Column("available_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
|
|
)
|
|
op.create_index(
|
|
"outbox_pending_idx",
|
|
"outbox",
|
|
["available_at", "id"],
|
|
postgresql_where=sa.column("published_at").is_(None),
|
|
)
|
|
op.create_index("outbox_job_idx", "outbox", ["job_id"])
|
|
op.create_table(
|
|
"job_events",
|
|
sa.Column("id", sa.BigInteger(), sa.Identity(always=True), primary_key=True),
|
|
sa.Column("job_id", sa.Uuid(), nullable=False),
|
|
sa.Column("attachment_id", sa.Uuid(), nullable=False),
|
|
sa.Column("kind", sa.Text(), nullable=False),
|
|
sa.Column("attempt", sa.Integer(), nullable=True),
|
|
sa.Column("detail", postgresql.JSONB(), nullable=False, server_default="{}"),
|
|
sa.Column("at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
|
)
|
|
op.create_index("job_events_attachment_idx", "job_events", ["attachment_id", "id"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
raise RuntimeError("Downgrade would delete pipeline data")
|