2024-08-19 18:16:07 +03:00
|
|
|
create type user_role as enum (
|
|
|
|
'user',
|
|
|
|
'moderator',
|
|
|
|
'admin'
|
|
|
|
);
|
|
|
|
|
2024-08-13 17:46:44 +03:00
|
|
|
create table users (
|
2024-08-30 17:09:22 +03:00
|
|
|
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,
|
2024-08-13 17:46:44 +03:00
|
|
|
primary key (id),
|
|
|
|
unique (telegram_user_id)
|
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
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,
|
2024-08-30 17:09:22 +03:00
|
|
|
is_enabled boolean not null,
|
2024-08-29 18:05:07 +03:00
|
|
|
primary key (id),
|
2024-08-30 17:09:22 +03:00
|
|
|
foreign key (owner_id) references users on delete cascade on update cascade,
|
|
|
|
unique (name)
|
2024-08-29 18:05:07 +03:00
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
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,
|
2024-08-13 17:46:44 +03:00
|
|
|
primary key (id),
|
2024-08-30 17:09:22 +03:00
|
|
|
foreign key (script_id) references scripts on delete cascade on update cascade,
|
2024-08-13 17:46:44 +03:00
|
|
|
unique (name)
|
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
-- TODO(messages ссылается на шаблонную таблицу message_templates)
|
2024-08-30 17:09:22 +03:00
|
|
|
-- 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
|
|
|
|
-- );
|
2024-08-13 17:46:44 +03:00
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
create table text_templates (
|
|
|
|
text character varying (4096) not null
|
|
|
|
) inherits (message_templates);
|
2024-08-19 18:16:07 +03:00
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
create table poll_templates (
|
|
|
|
question character varying (255) not null,
|
|
|
|
anonymous_voting boolean not null,
|
|
|
|
multiple_answers boolean not null
|
|
|
|
) inherits (message_templates);
|
2024-08-19 18:16:07 +03:00
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
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,
|
2024-08-13 17:46:44 +03:00
|
|
|
primary key (id),
|
2024-08-30 17:09:22 +03:00
|
|
|
foreign key (poll_template_id) references poll_templates on delete cascade on update cascade,
|
|
|
|
unique (poll_template_id),
|
|
|
|
unique (poll_template_id, ordinal)
|
2024-08-13 17:46:44 +03:00
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
create table poll_answers (
|
|
|
|
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_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_template_option_id) references poll_template_options on delete cascade on update cascade,
|
|
|
|
unique (poll_template_id, user_id, poll_template_option_id)
|
2024-08-19 18:16:07 +03:00
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
create table media_templates () inherits (message_templates);
|
2024-08-19 18:16:07 +03:00
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
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,
|
2024-08-13 17:46:44 +03:00
|
|
|
primary key (id),
|
2024-08-30 17:09:22 +03:00
|
|
|
foreign key (media_template_id) references media_templates on delete cascade on update cascade
|
2024-08-12 17:25:30 +03:00
|
|
|
);
|
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
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);
|
2024-08-13 17:46:44 +03:00
|
|
|
|
2024-08-30 17:09:22 +03:00
|
|
|
-- 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);
|