view database/schema/messages.sql @ 54:36d91dfe017f

use valid sql syntax, mostly from yetoo on freenode
author David A. Holland
date Sun, 10 Apr 2022 17:41:24 -0400
parents e1017d556437
children 40f64a96481f
line wrap: on
line source

--
-- Messages.
--

CREATE SEQUENCE next_rawmsgid;

-- all incoming mail in original form, for reference
-- (should be pruned periodically)
CREATE TABLE rawmail (
	id bigint		primary key default nextval('next_rawmsgid'),
	posttime timestamp	not null,
	data text		not null
)
WITHOUT OIDS;

CREATE SEQUENCE next_msgid;

-- comments
CREATE TABLE messages (
	id bigint		primary key default nextval('next_msgid'),
	pr bigint		not null references prs (id),
	number_in_pr bigint	not null,
	who bigint		not null references users (id),
	parent_id bigint	null references messages (id),
	posttime timestamp	not null,
	mimetype text		not null,
	body text		not null,

	-- we don't keep these directly, they go into an admin log entry
	-- XXX: we may need to keep the external message-id
	--from_address text	not null,
	--mail_subject text	not null,
	--message_id text	not null,
	rawid bigint		null references rawmail (id)

	check (parent_id != id)
)
WITHOUT OIDS;

CREATE SEQUENCE next_attachid;

-- for patches and mime-attachments
CREATE TABLE attachments (
	id bigint		primary key default nextval('next_attachid'),
	msgid bigint		not null references messages (id),
	number_in_msg bigint	not null,
	mimetype text		not null,
	body text		not null
)
WITHOUT OIDS;

-- Create indexes for number_in_pr and number_in_msg both to enforce
-- uniqueness and to enable lookup.
CREATE UNIQUE INDEX ON messages (pr, number_in_pr);
CREATE UNIQUE INDEX ON attachments (msgid, number_in_msg);