-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
55 lines (43 loc) · 1.64 KB
/
database.py
File metadata and controls
55 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Optional
import aiosqlite
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
engine = create_async_engine(
"sqlite+aiosqlite:///tasks.db",
echo=True
)
new_session = async_sessionmaker(engine, expire_on_commit=False)
class Model(DeclarativeBase):
pass
class TaskOrm(Model):
__tablename__ = "tasks"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column()
description: Mapped[Optional[str]] = mapped_column(default=None)
is_completed: Mapped[bool] = mapped_column(default=False)
def to_dict(self) -> dict:
return {
"id": self.id,
"name": self.name,
"description": self.description,
"is_completed": self.is_completed
}
async def create_tables():
try:
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.create_all)
logger.info("Таблица создана или существует")
except Exception as e:
logger.error(f"Ошибка создания таблиц: {e}")
raise
async def delete_tables():
try:
async with engine.begin() as conn:
await conn.run_sync(Model.metadata.drop_all)
logger.info("Таблицы удалены")
except Exception as e:
logger.error(f"Ошибка удаления таблиц: {e}")
raise