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

-- MySQL command sequence to create a database to accumulate OpenDMARC
-- report data

-- table mapping domain names to id numbers
CREATE TABLE domains (
	id INT(11) NOT NULL AUTO_INCREMENT,
	name VARCHAR(255) NOT NULL,
	firstseen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY(id),
	UNIQUE KEY(name)
) ENGINE=innodb DEFAULT CHARSET=latin1;

-- table mapping IP addresses to id numbers
CREATE TABLE ipaddr (
	id INT(11) NOT NULL AUTO_INCREMENT,
	addr VARCHAR(64) DEFAULT NULL,
	firstseen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY(id),
	UNIQUE KEY(addr)
) ENGINE=innodb DEFAULT CHARSET=latin1;

-- table tracking message-specific data
CREATE TABLE messages (
	id INT(11) NOT NULL AUTO_INCREMENT,
	date TIMESTAMP NOT NULL default CURRENT_TIMESTAMP,
	jobid VARCHAR(128) NOT NULL,
	reporter INT(10) UNSIGNED NOT NULL,
	ip INT(10) UNSIGNED NOT NULL,
	policy TINYINT(3) UNSIGNED NOT NULL,
	disp TINYINT(3) UNSIGNED NOT NULL,
	from_domain INT(10) UNSIGNED NOT NULL,
	env_domain INT(10) UNSIGNED NOT NULL,
	policy_domain INT(10) UNSIGNED NOT NULL,
	sigcount TINYINT(3) UNSIGNED NOT NULL,
	spf TINYINT(3) NOT NULL,
	align_spf TINYINT(3) UNSIGNED NOT NULL,
	align_dkim TINYINT(3) UNSIGNED NOT NULL,
	PRIMARY KEY(id),
	UNIQUE KEY(reporter,date,jobid)
) ENGINE=innodb DEFAULT CHARSET=latin1;

-- table mapping reporters to ids
CREATE TABLE reporters (
	id INT(11) NOT NULL AUTO_INCREMENT,
	name VARCHAR(255) NOT NULL,
	firstseen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY(id),
	UNIQUE KEY(name)
) ENGINE=innodb DEFAULT CHARSET=latin1;

-- table tracking report requests
CREATE TABLE requests (
	id INT(11) NOT NULL AUTO_INCREMENT,
	domain INT(11) NOT NULL,
	repuri VARCHAR(255) NOT NULL,
	pct TINYINT(4) NOT NULL,
	policy TINYINT(4) NOT NULL,
	spolicy TINYINT(4) NOT NULL,
	aspf TINYINT(4) NOT NULL,
	adkim TINYINT(4) NOT NULL,
	locked TINYINT(4) NOT NULL DEFAULT '0',
	firstseen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	lastsent TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
	PRIMARY KEY(id)
) ENGINE=innodb DEFAULT CHARSET=latin1;

-- table for tracking DKIM signature evaluation results
CREATE TABLE signatures (
	id INT(11) NOT NULL AUTO_INCREMENT,
	message INT(11) NOT NULL,
	domain INT(11) NOT NULL,
	pass TINYINT(4) NOT NULL,
	error TINYINT(4) NOT NULL,
	PRIMARY KEY(id)
) ENGINE=innodb DEFAULT CHARSET=latin1;
