From bf9d1166d617474756786b313df401db3757a8d7 Mon Sep 17 00:00:00 2001 From: Martin Kraft Date: Wed, 18 Jan 2023 19:41:13 -0500 Subject: [PATCH] MM-49688: Handles an empty mysql.time_zone_name table. (#22093) --- store/sqlstore/channel_store.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 5d6b9e83f3..d480efdd7c 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -4661,19 +4661,27 @@ func (s SqlChannelStore) PostCountsByDuration(channelIDs []string, sinceUnixMill if loc == "Local" { loc = "UTC" } + var format string if s.DriverName() == model.DatabaseDriverMysql { if duration == model.PostsByDay { - unixSelect = `DATE_FORMAT(CONVERT_TZ(FROM_UNIXTIME(Posts.CreateAt / 1000), 'GMT', '` + loc + `'),'%Y-%m-%d') AS duration` + format = `%Y-%m-%d` } else { - unixSelect = `DATE_FORMAT(CONVERT_TZ(FROM_UNIXTIME(Posts.CreateAt / 1000), 'GMT', '` + loc + `'),'%Y-%m-%dT%H') AS duration` + format = `%Y-%m-%dT%H` } + unixSelect = fmt.Sprintf(`DATE_FORMAT( + COALESCE( + CONVERT_TZ(FROM_UNIXTIME(Posts.CreateAt / 1000), 'GMT', '%s'), + FROM_UNIXTIME(Posts.CreateAt / 1000) + ), + '%s') AS duration`, loc, format) propsQuery = `(JSON_EXTRACT(Posts.Props, '$.from_bot') IS NULL OR JSON_EXTRACT(Posts.Props, '$.from_bot') = 'false') AND (JSON_EXTRACT(Posts.Props, '$.from_webhook') IS NULL OR JSON_EXTRACT(Posts.Props, '$.from_webhook') = 'false') AND (JSON_EXTRACT(Posts.Props, '$.from_plugin') IS NULL OR JSON_EXTRACT(Posts.Props, '$.from_plugin') = 'false') AND (JSON_EXTRACT(Posts.Props, '$.from_oauth_app') IS NULL OR JSON_EXTRACT(Posts.Props, '$.from_oauth_app') = 'false')` } else if s.DriverName() == model.DatabaseDriverPostgres { if duration == model.PostsByDay { - unixSelect = fmt.Sprintf(`TO_CHAR(TO_TIMESTAMP(Posts.CreateAt / 1000) AT TIME ZONE '%s', 'YYYY-MM-DD') AS duration`, loc) + format = "YYYY-MM-DD" } else { - unixSelect = fmt.Sprintf(`TO_CHAR(TO_TIMESTAMP(Posts.CreateAt / 1000) AT TIME ZONE '%s', 'YYYY-MM-DD"T"HH24') AS duration`, loc) + format = `YYYY-MM-DD"T"HH24` } + unixSelect = fmt.Sprintf(`TO_CHAR(TO_TIMESTAMP(Posts.CreateAt / 1000) AT TIME ZONE '%s', '%s') AS duration`, loc, format) propsQuery = `(Posts.Props ->> 'from_bot' IS NULL OR Posts.Props ->> 'from_bot' = 'false') AND (Posts.Props ->> 'from_webhook' IS NULL OR Posts.Props ->> 'from_webhook' = 'false') AND (Posts.Props ->> 'from_oauth_app' IS NULL OR Posts.Props ->> 'from_oauth_app' = 'false') AND (Posts.Props ->> 'from_plugin' IS NULL OR Posts.Props ->> 'from_plugin' = 'false')` } query := sq.