This commit is contained in:
jze9
2026-05-15 20:12:44 +05:00
parent 2335497226
commit dd7d8a7ccf
21 changed files with 672 additions and 121 deletions

1
api/migrations/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

63
api/migrations/env.py Normal file
View File

@@ -0,0 +1,63 @@
import asyncio
import os
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Подключаем модели для autogenerate
from bd.database import Base
import bd.models # noqa: F401
target_metadata = Base.metadata
# URL из ENV (приоритет) или из alembic.ini
DATABASE_URL = os.getenv("DATABASE_URL", config.get_main_option("sqlalchemy.url", ""))
config.set_main_option("sqlalchemy.url", DATABASE_URL)
def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_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()

View File

@@ -0,0 +1,28 @@
"""${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 identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[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:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,109 @@
"""initial
Revision ID: 25d6ed4a524a
Revises:
Create Date: 2026-05-15 14:46:07.892008
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '25d6ed4a524a'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('admins',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('username', sa.String(length=100), nullable=False),
sa.Column('password_hash', sa.String(length=300), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('username')
)
op.create_table('categories',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('slug', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_index(op.f('ix_categories_slug'), 'categories', ['slug'], unique=True)
op.create_table('tags',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('slug', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_index(op.f('ix_tags_slug'), 'tags', ['slug'], unique=True)
op.create_table('vk_sources',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('group_id', sa.String(length=50), nullable=False),
sa.Column('group_name', sa.String(length=200), nullable=False),
sa.Column('enabled', sa.Boolean(), nullable=False),
sa.Column('last_run', sa.DateTime(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('group_id')
)
op.create_table('articles',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('title', sa.String(length=500), nullable=False),
sa.Column('slug', sa.String(length=500), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('excerpt', sa.String(length=1000), nullable=False),
sa.Column('cover_url', sa.String(length=1000), nullable=True),
sa.Column('font_family', sa.String(length=100), nullable=False),
sa.Column('source_url', sa.String(length=1000), nullable=True),
sa.Column('status', sa.Enum('draft', 'published', name='articlestatus'), nullable=False),
sa.Column('view_count', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('published_at', sa.DateTime(), nullable=True),
sa.Column('category_id', sa.UUID(), nullable=True),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_articles_slug'), 'articles', ['slug'], unique=True)
op.create_table('article_tag',
sa.Column('article_id', sa.UUID(), nullable=True),
sa.Column('tag_id', sa.UUID(), nullable=True),
sa.ForeignKeyConstraint(['article_id'], ['articles.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['tag_id'], ['tags.id'], ondelete='CASCADE')
)
op.create_table('media',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('filename', sa.String(length=500), nullable=False),
sa.Column('url', sa.String(length=1000), nullable=False),
sa.Column('media_type', sa.String(length=20), nullable=False),
sa.Column('size_bytes', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('article_id', sa.UUID(), nullable=True),
sa.ForeignKeyConstraint(['article_id'], ['articles.id'], ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('media')
op.drop_table('article_tag')
op.drop_index(op.f('ix_articles_slug'), table_name='articles')
op.drop_table('articles')
op.drop_table('vk_sources')
op.drop_index(op.f('ix_tags_slug'), table_name='tags')
op.drop_table('tags')
op.drop_index(op.f('ix_categories_slug'), table_name='categories')
op.drop_table('categories')
op.drop_table('admins')
# ### end Alembic commands ###