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

Этот коммит содержится в:
Глеб Иваницкий 2024-08-30 17:09:22 +03:00
родитель 885aaa850a
Коммит a01154047a

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

@ -5,112 +5,100 @@ create type user_role as enum (
);
create table users (
id bigserial not null,
telegram_user_id bigint not null,
first_name character varying (64) not null,
last_name character varying (64),
username character varying (32),
role user_role,
id bigserial not null,
telegram_user_id bigint not null,
first_name character varying (64) not null,
last_name character varying (64),
username character varying (32),
role user_role,
primary key (id),
unique (telegram_user_id)
);
create table messages (
id bigserial not null,
telegram_message_id bigint not null,
created_at timestamp default now() not null,
primary key (id),
unique (telegram_message_id)
);
create table text_messages (
text character varying (4096) not null
) inherits (messages);
create table poll_schemas (
id bigserial not null,
owner_id bigint not null,
name character varying (32) not null,
question character varying (255) not null,
create table scripts (
id bigserial not null,
owner_id bigint not null,
name character varying (64) not null,
start_time character varying (256) not null,
primary key (id),
foreign key (owner_id) references users on delete cascade on update cascade,
unique (name)
);
create table poll_options (
id bigserial not null,
poll_schema_id bigint not null,
name character varying (100) not null,
ordinal bigint,
create table message_templates (
id bigserial not null,
script_id bigint not null,
name character varying (64) not null,
end_time character varying (256) not null,
primary key (id),
foreign key (poll_schema_id) references poll_schemas on delete cascade on update cascade,
unique (poll_schema_id, name),
unique (poll_schema_id, ordinal)
foreign key (script_id) references scripts on delete cascade on update cascade,
unique (name)
);
create table poll_messages (
telegram_poll_id text not null,
poll_schema_id bigint not null,
is_complete boolean default false not null,
);
create table media_messages () inherits (messages);
create table media_message_files (
id bigserial not null,
media_message_id bigint not null,
name character varying (256) not null,
caption character varying (4096) not null,
-- 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 (media_message_id) references media_messages on delete cascade on update cascade
foreign key (message_template_id) references message_templates on delete cascade on update cascade
);
create table contact_messages (
phone_number character varying (16) not null,
first_name character varying (32) not null
) inherits (messages);
create table text_templates (
text character varying (4096) not null
) inherits (message_templates);
create table dice_messages (
value smallint not null
create table poll_templates (
question character varying (255) not null,
anonymous_voting boolean not null,
multiple_answers boolean not null
) inherits (message_templates);
create table poll_template_options (
id bigserial not null,
poll_template_id bigint not null,
text character varying (100) not null,
ordinal bigint not null,
primary key (id),
foreign key (poll_template_id) references poll_templates on delete cascade on update cascade,
unique (poll_template_id),
unique (poll_template_id, ordinal)
);
-- create table polls (
-- id bigserial not null,
-- telegram_message_id bigint not null,
-- telegram_poll_id text not null,
-- poll_schema_id bigint not null,
-- created_at timestamp default now() not null,
-- is_complete boolean default false not null,
-- primary key (id),
-- foreign key (poll_schema_id) references poll_schemas on delete cascade on update cascade,
-- unique (telegram_message_id),
-- unique (telegram_poll_id)
-- );
create table poll_answers (
id bigserial not null,
poll_id bigint not null,
user_id bigint not null,
poll_option_id bigint not null,
id bigserial not null,
poll_template_id bigint not null,
user_id bigint not null,
poll_template_option_id bigint not null,
primary key (id),
foreign key (poll_id) references polls on delete cascade on update cascade,
foreign key (poll_template_id) references poll_templates on delete cascade on update cascade,
foreign key (user_id) references users on delete cascade on update cascade,
foreign key (poll_option_id) references poll_options on delete cascade on update cascade,
unique (poll_id, user_id, poll_option_id)
foreign key (poll_template_option_id) references poll_template_options on delete cascade on update cascade,
unique (poll_template_id, user_id, poll_template_option_id)
);
insert into poll_schemas (name, question) values ('mood', 'Оцените свое состояние на текущую минуту');
insert into poll_options (poll_schema_id, name, ordinal) values (1, '😄', 0);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '🤪', 1);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '🫠', 2);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '☠️', 3);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '🤡', 4);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '😟', 5);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '😩', 6);
insert into poll_options (poll_schema_id, name, ordinal) values (1, '😡', 7);
create table media_templates () inherits (message_templates);
insert into poll_schemas (name, question) values ('lunch', 'Какие у вас планы на обед?');
insert into poll_options (poll_schema_id, name, ordinal) values (2, '🍽️ Пойду в общепит', 0);
insert into poll_options (poll_schema_id, name, ordinal) values (2, '📦 Хочу заказать в офис', 1);
insert into poll_options (poll_schema_id, name, ordinal) values (2, '🥪 Всё своё ношу с собой', 2);
insert into poll_options (poll_schema_id, name, ordinal) values (2, '😴 Хочу спать', 3);
create table media_template_files (
id bigserial not null,
media_template_id bigint not null,
name character varying (256) not null,
caption character varying (4096) not null,
primary key (id),
foreign key (media_template_id) references media_templates on delete cascade on update cascade
);
create table contact_templates (
phone_number character varying (16) not null,
first_name character varying (32) not null
) inherits (message_templates);
create table dice_templates () inherits (message_templates);
-- TODO(определить архитектуру хранения уникальных идентификаторов вложений сообщения)
-- create table poll_messages (
-- telegram_poll_id text not null,
-- poll_schema_id bigint not null,
-- is_complete boolean default false not null,
-- foreign key (poll_schema_id) references poll_schemas on delete cascade on update cascade
-- ) inherits (messages);