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

Этот коммит содержится в:
Глеб Иваницкий 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,
name character varying (64) not null,
start_time character varying (256) not null,
is_enabled boolean not null,
primary key (id),
foreign key (owner_id) references users on delete cascade on update cascade,
unique (name)
@ -36,13 +37,13 @@ create table message_templates (
);
-- TODO(messages ссылается на шаблонную таблицу message_templates)
create table messages (
id bigserial not null,
message_template_id bigint not null,
telegram_message_id bigint not null,
primary key (id),
foreign key (message_template_id) references message_templates on delete cascade on update cascade
);
-- create table messages (
-- id bigserial not null,
-- message_template_id bigint not null,
-- telegram_message_id bigint not null,
-- primary key (id),
-- foreign key (message_template_id) references message_templates on delete cascade on update cascade
-- );
create table text_templates (
text character varying (4096) not null

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

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

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

@ -1,17 +1,37 @@
from datetime import datetime
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field
class UserRole(Enum):
user = 'user'
moderator = 'moderator'
admin = 'admin'
class User(BaseModel):
id: int
telegram_id: int = Field(
telegram_user_id: int = Field(
exclude=True,
)
first_name: str
last_name: 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):