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 scripts ( id bigserial not null, 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) ); 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 (script_id) references scripts on delete cascade on update cascade, unique (name) ); -- 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 text_templates ( text character varying (4096) not null ) inherits (message_templates); 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 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) ); create table media_templates () inherits (message_templates); 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);