likeopera-backend/database/db.sql

108 lines
3.6 KiB
MySQL
Raw Normal View History

create extension if not exists btree_gin;
2016-06-27 14:14:10 +03:00
create table accounts (
id serial not null primary key,
2019-05-16 21:29:58 +03:00
name text not null,
email text not null,
2016-06-27 14:14:10 +03:00
settings jsonb not null
-- настройки: replyto, cc, bcc, imap, smtp, folders
2016-06-27 14:14:10 +03:00
);
create unique index accounts_email on accounts (email);
2016-06-27 14:14:10 +03:00
create table folders (
id serial not null primary key,
uidvalidity int not null,
account_id int not null,
2019-05-16 21:29:58 +03:00
name text not null,
2016-08-01 14:05:08 +03:00
highestmodseq int not null default 0,
2019-05-16 21:29:58 +03:00
kind text not null,
2016-06-27 14:14:10 +03:00
foreign key (account_id) references accounts (id) on delete cascade on update cascade
);
2016-07-31 15:05:14 +03:00
create unique index folders_name on folders (account_id, name);
2016-06-27 14:14:10 +03:00
2019-05-17 16:57:37 +03:00
-- Очередь команд. Виды команд:
-- 1) пометить сообщение
-- 2) переместить сообщение
-- 3) удалить сообщение
-- 4) отправить черновик
create table command_queue (
id serial not null primary key,
account_id int not null,
folder_id int,
uid int,
props jsonb not null
);
-- Ещё нигде не сохранённые черновики сообщений
create table drafts (
id serial not null primary key,
time bigint not null,
props jsonb not null
);
2016-06-27 14:14:10 +03:00
create table messages (
id serial not null primary key,
2016-07-17 00:35:35 +03:00
thread_id int,
2016-06-28 01:07:35 +03:00
folder_id int not null,
uid int,
2019-05-16 21:29:58 +03:00
messageid text not null,
inreplyto text not null,
refs text[] not null,
2016-06-27 14:14:10 +03:00
subject text not null,
props jsonb not null,
body_html text not null default '',
body_text text not null default '',
body_html_text text not null default '',
2019-05-17 16:57:37 +03:00
-- FIXME bigint
2016-06-27 14:14:10 +03:00
time timestamptz not null,
size int not null,
2019-05-17 16:57:37 +03:00
-- FIXME jsonb
2019-05-16 21:29:58 +03:00
flags text[] not null,
2016-06-27 14:14:10 +03:00
foreign key (folder_id) references folders (id) on delete cascade on update cascade
);
2016-06-28 01:07:35 +03:00
create unique index messages_unique on messages (folder_id, uid);
create index messages_flags on messages using gin (folder_id, flags);
create index messages_messageid on messages (messageid);
2016-07-17 00:35:35 +03:00
create index messages_refs on messages using gin (refs);
create index messages_folder_id_time on messages (folder_id, time);
create index messages_time on messages (time);
-- create or replace function immutable_year(d timestamptz) returns text
-- language plpgsql immutable as $$
-- begin
-- return date_part('year', d);
-- end
-- $$;
-- create index messages_year on messages (immutable_year(time));
create or replace function messages_fulltext(msg messages) returns tsvector
language plpgsql immutable as $$
2016-08-28 00:18:04 +03:00
begin
return setweight(to_tsvector('russian', regexp_replace(
coalesce(msg.props->>'from', '') || ' ' ||
coalesce(msg.props->>'replyto', '') || ' ' ||
coalesce(msg.props->>'to', '') || ' ' ||
coalesce(msg.props->>'cc', '') || ' ' ||
coalesce(msg.props->>'bcc', '') || ' ' ||
coalesce(msg.props->>'attachments', '') || ' ' ||
msg.subject,
'\W+', ' ', 'g'
)), 'A')
|| setweight(to_tsvector('russian', msg.body_html_text || ' ' || msg.body_text), 'B');
2016-08-28 00:18:04 +03:00
end
$$;
create index messages_text on messages using gin (messages_fulltext(messages));
2016-07-08 01:15:19 +03:00
create table threads (
id serial not null primary key,
first_msg int,
2016-07-08 01:15:19 +03:00
msg_count int not null default 1,
foreign key (first_msg) references messages (id) on delete restrict on update cascade
);
2016-08-28 00:18:04 +03:00
create index threads_first_msg on threads (first_msg);
2016-07-08 01:15:19 +03:00
alter table messages add foreign key (thread_id) references threads (id) on delete restrict on update cascade;
alter table accounts owner to operetta;
alter table folders owner to operetta;
alter table messages owner to operetta;
alter table threads owner to operetta;