alter table ml_mailing_lists add remind_subject	text;
alter table ml_mailing_lists add remind_body text;
alter table ml_mailing_lists add remind_mime_type varchar(30);
alter table ml_mailing_lists alter column remind_mime_type set default 'text/plain';

alter table ml_mailing_list_user_map add reminder_count integer;
alter table ml_mailing_list_user_map alter column reminder_count set default 0;

create table ml_user_email_log (
       user_id		  integer primary key
       			  constraint ml_user_email_log_id_fk
			  references users,
       last_mail_date	  timestamptz default null
);

create index ml_user_email_log_date_ix on ml_user_email_log(last_mail_date);

insert into ml_user_email_log
(select m.user_id, max(l.send_date) as last_mail_date
from ml_mailing_list_user_map m
left outer join ml_email_log l
on (m.user_id = l.user_id)
group by m.user_id);
