initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# utils/db/__init__.py
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
|
||||
# Создание асинхронного движка SQLAlchemy
|
||||
DATABASE_URL = 'sqlite+aiosqlite:///db/telezab.db'
|
||||
engine = create_async_engine(DATABASE_URL, echo=False)
|
||||
AsyncSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession)
|
||||
|
||||
# Базовый класс для всех моделей
|
||||
Base = declarative_base()
|
||||
|
||||
# Импорт моделей
|
||||
from .whitelist import Whitelist
|
||||
from .user_events import UserEvent
|
||||
from .subscriptions import Subscription
|
||||
from .regions import Region
|
||||
from .events import Event
|
||||
from .admins import Admin
|
||||
|
||||
# Создание всех таблиц
|
||||
async def init_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
print("INFO:SQLAlchemy:Database inited")
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
# utils/db/admins.py
|
||||
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from utils.db import Base
|
||||
|
||||
class Admin(Base):
|
||||
__tablename__ = 'admins'
|
||||
|
||||
chat_id = Column(Integer, primary_key=True, unique=True, nullable=False)
|
||||
username = Column(String, nullable=False)
|
||||
@@ -0,0 +1,12 @@
|
||||
# utils/db/events.py
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Text, Boolean
|
||||
from utils.db import Base
|
||||
|
||||
class Event(Base):
|
||||
__tablename__ = 'events'
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
hash = Column(String, unique=True, nullable=False)
|
||||
data = Column(Text, nullable=False)
|
||||
delivered = Column(Boolean, default=False)
|
||||
@@ -0,0 +1,11 @@
|
||||
# utils/db/regions.py
|
||||
|
||||
from sqlalchemy import Column, String, Boolean
|
||||
from utils.db import Base
|
||||
|
||||
class Region(Base):
|
||||
__tablename__ = 'regions'
|
||||
|
||||
region_id = Column(String, primary_key=True, unique=True, nullable=False)
|
||||
region_name = Column(String, nullable=False)
|
||||
active = Column(Boolean, default=True)
|
||||
@@ -0,0 +1,22 @@
|
||||
# utils/db/subscriptions.py
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Boolean, UniqueConstraint
|
||||
from utils.db import Base
|
||||
|
||||
class Subscription(Base):
|
||||
__tablename__ = 'subscriptions'
|
||||
|
||||
chat_id = Column(Integer, nullable=False)
|
||||
region_id = Column(String, nullable=False)
|
||||
username = Column(String, nullable=False)
|
||||
active = Column(Boolean, default=True)
|
||||
skip = Column(Boolean, default=False)
|
||||
|
||||
# Определение составного первичного ключа
|
||||
__table_args__ = (
|
||||
UniqueConstraint('chat_id', 'region_id', name='unique_chat_region'),
|
||||
{'sqlite_autoincrement': True}
|
||||
)
|
||||
|
||||
# Определяем составной первичный ключ
|
||||
primary_key = Column(Integer, primary_key=True, autoincrement=True) # Добавление этого столбца необходимо для уникального первичного ключа
|
||||
@@ -0,0 +1,13 @@
|
||||
# utils/db/user_events.py
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Text
|
||||
from utils.db import Base
|
||||
|
||||
class UserEvent(Base):
|
||||
__tablename__ = 'user_events'
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
chat_id = Column(Integer, nullable=False)
|
||||
username = Column(String, nullable=False)
|
||||
action = Column(String, nullable=False)
|
||||
timestamp = Column(Text, nullable=False)
|
||||
@@ -0,0 +1,9 @@
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from utils.db import Base
|
||||
|
||||
class Whitelist(Base):
|
||||
__tablename__ = 'whitelist'
|
||||
|
||||
chat_id = Column(Integer, primary_key=True)
|
||||
username = Column(String)
|
||||
user_email = Column(String)
|
||||
Reference in New Issue
Block a user