cit-is-bot-backend/database.sql

117 строки
5.1 KiB
SQL

create type user_role as enum (
'user',
'moderator',
'admin'
);
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,
primary key (id),
unique (telegram_user_id)
);
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,
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,
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)
);
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_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,
primary key (id),
foreign key (media_message_id) references media_messages 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 dice_messages (
value smallint not null
);
-- 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,
primary key (id),
foreign key (poll_id) references polls 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)
);
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);
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);