Added support for debian stretch/mariadb combo.

fix #142
This commit is contained in:
Antoine Nguyen
2017-08-09 18:48:24 +02:00
parent c32c0ea2ae
commit 01b6ed6095
3 changed files with 44 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
"""Database related tools."""
import os
import platform
import pwd
import stat
@@ -124,32 +125,51 @@ class MySQL(Database):
"""MySQL backend."""
packages = {
"deb": ["mysql-server", "libmysqlclient-dev"],
"deb": ["mariadb-server"],
"rpm": ["mariadb", "mariadb-devel", "mariadb-server"],
}
service = "mariadb" if package.backend.FORMAT == "rpm" else "mysql"
service = "mariadb"
def _escape(self, query):
"""Replace special characters."""
return query.replace("'", "'\"'\"'")
def install_package(self):
"""Preseed package installation."""
package.backend.preconfigure(
"mysql-server", "root_password", "password", self.dbpassword)
package.backend.preconfigure(
"mysql-server", "root_password_again", "password", self.dbpassword)
name, version, _id = platform.linux_distribution()
if name == "debian":
mysql_name = "mysql" if version.startswith("8") else "mariadb"
self.packages["deb"].append("lib{}client-dev".format(mysql_name))
super(MySQL, self).install_package()
if package.backend.FORMAT == "rpm":
utils.exec_cmd("mysqladmin -u root password '{}'".format(
self.dbpassword))
if name == "debian" and version.startswith("8"):
package.backend.preconfigure(
"mariadb-server", "root_password", "password",
self.dbpassword)
package.backend.preconfigure(
"mariadb-server", "root_password_again", "password",
self.dbpassword)
else:
queries = [
"UPDATE user SET plugin='' WHERE user='root'",
"UPDATE user SET password=PASSWORD('{}') WHERE USER='root'"
.format(self.dbpassword),
"flush privileges"
]
for query in queries:
utils.exec_cmd(
"mysql -D mysql -e '{}'".format(self._escape(query)))
def _exec_query(self, query, dbname=None, dbuser=None, dbpassword=None):
"""Exec a mysql query."""
if dbuser is None and dbpassword is None:
dbuser = self.dbuser
dbpassword = self.dbpassword
cmd = "mysql -h {} -u {} -p{}".format(self.dbhost, dbuser, dbpassword)
cmd = "mysql -h {} -u {}".format(self.dbhost, dbuser)
if dbpassword:
cmd += " -p{}".format(dbpassword)
if dbname:
cmd += " -D {}".format(dbname)
query = query.replace("'", "'\"'\"'")
utils.exec_cmd(cmd + """ -e '{}' """.format(query))
utils.exec_cmd(cmd + """ -e '{}' """.format(self._escape(query)))
def create_user(self, name, password):
"""Create a user."""

View File

@@ -157,22 +157,22 @@ CREATE TABLE msgs (
dsn_sent char(1), -- was DSN sent? Y/N/q (q=quenched)
spam_level float, -- SA spam level (no boosts)
message_id varchar(255) DEFAULT '', -- mail Message-ID header field
from_addr varchar(255) CHARACTER SET utf8mb4 COLLATE utf8_bin DEFAULT '',
from_addr varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT '',
-- mail From header field, UTF8
subject varchar(255) CHARACTER SET utf8mb4 COLLATE utf8_bin DEFAULT '',
subject varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT '',
-- mail Subject header field, UTF8
host varchar(255) NOT NULL, -- hostname where amavisd is running
PRIMARY KEY (partition_tag,mail_id)
PRIMARY KEY (partition_tag,mail_id),
FOREIGN KEY (sid) REFERENCES maddr(id) ON DELETE RESTRICT
) ENGINE=InnoDB;
CREATE INDEX msgs_idx_sid ON msgs (sid);
CREATE INDEX msgs_idx_mess_id ON msgs (message_id); -- useful with pen pals
CREATE INDEX msgs_idx_time_num ON msgs (time_num);
-- alternatively when purging based on time_iso (instead of msgs_idx_time_num):
-- CREATE INDEX msgs_idx_time_iso ON msgs (time_iso);
CREATE INDEX msgs_idx_time_iso ON msgs (time_iso);
-- When using FOREIGN KEY contraints, InnoDB requires index on a field
-- (an the field must be the first field in the index). Hence create it:
-- CREATE INDEX msgs_idx_mail_id ON msgs (mail_id);
CREATE INDEX msgs_idx_mail_id ON msgs (mail_id);
-- per-recipient information related to each processed message;
-- NOTE: records in msgrcpt without corresponding msgs.mail_id record are
@@ -191,12 +191,14 @@ CREATE TABLE msgrcpt (
wl char(1) DEFAULT ' ', -- sender whitelisted by this recip
bspam_level float, -- per-recipient (total) spam level
smtp_resp varchar(255) DEFAULT '', -- SMTP response given to MTA
PRIMARY KEY (partition_tag,mail_id,rseqnum)
PRIMARY KEY (partition_tag,mail_id,rseqnum),
FOREIGN KEY (rid) REFERENCES maddr(id) ON DELETE RESTRICT,
FOREIGN KEY (mail_id) REFERENCES msgs(mail_id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE INDEX msgrcpt_idx_mail_id ON msgrcpt (mail_id);
CREATE INDEX msgrcpt_idx_rid ON msgrcpt (rid);
-- Additional index on rs since Modoboa uses it to filter its quarantine
CREATE INDEX msgrcpt_idx_rs ON msgrcpt (rs);
-- mail quarantine in SQL, enabled by $*_quarantine_method='sql:'
-- NOTE: records in quarantine without corresponding msgs.mail_id record are
@@ -206,6 +208,6 @@ CREATE TABLE quarantine (
mail_id varbinary(16) NOT NULL, -- long-term unique mail id
chunk_ind integer unsigned NOT NULL, -- chunk number, starting with 1
mail_text blob NOT NULL, -- store mail as chunks of octets
PRIMARY KEY (partition_tag,mail_id,chunk_ind)
PRIMARY KEY (partition_tag,mail_id,chunk_ind),
FOREIGN KEY (mail_id) REFERENCES msgs(mail_id) ON DELETE CASCADE
) ENGINE=InnoDB;

View File

@@ -173,6 +173,8 @@ CREATE TABLE msgrcpt (
);
CREATE INDEX msgrcpt_idx_mail_id ON msgrcpt (mail_id);
CREATE INDEX msgrcpt_idx_rid ON msgrcpt (rid);
-- Additional index on rs since Modoboa uses it to filter its quarantine
CREATE INDEX msgrcpt_idx_rs ON msgrcpt (rs);
-- mail quarantine in SQL, enabled by $*_quarantine_method='sql:'
-- NOTE: records in quarantine without corresponding msgs.mail_id record are