switch from utf8 to utf8mb4 on the database level

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2017-06-24 06:07:38 +00:00
parent 44760a7e0f
commit 1a0acb0185
10 changed files with 39 additions and 28 deletions

View File

@ -195,6 +195,10 @@ memcached_to_db_interval=900
; mysql stuff ; mysql stuff
; ;
// this can be either utf8 or utf8mb4. Make sure to match the value
// to the charset of the piler database! Also, make sure to set this
// value in sphinx.conf
mysqlcharset=utfmb4
;mysqlhost=127.0.0.1 ;mysqlhost=127.0.0.1
;mysqlport=3306 ;mysqlport=3306
mysqlsocket=/var/run/mysqld/mysqld.sock mysqlsocket=/var/run/mysqld/mysqld.sock

View File

@ -17,7 +17,7 @@ source base
source delta : base source delta : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query_pre = REPLACE INTO sph_counter SELECT 1, IFNULL(MAX(id), 0) FROM sph_index sql_query_pre = REPLACE INTO sph_counter SELECT 1, IFNULL(MAX(id), 0) FROM sph_index
sql_query_post_index = DELETE FROM sph_index WHERE id<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1) sql_query_post_index = DELETE FROM sph_index WHERE id<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1)
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index \ sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index \
@ -28,37 +28,37 @@ source delta : base
source main1 : base source main1 : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1 sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1
} }
source main2 : base source main2 : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1 sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1
} }
source main3 : base source main3 : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1 sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1
} }
source main4 : base source main4 : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1 sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1
} }
source dailydelta : base source dailydelta : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1 sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1
} }
source tag : base source tag : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT `_id`, `id` AS iid, `uid`, `tag` FROM `tag` sql_query = SELECT `_id`, `id` AS iid, `uid`, `tag` FROM `tag`
sql_attr_uint = iid sql_attr_uint = iid
@ -68,7 +68,7 @@ source tag : base
source note : base source note : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = SELECT `_id`, `id` AS iid, `uid`, `note` FROM `note` sql_query = SELECT `_id`, `id` AS iid, `uid`, `note` FROM `note`
sql_attr_uint = iid sql_attr_uint = iid
@ -79,7 +79,7 @@ source note : base
source att : base source att : base
{ {
sql_query_pre = SET NAMES utf8 sql_query_pre = SET NAMES utf8mb4
sql_query = select a.id as aid, m.id as mid, a.name AS aname, a.size, REPLACE(REPLACE(m.`from`, '@', 'X'), '.', 'X') as `from`, REPLACE(REPLACE((select group_concat(rcpt.`to` ORDER BY `to` ASC SEPARATOR ' ') from rcpt where rcpt.id=mid group by rcpt.id), '@', 'X'), '.', 'X') as `to` from attachment a, metadata m where m.piler_id=a.piler_id sql_query = select a.id as aid, m.id as mid, a.name AS aname, a.size, REPLACE(REPLACE(m.`from`, '@', 'X'), '.', 'X') as `from`, REPLACE(REPLACE((select group_concat(rcpt.`to` ORDER BY `to` ASC SEPARATOR ' ') from rcpt where rcpt.id=mid group by rcpt.id), '@', 'X'), '.', 'X') as `to` from attachment a, metadata m where m.piler_id=a.piler_id
sql_attr_uint = size sql_attr_uint = size

View File

@ -88,6 +88,7 @@ struct _parse_rule config_parse_rules[] =
{ "min_message_size", "integer", (void*) int_parser, offsetof(struct __config, min_message_size), "100", sizeof(int)}, { "min_message_size", "integer", (void*) int_parser, offsetof(struct __config, min_message_size), "100", sizeof(int)},
{ "min_word_len", "integer", (void*) int_parser, offsetof(struct __config, min_word_len), "1", sizeof(int)}, { "min_word_len", "integer", (void*) int_parser, offsetof(struct __config, min_word_len), "1", sizeof(int)},
{ "mmap_dedup_test", "integer", (void*) int_parser, offsetof(struct __config, mmap_dedup_test), "0", sizeof(int)}, { "mmap_dedup_test", "integer", (void*) int_parser, offsetof(struct __config, mmap_dedup_test), "0", sizeof(int)},
{ "mysqlcharset", "string", (void*) string_parser, offsetof(struct __config, mysqlcharset), "utf8mb4", MAXVAL-1},
{ "mysqlhost", "string", (void*) string_parser, offsetof(struct __config, mysqlhost), "", MAXVAL-1}, { "mysqlhost", "string", (void*) string_parser, offsetof(struct __config, mysqlhost), "", MAXVAL-1},
{ "mysqlport", "integer", (void*) int_parser, offsetof(struct __config, mysqlport), "", sizeof(int)}, { "mysqlport", "integer", (void*) int_parser, offsetof(struct __config, mysqlport), "", sizeof(int)},
{ "mysqlsocket", "string", (void*) string_parser, offsetof(struct __config, mysqlsocket), "/tmp/mysql.sock", MAXVAL-1}, { "mysqlsocket", "string", (void*) string_parser, offsetof(struct __config, mysqlsocket), "/tmp/mysql.sock", MAXVAL-1},

View File

@ -65,6 +65,7 @@ struct __config {
// mysql stuff // mysql stuff
char mysqlcharset[MAXVAL];
char mysqlhost[MAXVAL]; char mysqlhost[MAXVAL];
int mysqlport; int mysqlport;
char mysqlsocket[MAXVAL]; char mysqlsocket[MAXVAL];

View File

@ -11,6 +11,7 @@
int open_database(struct session_data *sdata, struct __config *cfg){ int open_database(struct session_data *sdata, struct __config *cfg){
int rc=1; int rc=1;
char buf[BUFLEN];
mysql_init(&(sdata->mysql)); mysql_init(&(sdata->mysql));
@ -22,8 +23,11 @@ int open_database(struct session_data *sdata, struct __config *cfg){
return ERR; return ERR;
} }
mysql_real_query(&(sdata->mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); snprintf(buf, sizeof(buf)-2, "SET NAMES %s", cfg->mysqlcharset);
mysql_real_query(&(sdata->mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); mysql_real_query(&(sdata->mysql), buf, strlen(buf));
snprintf(buf, sizeof(buf)-2, "SET CHARACTER SET %s", cfg->mysqlcharset);
mysql_real_query(&(sdata->mysql), buf, strlen(buf));
return OK; return OK;
} }

View File

@ -1,4 +1,4 @@
create database MYSQL_DATABASE character set 'utf8'; create database MYSQL_DATABASE character set 'utf8mb4';
grant all privileges on MYSQL_DATABASE.* to MYSQL_USERNAME@localhost identified by 'MYSQL_PASSWORD'; grant all privileges on MYSQL_DATABASE.* to MYSQL_USERNAME@localhost identified by 'MYSQL_PASSWORD';
flush privileges; flush privileges;

View File

@ -12,10 +12,10 @@ create table if not exists `sph_index` (
`to` text(8192) default null, `to` text(8192) default null,
`fromdomain` char(255) default null, `fromdomain` char(255) default null,
`todomain` text(512) default null, `todomain` text(512) default null,
`subject` text(512) default null, `subject` blob(512) default null,
`arrived` int unsigned not null, `arrived` int unsigned not null,
`sent` int unsigned not null, `sent` int unsigned not null,
`body` text, `body` mediumblob,
`size` int default '0', `size` int default '0',
`direction` int default 0, `direction` int default 0,
`folder` int default 0, `folder` int default 0,
@ -29,7 +29,7 @@ create table if not exists `metadata` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` varchar(128) not null, `from` varchar(128) not null,
`fromdomain` varchar(64) not null, `fromdomain` varchar(64) not null,
`subject` text(512) default null, `subject` blob(512) default null,
`spam` tinyint(1) default 0, `spam` tinyint(1) default 0,
`arrived` int unsigned not null, `arrived` int unsigned not null,
`sent` int unsigned not null, `sent` int unsigned not null,
@ -117,7 +117,7 @@ create table if not exists `archiving_rule` (
`_size` char(2) default null, `_size` char(2) default null,
`size` int default 0, `size` int default 0,
`attachment_name` varchar(128) default null, `attachment_name` varchar(128) default null,
`attachment_type` varchar(128) default null, `attachment_type` varchar(64) default null,
`_attachment_size` char(2) default null, `_attachment_size` char(2) default null,
`attachment_size` int default 0, `attachment_size` int default 0,
`spam` tinyint(1) default -1, `spam` tinyint(1) default -1,
@ -130,15 +130,15 @@ create table if not exists `archiving_rule` (
create table if not exists `retention_rule` ( create table if not exists `retention_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`domain` varchar(128) default null, `domain` varchar(100) default null,
`from` varchar(128) default null, `from` varchar(100) default null,
`to` varchar(128) default null, `to` varchar(100) default null,
`subject` varchar(128) default null, `subject` varchar(128) default null,
`body` varchar(128) default null, `body` varchar(128) default null,
`_size` char(2) default null, `_size` char(2) default null,
`size` int default 0, `size` int default 0,
`attachment_name` varchar(128) default null, `attachment_name` varchar(100) default null,
`attachment_type` varchar(128) default null, `attachment_type` varchar(64) default null,
`_attachment_size` char(2) default null, `_attachment_size` char(2) default null,
`attachment_size` int default 0, `attachment_size` int default 0,
`spam` tinyint(1) default -1, `spam` tinyint(1) default -1,
@ -151,15 +151,15 @@ create table if not exists `retention_rule` (
create table if not exists `folder_rule` ( create table if not exists `folder_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`domain` varchar(128) default null, `domain` varchar(100) default null,
`from` varchar(128) default null, `from` varchar(100) default null,
`to` varchar(128) default null, `to` varchar(100) default null,
`subject` varchar(128) default null, `subject` varchar(128) default null,
`body` varchar(128) default null, `body` varchar(128) default null,
`_size` char(2) default null, `_size` char(2) default null,
`size` int default 0, `size` int default 0,
`attachment_name` varchar(128) default null, `attachment_name` varchar(128) default null,
`attachment_type` varchar(128) default null, `attachment_type` varchar(64) default null,
`_attachment_size` char(2) default null, `_attachment_size` char(2) default null,
`attachment_size` int default 0, `attachment_size` int default 0,
`spam` tinyint(1) default -1, `spam` tinyint(1) default -1,

View File

@ -202,6 +202,7 @@ $config['DB_HOSTNAME'] = 'localhost';
$config['DB_USERNAME'] = 'piler'; $config['DB_USERNAME'] = 'piler';
$config['DB_PASSWORD'] = 'piler'; $config['DB_PASSWORD'] = 'piler';
$config['DB_DATABASE'] = 'piler'; $config['DB_DATABASE'] = 'piler';
$config['DB_CHARSET'] = 'utf8mb4';
$config['SPHINX_DRIVER'] = 'sphinx'; $config['SPHINX_DRIVER'] = 'sphinx';
$config['SPHINX_DATABASE'] = 'sphinx'; $config['SPHINX_DATABASE'] = 'sphinx';

View File

@ -10,8 +10,8 @@ class MySQL {
try { try {
$this->link = new PDO("mysql:host=$hostname;dbname=$database", $username, $password, $this->link = new PDO("mysql:host=$hostname;dbname=$database", $username, $password,
array( array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . DB_CHARSET,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8" PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET " . DB_CHARSET
) )
); );

View File

@ -9,7 +9,7 @@ class Sphinx {
list($host, $port) = explode(":", $hostname); list($host, $port) = explode(":", $hostname);
try { try {
$this->link = new PDO("mysql:host=$host;port=$port;dbname=$database;charset=utf8", $username, $password); $this->link = new PDO("mysql:host=$host;port=$port;dbname=$database;charset=" . DB_CHARSET, $username, $password);
} }
catch(PDOException $exception) { catch(PDOException $exception) {
exit('Error: ' . $exception->getMessage() . " on database: $database<br />"); exit('Error: ' . $exception->getMessage() . " on database: $database<br />");