Create authorization system

This commit is contained in:
2026-01-05 23:31:36 +03:00
parent d438f7bf5b
commit 2e14a7f364
39 changed files with 2500 additions and 9 deletions
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.
+93
View File
@@ -0,0 +1,93 @@
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 app.users.models import UserModel
from app.database import Base
from app.config import settings
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
# 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 = 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:
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
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:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+28
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"}
@@ -0,0 +1,48 @@
"""ADD: user table
Revision ID: 4d00c9b0516e
Revises: 52b7263bba19
Create Date: 2026-01-04 14:04:33.143440
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '4d00c9b0516e'
down_revision: Union[str, Sequence[str], None] = '52b7263bba19'
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('user', 'birth_day',
existing_type=sa.DATE(),
nullable=True)
op.alter_column('user', 'description',
existing_type=sa.VARCHAR(),
nullable=True)
op.alter_column('user', 'avatar_url',
existing_type=sa.VARCHAR(),
nullable=True)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('user', 'avatar_url',
existing_type=sa.VARCHAR(),
nullable=False)
op.alter_column('user', 'description',
existing_type=sa.VARCHAR(),
nullable=False)
op.alter_column('user', 'birth_day',
existing_type=sa.DATE(),
nullable=False)
# ### end Alembic commands ###
@@ -0,0 +1,47 @@
"""ADD: user table
Revision ID: 52b7263bba19
Revises: 9b013a15d8fb
Create Date: 2026-01-04 13:48:55.058538
"""
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 = '52b7263bba19'
down_revision: Union[str, Sequence[str], None] = '9b013a15d8fb'
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.drop_index(op.f('refresh_session_id_idx'), table_name='refresh_session')
op.drop_index(op.f('refresh_session_refresh_token_idx'), table_name='refresh_session')
op.drop_table('refresh_session')
op.add_column('user', sa.Column('hashed_password', sa.String(), nullable=False))
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'hashed_password')
op.create_table('refresh_session',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('refresh_token', sa.UUID(), autoincrement=False, nullable=False),
sa.Column('expires_in', sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column('user_ud', sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column('created_at', postgresql.TIMESTAMP(), server_default=sa.text('now()'), autoincrement=False, nullable=False),
sa.Column('updated_at', postgresql.TIMESTAMP(), server_default=sa.text('now()'), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['user_ud'], ['user.id'], name=op.f('refresh_session_user_ud_fkey'), ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id', name=op.f('refresh_session_pkey'))
)
op.create_index(op.f('refresh_session_refresh_token_idx'), 'refresh_session', ['refresh_token'], unique=False)
op.create_index(op.f('refresh_session_id_idx'), 'refresh_session', ['id'], unique=False)
# ### end Alembic commands ###
@@ -0,0 +1,67 @@
"""Initial revision
Revision ID: 9b013a15d8fb
Revises:
Create Date: 2025-12-21 17:27:03.170318
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '9b013a15d8fb'
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('user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('display_name', sa.String(), nullable=False),
sa.Column('username', sa.String(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('birth_day', sa.DATE(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.Column('avatar_url', sa.String(), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.Column('is_verified', sa.Boolean(), nullable=False),
sa.Column('is_superuser', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('user_pkey'))
)
op.create_index(op.f('user_email_idx'), 'user', ['email'], unique=True)
op.create_index(op.f('user_id_idx'), 'user', ['id'], unique=False)
op.create_index(op.f('user_username_idx'), 'user', ['username'], unique=True)
op.create_table('refresh_session',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('refresh_token', sa.UUID(), nullable=False),
sa.Column('expires_in', sa.Integer(), nullable=False),
sa.Column('user_ud', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['user_ud'], ['user.id'], name=op.f('refresh_session_user_ud_fkey'), ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id', name=op.f('refresh_session_pkey'))
)
op.create_index(op.f('refresh_session_id_idx'), 'refresh_session', ['id'], unique=False)
op.create_index(op.f('refresh_session_refresh_token_idx'), 'refresh_session', ['refresh_token'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('refresh_session_refresh_token_idx'), table_name='refresh_session')
op.drop_index(op.f('refresh_session_id_idx'), table_name='refresh_session')
op.drop_table('refresh_session')
op.drop_index(op.f('user_username_idx'), table_name='user')
op.drop_index(op.f('user_id_idx'), table_name='user')
op.drop_index(op.f('user_email_idx'), table_name='user')
op.drop_table('user')
# ### end Alembic commands ###