Разработана новая архитектура базы данных

Этот коммит содержится в:
Глеб Иваницкий 2024-08-30 17:09:22 +03:00 коммит произвёл Gleb O. Ivaniczkij
родитель a01154047a
Коммит e53b57464f
3 изменённых файлов: 41 добавлений и 14 удалений

Просмотреть файл

@ -20,6 +20,7 @@ create table scripts (
owner_id bigint not null, owner_id bigint not null,
name character varying (64) not null, name character varying (64) not null,
start_time character varying (256) not null, start_time character varying (256) not null,
is_enabled boolean not null,
primary key (id), primary key (id),
foreign key (owner_id) references users on delete cascade on update cascade, foreign key (owner_id) references users on delete cascade on update cascade,
unique (name) unique (name)
@ -36,13 +37,13 @@ create table message_templates (
); );
-- TODO(messages ссылается на шаблонную таблицу message_templates) -- TODO(messages ссылается на шаблонную таблицу message_templates)
create table messages ( -- create table messages (
id bigserial not null, -- id bigserial not null,
message_template_id bigint not null, -- message_template_id bigint not null,
telegram_message_id bigint not null, -- telegram_message_id bigint not null,
primary key (id), -- primary key (id),
foreign key (message_template_id) references message_templates on delete cascade on update cascade -- foreign key (message_template_id) references message_templates on delete cascade on update cascade
); -- );
create table text_templates ( create table text_templates (
text character varying (4096) not null text character varying (4096) not null

Просмотреть файл

@ -20,7 +20,7 @@ class NotFoundError(Exception):
class Users: class Users:
@staticmethod @staticmethod
async def insert_or_update_user( async def insert_or_update_user(
telegram_id: int, telegram_user_id: int,
first_name: str, first_name: str,
last_name: str = None, last_name: str = None,
username: str = None, username: str = None,
@ -34,7 +34,7 @@ class Users:
last_name, last_name,
username username
) values ( ) values (
%(telegram_id)s, %(telegram_user_id)s,
%(first_name)s, %(first_name)s,
%(last_name)s, %(last_name)s,
%(username)s %(username)s
@ -43,24 +43,30 @@ class Users:
last_name = excluded.last_name, last_name = excluded.last_name,
username = excluded.username username = excluded.username
returning returning
users.id; users.id,
users.telegram_user_id,
users.first_name,
users.last_name,
users.username,
users.role;
''' '''
await cursor.execute( await cursor.execute(
sql, sql,
{ {
'telegram_id': telegram_id, 'telegram_user_id': telegram_user_id,
'first_name': first_name, 'first_name': first_name,
'last_name': last_name, 'last_name': last_name,
'username': username, 'username': username,
}, },
) )
user_id, = await cursor.fetchone() user_id, telegram_user_id, first_name, last_name, username, role, = await cursor.fetchone()
return models.User( return models.User(
id=user_id, id=user_id,
telegram_id=telegram_id, telegram_id=telegram_user_id,
first_name=first_name, first_name=first_name,
last_name=last_name, last_name=last_name,
username=username, username=username,
role=role,
) )

Просмотреть файл

@ -1,17 +1,37 @@
from datetime import datetime from datetime import datetime
from enum import Enum
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
class UserRole(Enum):
user = 'user'
moderator = 'moderator'
admin = 'admin'
class User(BaseModel): class User(BaseModel):
id: int id: int
telegram_id: int = Field( telegram_user_id: int = Field(
exclude=True, exclude=True,
) )
first_name: str first_name: str
last_name: Optional[str] last_name: Optional[str]
username: Optional[str] username: Optional[str]
role: UserRole
class Scripts(BaseModel):
id: int
owner: User
name: str
start_time: str
messages = []
is_enabled: bool
########################################################################################################################
class PollSchema(BaseModel): class PollSchema(BaseModel):