drop table if exists forum_posts cascade;
create table forum_posts(
	forum_post_id serial not null,
	forum_post_parentid integer references forum_posts( forum_post_id ),
	forum_topic_id integer not null references forum_topics( forum_topic_id ),
	owner_user_id integer references users( user_id ),
	ctstamp timestamp without time zone,
	poster_name varchar(128),
	poster_email varchar(128),
	post text,
	ipaddress varchar(16),
	hostname varchar(255),
	is_admin_acknowledged boolean default false,
	is_admin_moderated boolean default false,
	meta_post_number integer not null,
	meta_ordering integer not null,
	oldid integer,
	constraint forum_posts_pkey primary key( forum_post_id )
);

comment on table forum_posts is 'storage of forum posts';
comment on column forum_posts.forum_post_id is 'unique id of table';
comment on column forum_posts.forum_post_parentid is 'parent id of post';
comment on column forum_posts.forum_topic_id is 'reference id to forum_topics table';
comment on column forum_posts.owner_user_id is 'reference id to users table';
comment on column forum_posts.ctstamp is 'post creation timestamp';
comment on column forum_posts.poster_name is 'name of the poster (old version = the name, new version = cache of user_name in users table)';
comment on column forum_posts.poster_email is 'email of the poster (old version = the email, new version = cache of email in users table)';
comment on column forum_posts.post is 'the post itself';
comment on column forum_posts.ipaddress is 'ipaddress of the poster';
comment on column forum_posts.hostname is 'resolved hostname of the poster';
comment on column forum_posts.is_admin_acknowledged is 'is admin acknowledged flag (false = not seen by admin)';
comment on column forum_posts.is_admin_moderated is 'moderated flag of admin';
comment on column forum_posts.meta_post_number is 'number of post in the current thread';
comment on column forum_posts.meta_ordering is 'ordering of the post (should be recalculated on moderation / deletion)';
comment on column forum_posts.oldid is 'migrational data';

comment on constraint forum_posts_pkey on forum_posts is 'unique constraint of table';
comment on constraint forum_posts_forum_post_parentid_fkey on forum_posts is 'parental constraint';
comment on constraint forum_posts_owner_user_id_fkey on forum_posts is 'foreign constraint to users table';
comment on constraint forum_posts_forum_topic_id_fkey on forum_posts is 'foreign constraint to forum_topics table';

create index forum_posts_threadlocal_post_number_ind on forum_posts( forum_topic_id , meta_post_number );
comment on index forum_posts_threadlocal_post_number_ind is 'index on topic_id / meta_post_number';
create index forum_posts_threadlocal_ordering_ind on forum_posts( forum_topic_id , meta_ordering );
comment on index forum_posts_threadlocal_ordering_ind is 'index on topic_id / meta_ordering';
create index forum_posts_threadlocal_moderated_ind on forum_posts( forum_topic_id , is_admin_moderated );
comment on index forum_posts_threadlocal_moderated_ind is 'index on topic_id / is_admin_moderated';
