feat: first commit, auth

This commit is contained in:
2026-07-24 11:33:56 +07:00
commit 0d7944fdab
43 changed files with 1303 additions and 0 deletions

1
alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.

94
alembic/env.py Normal file
View File

@@ -0,0 +1,94 @@
import asyncio
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
from config import cfg as project_config
from db.base import Base
from db.models import *
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = project_config.db_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:
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
url = project_config.db_url
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
url=url,
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:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

28
alembic/script.py.mako Normal file
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,36 @@
"""revoked_at -> nullable, NULL by default
Revision ID: 086c8d206446
Revises: 40c196465541
Create Date: 2026-07-24 11:04:35.693439
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '086c8d206446'
down_revision: Union[str, Sequence[str], None] = '40c196465541'
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.alter_column('sessions', 'revoked_at',
existing_type=postgresql.TIMESTAMP(),
nullable=True)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('sessions', 'revoked_at',
existing_type=postgresql.TIMESTAMP(),
nullable=False)
# ### end Alembic commands ###

View File

@@ -0,0 +1,41 @@
"""added sessions table
Revision ID: 271e258d06be
Revises: a13612c28fcd
Create Date: 2026-07-23 22:12:08.027226
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '271e258d06be'
down_revision: Union[str, Sequence[str], None] = 'a13612c28fcd'
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('sessions',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.BIGINT(), nullable=False),
sa.Column('refresh_token_hash', sa.TEXT(), nullable=False),
sa.Column('source', sa.TEXT(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('sessions')
# ### end Alembic commands ###

View File

@@ -0,0 +1,34 @@
"""revoking sessions
Revision ID: 40c196465541
Revises: 85e0c74c04b6
Create Date: 2026-07-24 10:59:52.997084
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '40c196465541'
down_revision: Union[str, Sequence[str], None] = '85e0c74c04b6'
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.add_column('sessions', sa.Column('is_revoked', sa.BOOLEAN(), nullable=False))
op.add_column('sessions', sa.Column('revoked_at', postgresql.TIMESTAMP(), nullable=False))
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('sessions', 'revoked_at')
op.drop_column('sessions', 'is_revoked')
# ### end Alembic commands ###

View File

@@ -0,0 +1,32 @@
"""fanthom changes
Revision ID: 85e0c74c04b6
Revises: 271e258d06be
Create Date: 2026-07-23 22:15:07.808811
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '85e0c74c04b6'
down_revision: Union[str, Sequence[str], None] = '271e258d06be'
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_unique_constraint(None, 'sessions', ['id'])
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'sessions', type_='unique')
# ### end Alembic commands ###

View File

@@ -0,0 +1,44 @@
"""users.username and users.password -> nullable
Revision ID: a13612c28fcd
Revises: b1a3f3cd587b
Create Date: 2026-07-22 21:51:19.926393
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'a13612c28fcd'
down_revision: Union[str, Sequence[str], None] = 'b1a3f3cd587b'
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.alter_column('users', 'username',
existing_type=sa.TEXT(),
nullable=True)
op.alter_column('users', 'hashed_password',
existing_type=sa.VARCHAR(length=255),
nullable=True)
op.create_unique_constraint(None, 'users', ['id'])
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'users', type_='unique')
op.alter_column('users', 'hashed_password',
existing_type=sa.VARCHAR(length=255),
nullable=False)
op.alter_column('users', 'username',
existing_type=sa.TEXT(),
nullable=False)
# ### end Alembic commands ###

View File

@@ -0,0 +1,41 @@
"""created users table
Revision ID: b1a3f3cd587b
Revises:
Create Date: 2026-07-22 21:12:08.095864
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b1a3f3cd587b'
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('users',
sa.Column('id', sa.BIGINT(), autoincrement=True, nullable=False),
sa.Column('username', sa.TEXT(), nullable=False),
sa.Column('hashed_password', sa.VARCHAR(length=255), nullable=False),
sa.Column('telegram_id', sa.BIGINT(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('telegram_id'),
sa.UniqueConstraint('username')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('users')
# ### end Alembic commands ###