Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion db/README.schema
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,26 @@ requests A table containing a cache of DMARC reporting requests.
For each domain, the destination reporting URI for aggregate
reports is recorded along with a "last report sent" timestamp.

UPGRADING AN EXISTING SCHEMA
----------------------------

If you are upgrading from an older installation rather than starting fresh,
the following ALTER TABLE statements will bring your schema up to date.

These are also shown by opendmarc-reports and opendmarc-import at startup
if the relevant columns are missing.

1. requests.lastsent (introduced to fix MySQL strict mode incompatibility):

ALTER TABLE requests MODIFY lastsent TIMESTAMP NULL DEFAULT NULL;

2. messages.spf_scope (introduced to support RFC 7489 <scope> in reports):

ALTER TABLE messages ADD COLUMN spf_scope TINYINT NOT NULL DEFAULT '-1' AFTER spf;

3. requests.fo (introduced to support RFC 7489 <fo> in reports):

ALTER TABLE requests ADD COLUMN fo TINYINT NOT NULL DEFAULT '0' AFTER pct;

--
Copyright (c) 2012, 2018, The Trusted Domain Project. All rights reserved.
Copyright (c) 2012, 2018, 2026, The Trusted Domain Project. All rights reserved.
2 changes: 1 addition & 1 deletion db/schema.mysql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CREATE TABLE IF NOT EXISTS requests (
pct TINYINT NOT NULL DEFAULT '0',
locked TINYINT NOT NULL DEFAULT '0',
firstseen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastsent TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00',
lastsent TIMESTAMP NULL DEFAULT NULL,

PRIMARY KEY(id),
KEY(lastsent),
Expand Down
2 changes: 1 addition & 1 deletion reports/opendmarc-expire.in
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ if ($verbose)
print STDERR "$progname: expiring request data older than $maxage days\n";
}

$dbi_s = $dbi_h->prepare("DELETE FROM requests WHERE lastsent <= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL ? DAY) AND NOT lastsent = '0000-00-00 00:00:00'");
$dbi_s = $dbi_h->prepare("DELETE FROM requests WHERE lastsent IS NOT NULL AND lastsent <= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL ? DAY)");
$rows = $dbi_s->execute($maxage);
if (!$rows)
{
Expand Down
19 changes: 17 additions & 2 deletions reports/opendmarc-reports.in
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ if ($use_utc)
}
}

#
# Check schema is current
#

$dbi_s = $dbi_h->prepare("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'requests' AND COLUMN_NAME = 'lastsent'");
if ($dbi_s->execute() && (my $row = $dbi_s->fetchrow_arrayref()))
{
if ($row->[0] eq 'NO')
{
print STDERR "$progname: WARNING: requests.lastsent column is NOT NULL; schema needs upgrading.\n";
print STDERR "$progname: WARNING: Run: ALTER TABLE requests MODIFY lastsent TIMESTAMP NULL DEFAULT NULL;\n";
}
}
$dbi_s->finish;

#
# Select domains on which to report
#
Expand All @@ -360,7 +375,7 @@ if (defined($forcedomain))
}
elsif ($daybound)
{
$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE DATE(lastsent) < DATE(FROM_UNIXTIME(?))");
$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE lastsent IS NULL OR DATE(lastsent) < DATE(FROM_UNIXTIME(?))");

if (!$dbi_s->execute($now))
{
Expand All @@ -372,7 +387,7 @@ elsif ($daybound)
}
else
{
$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE lastsent <= DATE_SUB(FROM_UNIXTIME(?), INTERVAL ? SECOND)");
$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE lastsent IS NULL OR lastsent <= DATE_SUB(FROM_UNIXTIME(?), INTERVAL ? SECOND)");

if (!$dbi_s->execute($now, $interval))
{
Expand Down