solution
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from app.core.config import Settings
|
||||
from app.models import Base
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name, disable_existing_loggers=False)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
explicit = config.get_main_option("sqlalchemy.url")
|
||||
if explicit:
|
||||
return explicit
|
||||
return Settings().get_database_url()
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=_database_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
compare_type=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
section = config.get_section(config.config_ini_section, {})
|
||||
section["sqlalchemy.url"] = _database_url().replace("%", "%%")
|
||||
connectable = async_engine_from_config(
|
||||
section, prefix="sqlalchemy.", poolclass=pool.NullPool
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,25 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Добавляет результаты идемпотентных запросов."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "49d2c947a3f1"
|
||||
down_revision = "abd273076068"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Создаёт таблицу ключей без изменения существующих данных."""
|
||||
op.create_table(
|
||||
"idempotency_keys",
|
||||
sa.Column("user_id", sa.UUID(), nullable=False),
|
||||
sa.Column("method", sa.String(8), nullable=False),
|
||||
sa.Column("path", sa.String(255), nullable=False),
|
||||
sa.Column("key", sa.String(128), nullable=False),
|
||||
sa.Column("request_hash", sa.String(64), nullable=False),
|
||||
sa.Column("message_id", sa.UUID(), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["user_id"], ["users.id"], name=op.f("fk_idempotency_keys_user_id_users")
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["message_id"],
|
||||
["messages.id"],
|
||||
name=op.f("fk_idempotency_keys_message_id_messages"),
|
||||
),
|
||||
sa.PrimaryKeyConstraint(
|
||||
"user_id", "method", "path", "key", name=op.f("pk_idempotency_keys")
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_idempotency_keys_created_at", "idempotency_keys", ["created_at"]
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Удаляет таблицу результатов запросов."""
|
||||
op.drop_index("ix_idempotency_keys_created_at", table_name="idempotency_keys")
|
||||
op.drop_table("idempotency_keys")
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Attachment, связи сообщений и устойчивый журнал загрузок."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "8f31b94a2d10"
|
||||
down_revision = "49d2c947a3f1"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"attachments",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("chat_id", sa.UUID(), nullable=False),
|
||||
sa.Column("owner_id", sa.UUID(), nullable=False),
|
||||
sa.Column("object_key", sa.String(128), nullable=False),
|
||||
sa.Column("filename", sa.String(255), nullable=False),
|
||||
sa.Column("content_type", sa.String(32), nullable=False),
|
||||
sa.Column("size_bytes", sa.Integer(), nullable=False),
|
||||
sa.Column("sha256", sa.String(64), nullable=False),
|
||||
sa.Column("status", sa.String(16), server_default="ready", nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"size_bytes BETWEEN 1 AND 10485760", name=op.f("ck_attachments_size_bytes")
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"content_type IN ('image/png', 'image/jpeg')",
|
||||
name=op.f("ck_attachments_content_type"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"sha256 ~ '^[a-f0-9]{64}$'", name=op.f("ck_attachments_sha256")
|
||||
),
|
||||
sa.CheckConstraint("status = 'ready'", name=op.f("ck_attachments_status")),
|
||||
sa.ForeignKeyConstraint(
|
||||
["chat_id"], ["chats.id"], name=op.f("fk_attachments_chat_id_chats")
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["owner_id"], ["users.id"], name=op.f("fk_attachments_owner_id_users")
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_attachments")),
|
||||
sa.UniqueConstraint("object_key", name=op.f("uq_attachments_object_key")),
|
||||
)
|
||||
op.create_index("ix_attachments_chat_id", "attachments", ["chat_id"])
|
||||
op.create_table(
|
||||
"message_attachments",
|
||||
sa.Column("message_id", sa.UUID(), nullable=False),
|
||||
sa.Column("attachment_id", sa.UUID(), nullable=False),
|
||||
sa.Column("position", sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["message_id"],
|
||||
["messages.id"],
|
||||
ondelete="CASCADE",
|
||||
name=op.f("fk_message_attachments_message_id_messages"),
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["attachment_id"],
|
||||
["attachments.id"],
|
||||
name=op.f("fk_message_attachments_attachment_id_attachments"),
|
||||
),
|
||||
sa.PrimaryKeyConstraint(
|
||||
"message_id", "attachment_id", name=op.f("pk_message_attachments")
|
||||
),
|
||||
)
|
||||
op.create_table(
|
||||
"upload_intents",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("object_key", sa.String(128), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_upload_intents")),
|
||||
sa.UniqueConstraint("object_key", name=op.f("uq_upload_intents_object_key")),
|
||||
)
|
||||
op.create_index("ix_upload_intents_created_at", "upload_intents", ["created_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_upload_intents_created_at", table_name="upload_intents")
|
||||
op.drop_table("upload_intents")
|
||||
op.drop_table("message_attachments")
|
||||
op.drop_index("ix_attachments_chat_id", table_name="attachments")
|
||||
op.drop_table("attachments")
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Offline cutover: повторно опубликовать queued jobs в RabbitMQ."""
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "a62c950e18b3"
|
||||
down_revision = "f9183d27a640"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE attachment_jobs j SET published_attempt = attempt
|
||||
FROM attachments a WHERE a.id = j.attachment_id AND a.status = 'queued'
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
|
||||
upgrade()
|
||||
@@ -0,0 +1,32 @@
|
||||
"""add counters
|
||||
|
||||
Revision ID: abd273076068
|
||||
Revises: db06fd3126ff
|
||||
Create Date: 2026-10-26 22:01:01.910396
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "abd273076068"
|
||||
down_revision: Union[str, None] = "db06fd3126ff"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
|
||||
op.create_table(
|
||||
"counters",
|
||||
sa.Column("name", sa.String(length=64), nullable=False),
|
||||
sa.Column("value", sa.BigInteger(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("name", name=op.f("pk_counters")),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
|
||||
op.drop_table("counters")
|
||||
@@ -0,0 +1,19 @@
|
||||
"""Сохранить W3C context между HTTP-транзакцией и publisher."""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "b55d0c6819ab"
|
||||
down_revision = "a62c950e18b3"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column(
|
||||
"attachment_jobs", sa.Column("trace_context", sa.JSON(), nullable=True)
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column("attachment_jobs", "trace_context")
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Состояния, варианты и устойчивые 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"])
|
||||
|
||||
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")
|
||||
@@ -0,0 +1,105 @@
|
||||
"""create initial schema
|
||||
|
||||
Revision ID: db06fd3126ff
|
||||
Revises:
|
||||
Create Date: 2026-10-26 17:09:16.675339
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "db06fd3126ff"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
|
||||
op.create_table(
|
||||
"chats",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("title", sa.String(length=100), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_chats")),
|
||||
)
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("username", sa.String(length=32), nullable=False),
|
||||
sa.Column("display_name", sa.String(length=100), nullable=False),
|
||||
sa.Column("password_hash", sa.String(length=255), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_users")),
|
||||
sa.UniqueConstraint("username", name=op.f("uq_users_username")),
|
||||
)
|
||||
op.create_table(
|
||||
"chat_members",
|
||||
sa.Column("chat_id", sa.UUID(), nullable=False),
|
||||
sa.Column("user_id", sa.UUID(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["chat_id"],
|
||||
["chats.id"],
|
||||
name=op.f("fk_chat_members_chat_id_chats"),
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["user_id"], ["users.id"], name=op.f("fk_chat_members_user_id_users")
|
||||
),
|
||||
sa.PrimaryKeyConstraint("chat_id", "user_id", name=op.f("pk_chat_members")),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_chat_members_user_id_chat_id",
|
||||
"chat_members",
|
||||
["user_id", "chat_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_table(
|
||||
"messages",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("chat_id", sa.UUID(), nullable=False),
|
||||
sa.Column("sender_id", sa.UUID(), nullable=False),
|
||||
sa.Column("text", sa.Text(), nullable=False),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["chat_id"], ["chats.id"], name=op.f("fk_messages_chat_id_chats")
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["sender_id"], ["users.id"], name=op.f("fk_messages_sender_id_users")
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_messages")),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_messages_chat_id_created_at_id",
|
||||
"messages",
|
||||
["chat_id", "created_at", "id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
|
||||
op.drop_index("ix_messages_chat_id_created_at_id", table_name="messages")
|
||||
op.drop_table("messages")
|
||||
op.drop_index("ix_chat_members_user_id_chat_id", table_name="chat_members")
|
||||
op.drop_table("chat_members")
|
||||
op.drop_table("users")
|
||||
op.drop_table("chats")
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Ключи crop/WebP и дообработка старых ready-вложений."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "e7048b6da913"
|
||||
down_revision = "c35e91a2f706"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("attachments", sa.Column("crop_key", sa.String(128), nullable=True))
|
||||
op.add_column(
|
||||
"attachments", sa.Column("optimized_key", sa.String(128), nullable=True)
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
INSERT INTO upload_intents (id, object_key)
|
||||
SELECT gen_random_uuid(), j.staging_key
|
||||
FROM attachment_jobs j JOIN attachments a ON a.id = j.attachment_id
|
||||
WHERE a.status = 'ready' AND j.staging_key IS NOT NULL
|
||||
AND j.staging_key <> a.object_key
|
||||
ON CONFLICT (object_key) DO NOTHING
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE attachment_jobs j
|
||||
SET staging_key = a.object_key, attempt = 0, published_attempt = 0,
|
||||
next_attempt_at = clock_timestamp(), lease_token = NULL, lease_until = NULL,
|
||||
last_error_code = NULL, finished_at = NULL
|
||||
FROM attachments a
|
||||
WHERE a.id = j.attachment_id AND a.status = 'ready'
|
||||
"""
|
||||
)
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE attachments SET status = 'queued', variants = '[]'::jsonb,
|
||||
updated_at = clock_timestamp()
|
||||
WHERE status = 'ready'
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
active = op.get_bind().scalar(
|
||||
sa.text(
|
||||
"SELECT count(*) FROM attachments WHERE status IN ('queued', 'processing')"
|
||||
)
|
||||
)
|
||||
if active:
|
||||
raise RuntimeError("Перед downgrade необходимо завершить активные задания")
|
||||
op.execute(
|
||||
"""
|
||||
UPDATE attachments a SET variants = COALESCE(
|
||||
(SELECT jsonb_agg(v) FROM jsonb_array_elements(a.variants) v
|
||||
WHERE v->>'name' = 'thumbnail'), '[]'::jsonb)
|
||||
"""
|
||||
)
|
||||
op.drop_column("attachments", "optimized_key")
|
||||
op.drop_column("attachments", "crop_key")
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Бюджет повторов и аудит операторского replay."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "f9183d27a640"
|
||||
down_revision = "e7048b6da913"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"attachment_jobs",
|
||||
sa.Column(
|
||||
"cycle_start_attempt", sa.Integer(), nullable=False, server_default="0"
|
||||
),
|
||||
)
|
||||
op.add_column(
|
||||
"attachment_jobs", sa.Column("attempt_limit", sa.Integer(), nullable=True)
|
||||
)
|
||||
op.add_column(
|
||||
"attachment_jobs",
|
||||
sa.Column("last_error_permanent", sa.Boolean(), nullable=True),
|
||||
)
|
||||
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("actor", sa.String(64), nullable=False),
|
||||
sa.Column("reason", sa.String(32), nullable=False),
|
||||
sa.Column("previous_attempt", sa.Integer(), nullable=False),
|
||||
sa.Column("attempt_limit", sa.Integer(), nullable=False),
|
||||
sa.Column("previous_error_code", sa.String(64), nullable=True),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.func.clock_timestamp(),
|
||||
),
|
||||
)
|
||||
op.create_index("ix_job_replays_job_id", "job_replays", ["job_id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("job_replays")
|
||||
op.drop_column("attachment_jobs", "last_error_permanent")
|
||||
op.drop_column("attachment_jobs", "attempt_limit")
|
||||
op.drop_column("attachment_jobs", "cycle_start_attempt")
|
||||
Reference in New Issue
Block a user