Skip to content

Commit 8f0d33d

Browse files
committed
changes
1 parent 0f3c190 commit 8f0d33d

4 files changed

Lines changed: 39 additions & 73 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL := /bin/bash
22

33
configure: venv
4-
source ./venv/bin/activate && pip install -r requirements.txt -r requirements.dev.txt
4+
source ./venv/bin/activate && pip install --force-reinstall -r requirements.txt -r requirements.dev.txt
55

66
venv:
77
python3.11 -m venv venv

migrations/env.py

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,48 @@
1+
import asyncio
2+
import asyncio
13
from logging.config import fileConfig
2-
4+
35
from alembic import context
4-
from sqlalchemy import engine_from_config, pool
5-
6+
from sqlalchemy import pool
7+
from sqlalchemy.ext.asyncio import async_engine_from_config
8+
69
from src.models.base import BaseDbModel
710
from src.settings import get_settings
8-
9-
10-
# this is the Alembic Config object, which provides
11-
# access to the values within the .ini file in use.
11+
1212
config = context.config
1313
settings = get_settings()
14-
15-
# Interpret the config file for Python logging.
16-
# This line sets up loggers basically.
14+
config.set_main_option('sqlalchemy.url', str(settings.DB_DSN))
15+
1716
if config.config_file_name is not None:
1817
fileConfig(config.config_file_name)
19-
20-
# add your model's MetaData object here
21-
# for 'autogenerate' support
22-
# from myapp import mymodel
23-
# target_metadata = mymodel.Base.metadata
18+
2419
target_metadata = BaseDbModel.metadata
25-
26-
# other values from the config, defined by the needs of env.py,
27-
# can be acquired:
28-
# my_important_option = config.get_main_option("my_important_option")
29-
# ... etc.
30-
31-
32-
def run_migrations_offline():
33-
"""Run migrations in 'offline' mode.
34-
35-
This configures the context with just a URL
36-
and not an Engine, though an Engine is acceptable
37-
here as well. By skipping the Engine creation
38-
we don't even need a DBAPI to be available.
39-
40-
Calls to context.execute() here emit the given string to the
41-
script output.
42-
43-
"""
44-
url = config.get_main_option("sqlalchemy.url")
45-
context.configure(
46-
url=url,
47-
target_metadata=target_metadata,
48-
literal_binds=True,
49-
dialect_opts={"paramstyle": "named"},
50-
)
51-
20+
21+
def run_migrations_offline() -> None:
22+
url = config.get_main_option('sqlalchemy.url')
23+
context.configure(url=url, target_metadata=target_metadata,
24+
literal_binds=True, dialect_opts={'paramstyle': 'named'})
5225
with context.begin_transaction():
5326
context.run_migrations()
54-
55-
56-
def run_migrations_online():
57-
"""Run migrations in 'online' mode.
58-
59-
In this scenario we need to create an Engine
60-
and associate a connection with the context.
61-
62-
"""
63-
configuration = config.get_section(config.config_ini_section)
27+
28+
def do_run_migrations(connection):
29+
context.configure(connection=connection, target_metadata=target_metadata)
30+
with context.begin_transaction():
31+
context.run_migrations()
32+
33+
async def run_async_migrations() -> None:
34+
configuration = config.get_section(config.config_ini_section, {})
6435
configuration['sqlalchemy.url'] = str(settings.DB_DSN)
65-
connectable = engine_from_config(
66-
configuration,
67-
prefix="sqlalchemy.",
68-
poolclass=pool.NullPool,
69-
)
70-
71-
with connectable.connect() as connection:
72-
context.configure(connection=connection, target_metadata=target_metadata)
73-
74-
with context.begin_transaction():
75-
context.run_migrations()
76-
77-
36+
connectable = async_engine_from_config(
37+
configuration, prefix='sqlalchemy.', poolclass=pool.NullPool)
38+
async with connectable.connect() as connection:
39+
await connection.run_sync(do_run_migrations)
40+
await connectable.dispose()
41+
42+
def run_migrations_online() -> None:
43+
asyncio.run(run_async_migrations())
44+
7845
if context.is_offline_mode():
7946
run_migrations_offline()
8047
else:
81-
run_migrations_online()
48+
run_migrations_online()

src/handlers/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from src.handlers import registration, admin, menu, text
44

55
def setup_routers() -> Router:
6-
root=Router
7-
root.include_router(admin.router)
6+
root=Router()
7+
#root.include_router(admin.router)
88
root.include_router(registration.router)
9-
root.include_router(menu.router)
9+
#root.include_router(menu.router)
1010
root.include_router(text.router)
1111
return root

src/handlers/registration.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ async def cmd_start(message: Message, state: FSMContext, session: AsyncSession)-
1717
user_id=message.from_user.id
1818
username=message.from_user.username
1919

20-
existing = await session.execute(
21-
select(Users).where(Users.tg_id==user_id)
22-
)
20+
existing = Users.query(session=session).filter(Users.tg_id == user_id).one_or_none()
21+
2322
user=existing.scalar_one_or_none()
2423
if user:
2524
await message.answer(

0 commit comments

Comments
 (0)