feat: create backend skeleton

This commit is contained in:
h
2026-05-29 16:11:41 +02:00
parent e08d26dd10
commit 62aac0bf32
37 changed files with 1591 additions and 3 deletions
+50
View File
@@ -0,0 +1,50 @@
import asyncio
from logging.config import fileConfig
from alembic import context
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel import SQLModel
from utils.db import models # noqa: F401
from utils.env import env
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = SQLModel.metadata
def _async_url() -> str:
return env.db.connection_url.replace("postgresql://", "postgresql+asyncpg://", 1)
def run_migrations_offline() -> None:
context.configure(
url=_async_url(),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def _do_run_migrations(connection) -> None: # noqa: ANN001
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
connectable = create_async_engine(_async_url())
async with connectable.connect() as connection:
await connection.run_sync(_do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())
@@ -0,0 +1 @@
CREATE EXTENSION IF NOT EXISTS timescaledb;
+26
View File
@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
import sqlmodel
${imports if imports else ""}
revision: str = ${repr(up_revision)}
down_revision: str | None = ${repr(down_revision)}
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
depends_on: 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,75 @@
"""initial accounts and messages hypertable
Revision ID: 77df960a31de
Revises:
Create Date: 2026-05-29 10:58:16.118857
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "77df960a31de"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"accounts",
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("tg_user_id", sa.BigInteger(), nullable=True),
sa.Column("label", sa.String(), nullable=True),
sa.Column("phone", sa.String(), nullable=True),
sa.Column("session_name", sa.String(), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("raw", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.PrimaryKeyConstraint("account_id"),
sa.UniqueConstraint("tg_user_id"),
)
op.create_table(
"messages",
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("chat_id", sa.BigInteger(), nullable=False),
sa.Column("message_id", sa.BigInteger(), nullable=False),
sa.Column("date", sa.DateTime(timezone=True), nullable=False),
sa.Column("raw", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint("account_id", "chat_id", "message_id", "date"),
)
# ### end Alembic commands ###
op.execute(
"SELECT create_hypertable('messages', by_range('date', INTERVAL '1 month'))"
)
op.execute(
"ALTER TABLE messages SET ("
"timescaledb.enable_columnstore = true, "
"timescaledb.segmentby = 'chat_id', "
"timescaledb.orderby = 'date DESC')"
)
op.execute("CALL add_columnstore_policy('messages', after => INTERVAL '30 days')")
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("messages")
op.drop_table("accounts")
# ### end Alembic commands ###