1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
From: Scott Kitterman <scott@kitterman.com>
Date: Mon, 23 Dec 2019 11:12:36 -0500
Subject: ticket193
===================================================================
---
db/Makefile.am | 2 +-
db/README.update-db-schema.mysql | 8 ++++++++
db/schema.mysql | 3 ++-
db/update-db-schema.mysql | 12 ++++++++++++
reports/opendmarc-expire.in | 13 ++++++++++++-
5 files changed, 35 insertions(+), 3 deletions(-)
create mode 100644 db/README.update-db-schema.mysql
create mode 100644 db/update-db-schema.mysql
diff --git a/db/Makefile.am b/db/Makefile.am
index 43b8614..83bc1d1 100644
--- a/db/Makefile.am
+++ b/db/Makefile.am
@@ -1,3 +1,3 @@
# Copyright (c) 2012, The Trusted Domain Project. All rights reserved.
-dist_doc_DATA = README.schema schema.mysql
+dist_doc_DATA = README.schema schema.mysql README.update-db-schema.mysql update-db-schema.mysql
diff --git a/db/README.update-db-schema.mysql b/db/README.update-db-schema.mysql
new file mode 100644
index 0000000..8a6a909
--- /dev/null
+++ b/db/README.update-db-schema.mysql
@@ -0,0 +1,8 @@
+
+To update your database to the current state use this script like this:
+
+ mysql -u <user> -p <passwd> --force < update-db-schema.mysql
+
+You might receive up to four errors about duplicate keys - this is expected if your database
+already has these keys (because you used the MySQL schema in the db sub-direcory instead of
+the obsolete schema in the reports sub-dirctory).
diff --git a/db/schema.mysql b/db/schema.mysql
index 059c3de..926d141 100644
--- a/db/schema.mysql
+++ b/db/schema.mysql
@@ -5,6 +5,7 @@
CREATE DATABASE IF NOT EXISTS opendmarc;
USE opendmarc;
+SET TIME_ZONE='+00:00';
-- A table for mapping domain names and their DMARC policies to IDs
CREATE TABLE IF NOT EXISTS domains (
@@ -66,7 +67,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 NOT NULL DEFAULT '1970-01-01 00:00:01',
PRIMARY KEY(id),
KEY(lastsent),
diff --git a/db/update-db-schema.mysql b/db/update-db-schema.mysql
new file mode 100644
index 0000000..5c0a190
--- /dev/null
+++ b/db/update-db-schema.mysql
@@ -0,0 +1,12 @@
+use opendmarc;
+SET TIME_ZONE="+00:00";
+ALTER TABLE ipaddr MODIFY COLUMN addr VARCHAR(64) NOT NULL;
+DELETE FROM ipaddr WHERE addr = NULL;
+ALTER TABLE messages MODIFY COLUMN spf TINYINT NOT NULL;
+ALTER TABLE requests ALTER COLUMN locked SET DEFAULT '0';
+ALTER TABLE requests ALTER COLUMN lastsent SET DEFAULT '1970-01-01 00:00:01';
+ALTER TABLE requests ADD UNIQUE KEY domain (domain);
+ALTER TABLE requests ADD KEY lastsent (lastsent);
+ALTER TABLE messages ADD KEY date (date);
+ALTER TABLE signatures ADD KEY message (message);
+
diff --git a/reports/opendmarc-expire.in b/reports/opendmarc-expire.in
index 326a5a3..0115429 100755
--- a/reports/opendmarc-expire.in
+++ b/reports/opendmarc-expire.in
@@ -210,6 +210,17 @@ if ($verbose)
print STDERR "$progname: connected to database\n";
}
+# switch to UTC to have a defined date behaviour
+$dbi_s = $dbi_h->prepare("SET TIME_ZONE='+00:00'");
+
+if (!$dbi_s->execute())
+{
+ print STDERR "$progname: failed to change to UTC: " . $dbi_h->errstr . "\n";
+ $dbi_s->finish;
+ $dbi_h->disconnect;
+ exit(1);
+}
+
#
# Expire messages
#
@@ -414,7 +425,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 <= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL ? DAY) AND NOT lastsent <= '1970-01-01 00:00:01'");
$rows = $dbi_s->execute($maxage);
if (!$rows)
{
|