Modify

Opened 7 years ago

Last modified 7 years ago

#13054 new defect

Issue with 'RecentTopics' macro on PostgreSQL DB

Reported by: ntmlod Owned by:
Priority: normal Component: DiscussionPlugin
Severity: normal Keywords:
Cc: Trac Release: 1.0

Description

Not as an expert in the different SQL implementations but the syntax of get_recent_topics seems not to be compliant with psql

Log error

2017-01-17 15:24:26,276 Trac[formatter] ERROR: Macro RecentTopics(2) failed: 
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/trac/wiki/formatter.py", line 765, in _macro_formatter
    return macro.ensure_inline(macro.process(args))
  File "/usr/lib/python2.7/site-packages/trac/wiki/formatter.py", line 356, in process
    text = self.processor(text)
  File "/usr/lib/python2.7/site-packages/trac/wiki/formatter.py", line 343, in _macro_processor
    text)
  File "build/bdist.linux-x86_64/egg/tracdiscussion/wiki.py", line 102, in expand_macro
    return self._recent_topics(formatter, content)
  File "build/bdist.linux-x86_64/egg/tracdiscussion/wiki.py", line 169, in _recent_topics
    entries = self.api.get_recent_topics(context, forum_id, limit)
  File "build/bdist.linux-x86_64/egg/tracdiscussion/model.py", line 253, in get_recent_topics
    for row in self.env.db_query(sql, values)]
  File "/usr/lib/python2.7/site-packages/trac/db/api.py", line 122, in execute
    return db.execute(query, params)
  File "/usr/lib/python2.7/site-packages/trac/db/util.py", line 121, in execute
    cursor.execute(query, params)
  File "/usr/lib/python2.7/site-packages/trac/db/util.py", line 65, in execute
    return self.cursor.execute(sql_escape_percent(sql), args)
ProgrammingError: subquery in FROM must have an alias
LINE 1: SELECT forum, topic, MAX(time) as max_time  FROM  (SELECT fo...
                                                          ^
HINT:  For example, FROM (SELECT ...) [AS] foo.

My 2 cents fix

  • tracdiscussion/model.py

     
    231231
    232232    def get_recent_topics(self, context, forum_id, limit):
    233233        columns = ('forum', 'topic', 'time')
    234         sql = ("SELECT forum, topic, MAX(time) as max_time"
     234        sql = ("SELECT forum, topic, max(time)"
    235235               "  FROM"
    236236               "  (SELECT forum, topic, time"
    237237                   " FROM message"
    238238               "   UNION"
    239239               "   SELECT forum, id as topic, time"
    240                    " FROM topic)" +
    241                (forum_id and " WHERE forum=%s" or '') +
    242                " GROUP BY topic"
    243                " ORDER BY max_time DESC" +
     240                   " FROM topic)"
     241           "  AS foo" +
     242               (forum_id and " WHERE foo.forum=%s" or '') +
     243               " GROUP BY foo.topic, foo.forum, foo.time"
     244               " ORDER BY time DESC" +
    244245               (limit and " LIMIT %s" or ''))
    245246        values = []
    246247        if forum_id:

Attachments (0)

Change History (3)

comment:1 Changed 7 years ago by Ryan J Ollos

If you added a unit test we could evaluate the patch for the 3 database types.

comment:2 Changed 7 years ago by ntmlod

After a review, my initial patch was correct but did not give one result per topic

After several query tries, I defined this new fix

  • discussionplugin/1.0/tracdiscussion/model.py

     
    231231
    232232    def get_recent_topics(self, context, forum_id, limit):
    233233        columns = ('forum', 'topic', 'time')
    234         sql = ("SELECT forum, topic, MAX(time) as max_time"
     234        sql = ("SELECT forum, topic, MAX(time) as time"
    235235               "  FROM"
    236236               "  (SELECT forum, topic, time"
    237237                   " FROM message"
    238238               "   UNION"
    239239               "   SELECT forum, id as topic, time"
    240                    " FROM topic)" +
     240                   " FROM topic) AS foo" +
    241241               (forum_id and " WHERE forum=%s" or '') +
    242                " GROUP BY topic"
    243                " ORDER BY max_time DESC" +
     242               " GROUP BY topic, forum"
     243               " ORDER BY time DESC" +
    244244               (limit and " LIMIT %s" or ''))
    245245        values = []
    246246        if forum_id:

It seems that we can't rename MAX aggregator differently from 'time', must set an alias for the sub-query and add 'forum' argument in GROUP BY as a consequence.

Last edited 7 years ago by ntmlod (previous) (diff)

comment:3 Changed 7 years ago by Jun Omae

Summary: Issue with 'RecentChanges' macro on PostgreSQL DBIssue with 'RecentTopics' macro on PostgreSQL DB

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.