Files
mtusi2026lab6/migrations/versions/25c48b226c14_lab5_attachment_statuses_jobs_outbox.py
T

78 lines
2.2 KiB
Python

"""lab5: attachment statuses, jobs outbox
Revision ID: <новый_hash>
Revises: c3f18af4ec56
Create Date: <оставь>
"""
from __future__ import annotations
from alembic import op
revision = "25c48b226c14"
down_revision = "c3f18af4ec56"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("""
ALTER TABLE attachments
DROP CONSTRAINT IF EXISTS attachments_status_check
""")
op.execute("""
ALTER TABLE attachments
ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued', 'processing', 'ready', 'failed'))
""")
op.execute("""
ALTER TABLE attachments
ALTER COLUMN status SET DEFAULT 'queued'
""")
op.execute("""
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS attempt INTEGER NOT NULL DEFAULT 0
""")
op.execute("""
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS staging_key TEXT
""")
op.execute("""
CREATE TABLE IF NOT EXISTS jobs (
job_id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL DEFAULT 1,
attempt INTEGER NOT NULL DEFAULT 1,
status TEXT NOT NULL DEFAULT 'pending',
created_at TIMESTAMPTZ NOT NULL,
published_at TIMESTAMPTZ,
CONSTRAINT jobs_status_check
CHECK (status IN ('pending', 'published', 'failed'))
)
""")
op.execute("""
CREATE INDEX IF NOT EXISTS jobs_pending_idx
ON jobs (status, created_at)
WHERE status = 'pending'
""")
op.execute("""
CREATE INDEX IF NOT EXISTS jobs_attachment_idx
ON jobs (attachment_id)
""")
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS jobs")
op.execute("ALTER TABLE attachments DROP COLUMN IF EXISTS staging_key")
op.execute("ALTER TABLE attachments DROP COLUMN IF EXISTS attempt")
op.execute("""
ALTER TABLE attachments
DROP CONSTRAINT IF EXISTS attachments_status_check
""")
op.execute("""
ALTER TABLE attachments
ADD CONSTRAINT attachments_status_check
CHECK (status = 'ready')
""")