Перенести реализацию лабораторной 5
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from app.config import load_settings
|
||||
from app.db.postgres import Base
|
||||
from app import models # noqa: F401 - imports ORM models into Base.metadata
|
||||
|
||||
config = context.config
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _url() -> str:
|
||||
return load_settings().database_url
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(url=_url(), target_metadata=target_metadata, literal_binds=True, dialect_opts={'paramstyle': 'named'})
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def _run_migrations(connection) -> None:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def _run_migrations_async() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration['sqlalchemy.url'] = _url()
|
||||
connectable = async_engine_from_config(configuration, prefix='sqlalchemy.', poolclass=pool.NullPool)
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(_run_migrations)
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
asyncio.run(_run_migrations_async())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,22 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Initial PostgreSQL schema for lab 2.
|
||||
|
||||
Revision ID: 20261019_0001
|
||||
Revises:
|
||||
Create Date: 2026-10-19
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '20261019_0001'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
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.Text(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('username'),
|
||||
)
|
||||
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), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
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'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('chat_id', 'user_id'),
|
||||
)
|
||||
op.create_index('chat_members_user_chat_idx', 'chat_members', ['user_id', 'chat_id'])
|
||||
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.String(length=4000), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['sender_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
op.create_index('messages_history_idx', 'messages', ['chat_id', 'created_at', 'id'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('messages_history_idx', table_name='messages')
|
||||
op.drop_table('messages')
|
||||
op.drop_index('chat_members_user_chat_idx', table_name='chat_members')
|
||||
op.drop_table('chat_members')
|
||||
op.drop_table('chats')
|
||||
op.drop_table('users')
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Add shared message idempotency for lab 3.
|
||||
|
||||
Revision ID: 20261023_0002
|
||||
Revises: 20261019_0001
|
||||
Create Date: 2026-10-23
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '20261023_0002'
|
||||
down_revision = '20261019_0001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
'messages',
|
||||
sa.Column('idempotency_key', sa.String(length=128), nullable=True),
|
||||
)
|
||||
op.create_unique_constraint(
|
||||
'messages_sender_chat_idempotency_key',
|
||||
'messages',
|
||||
['sender_id', 'chat_id', 'idempotency_key'],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_constraint(
|
||||
'messages_sender_chat_idempotency_key',
|
||||
'messages',
|
||||
type_='unique',
|
||||
)
|
||||
op.drop_column('messages', 'idempotency_key')
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Add S3 attachment metadata and message links for lab 4.
|
||||
|
||||
Revision ID: 20261030_0003
|
||||
Revises: 20261023_0002
|
||||
Create Date: 2026-10-30
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '20261030_0003'
|
||||
down_revision = '20261023_0002'
|
||||
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('filename', sa.String(length=255), nullable=False),
|
||||
sa.Column('object_key', sa.String(length=255), nullable=False),
|
||||
sa.Column('content_type', sa.String(length=32), nullable=False),
|
||||
sa.Column('size_bytes', sa.BigInteger(), nullable=False),
|
||||
sa.Column('sha256', sa.String(length=64), nullable=False),
|
||||
sa.Column('width', sa.Integer(), nullable=False),
|
||||
sa.Column('height', sa.Integer(), nullable=False),
|
||||
sa.Column('status', sa.String(length=16), nullable=False),
|
||||
sa.Column('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(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['owner_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('object_key', name='attachments_object_key_key'),
|
||||
)
|
||||
op.create_index(
|
||||
'attachments_chat_created_idx',
|
||||
'attachments',
|
||||
['chat_id', 'created_at', '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(['attachment_id'], ['attachments.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['message_id'], ['messages.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('message_id', 'attachment_id'),
|
||||
sa.UniqueConstraint(
|
||||
'message_id',
|
||||
'position',
|
||||
name='message_attachments_message_position_key',
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
'message_attachments_attachment_idx',
|
||||
'message_attachments',
|
||||
['attachment_id'],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('message_attachments_attachment_idx', table_name='message_attachments')
|
||||
op.drop_table('message_attachments')
|
||||
op.drop_index('attachments_chat_created_idx', table_name='attachments')
|
||||
op.drop_table('attachments')
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Add asynchronous attachment jobs, variants and transactional outbox.
|
||||
|
||||
Revision ID: 20261116_0004
|
||||
Revises: 20261030_0003
|
||||
Create Date: 2026-11-16
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '20261116_0004'
|
||||
down_revision = '20261030_0003'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.alter_column('attachments', 'object_key', new_column_name='original_key')
|
||||
op.alter_column('attachments', 'original_key', nullable=True)
|
||||
op.add_column('attachments', sa.Column('staging_key', sa.String(length=255), nullable=True))
|
||||
op.add_column(
|
||||
'attachments',
|
||||
sa.Column('pipeline_version', sa.Integer(), nullable=False, server_default='1'),
|
||||
)
|
||||
op.create_unique_constraint('attachments_staging_key_key', 'attachments', ['staging_key'])
|
||||
|
||||
op.create_table(
|
||||
'attachment_variants',
|
||||
sa.Column('attachment_id', sa.Uuid(), nullable=False),
|
||||
sa.Column('pipeline_version', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=16), nullable=False),
|
||||
sa.Column('object_key', sa.String(length=255), nullable=False),
|
||||
sa.Column('content_type', sa.String(length=32), 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.String(length=64), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.CheckConstraint("name IN ('thumbnail', 'crop', 'optimized')", name='variant_name_check'),
|
||||
sa.ForeignKeyConstraint(['attachment_id'], ['attachments.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('attachment_id', 'pipeline_version', 'name'),
|
||||
sa.UniqueConstraint('object_key', name='attachment_variants_object_key_key'),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'attachment_jobs',
|
||||
sa.Column('id', sa.Uuid(), nullable=False),
|
||||
sa.Column('attachment_id', sa.Uuid(), nullable=False),
|
||||
sa.Column('pipeline_version', sa.Integer(), nullable=False),
|
||||
sa.Column('state', sa.String(length=16), nullable=False),
|
||||
sa.Column('attempt', sa.Integer(), nullable=False),
|
||||
sa.Column('max_attempts', sa.Integer(), nullable=False),
|
||||
sa.Column('next_attempt_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('lease_owner', sa.String(length=128), nullable=True),
|
||||
sa.Column('lease_until', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('last_error_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('replay_count', sa.Integer(), nullable=False, server_default='0'),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"state IN ('queued', 'processing', 'ready', 'failed')",
|
||||
name='attachment_job_state_check',
|
||||
),
|
||||
sa.ForeignKeyConstraint(['attachment_id'], ['attachments.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint(
|
||||
'attachment_id', 'pipeline_version', name='attachment_job_attachment_version_key'
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
'attachment_jobs_lease_idx',
|
||||
'attachment_jobs',
|
||||
['state', 'lease_until'],
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'outbox_events',
|
||||
sa.Column('id', sa.Uuid(), nullable=False),
|
||||
sa.Column('event_type', sa.String(length=64), nullable=False),
|
||||
sa.Column('job_id', sa.Uuid(), nullable=False),
|
||||
sa.Column('payload', sa.JSON(), nullable=False),
|
||||
sa.Column('available_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('published_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('claim_owner', sa.String(length=128), nullable=True),
|
||||
sa.Column('claim_until', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('publish_attempts', sa.Integer(), nullable=False, server_default='0'),
|
||||
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(['job_id'], ['attachment_jobs.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
op.create_index(
|
||||
'outbox_events_pending_idx',
|
||||
'outbox_events',
|
||||
['published_at', 'available_at'],
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'job_events',
|
||||
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column('job_id', sa.Uuid(), nullable=False),
|
||||
sa.Column('kind', sa.String(length=32), nullable=False),
|
||||
sa.Column('attempt', sa.Integer(), nullable=False),
|
||||
sa.Column('error_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['job_id'], ['attachment_jobs.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
op.create_index('job_events_job_created_idx', 'job_events', ['job_id', 'created_at'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('job_events_job_created_idx', table_name='job_events')
|
||||
op.drop_table('job_events')
|
||||
op.drop_index('outbox_events_pending_idx', table_name='outbox_events')
|
||||
op.drop_table('outbox_events')
|
||||
op.drop_index('attachment_jobs_lease_idx', table_name='attachment_jobs')
|
||||
op.drop_table('attachment_jobs')
|
||||
op.drop_table('attachment_variants')
|
||||
op.drop_constraint('attachments_staging_key_key', 'attachments', type_='unique')
|
||||
op.drop_column('attachments', 'pipeline_version')
|
||||
op.drop_column('attachments', 'staging_key')
|
||||
op.alter_column('attachments', 'original_key', nullable=False)
|
||||
op.alter_column('attachments', 'original_key', new_column_name='object_key')
|
||||
Reference in New Issue
Block a user