-- Copyright (c) 2010-2013, The Trusted Domain Project.  All rights reserved.

-- MySQL command sequence to create a database to accumulate OpenDKIM
-- reputation data

-- table containing data aggregation for staging to predictions
create table if not exists aggregate (
	id int auto_increment,
	reporter int,
	domain int not null,
	updated timestamp not null default current_timestamp on update current_timestamp,
	date timestamp not null default '0000-00-00 00:00:00',
	messages int,
	spam int,
	ratio float,

	primary key (id),
	unique key (domain, date, reporter)
) engine=innodb default charset=latin1;

-- table mapping domain codes to flow predictions
create table if not exists predictions (
	id int not null auto_increment,
	reporter int,
	domain int not null,
	name varchar(255),
	updated timestamp not null default current_timestamp on update current_timestamp,
	rate_samples int,
	rate_max int,
	rate_avg float,
	rate_stddev float,
	rate_high int,
	ratio_max float,
	ratio_avg float,
	ratio_stddev float,
	ratio_high float,
	daily_limit_high int,
	daily_limit_low int,
	today_mail int,
	today_spam float,

	primary key (id),
	key (name),
	unique key (domain, reporter)
) engine=innodb default charset=latin1;

-- table containing reputation history
create table if not exists history (
	id int not null auto_increment,
	date timestamp not null,
	domain int not null,
	maxmsgs int,
	maxspam float,
	maxlimit int,

	primary key(id),
	unique key(domain, date)
) engine=innodb default charset=latin1;

-- alter domains table to contain "low time" flag
alter table domains add column low_time tinyint unsigned not null default 0;

-- For upgrading from pre-2.8.0 releases:
--
-- alter table history add column maxmsgs int after domain, change
-- column ratio maxspam float, change column msglimit maxlimit int;        
