Patch #33290

Stop DB access when IssueQuery class is defined

Added by Kevin Fischer 11 months ago. Updated 22 days ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:Jean-Philippe Lang% Done:


Category:Plugin API
Target version:4.1.2


During plugin development I found out that the definition of the Query class accesses the database just by being defined.

The reason for this is the `groupable` method in the `TimestampQueryColumn` class which tries to access the current user.

This method is called during the constructor (which is called in the query column definitions in the class body of IssueQuery)

Why is this a problem?

In case your database is not yet created and/or migrated, any source code which just references the IssueQuery class will raise an exception since it cannot find the database/table.

And in general I think it's not good to access the database just by defining a class.


I attached a patch which also acts as a code cleanup/refactoring. It moves the group by statement into its own method. Before the boolean property `groupable` was just redefined to contain the group by statement as well, which was confusing in the first place in my opinion.

I ran all tests locally and they passed.

0001-stop-db-access-on-class-definition.patch Magnifier (1.65 KB) Kevin Fischer, 2020-04-15 10:44

0001-stop-db-access-on-class-definition-v2.patch Magnifier (3.08 KB) Kevin Fischer, 2021-02-05 11:54

Related issues

Related to Redmine - Feature #2679: Ticket grouping Closed 2009-02-05
Related to Redmine - Feature #13803: Implement grouping issues by date (start, due, creation, ... Closed
Related to Redmine - Defect #33121: IssueQuery not usable from plugins New


#1 Updated by Go MAEDA 11 months ago

#2 Updated by Kevin Fischer 11 months ago

This only happens from Redmine 4.1 by the way.

The commit bringing the change was r17724

#3 Updated by Alexander Meindl 11 months ago

I can confirm this problem with Redmine 4.1. It is very difficult to test plugins with the bug.

#4 Updated by Go MAEDA 11 months ago

  • Related to Feature #13803: Implement grouping issues by date (start, due, creation, update, closing dates) added

#5 Updated by Kevin Fischer 8 months ago

Is there any chance this change will be considered for the next version?

If you reference the IssueQuery class in a plugin's init.rb file (to include a module with new functionality) it becomes impossible to create a new database (which is very inconvenient when running for example tests on a CI system) since the system complains about the User table not yet existing.

A class definition should not need to access the database.

#6 Updated by Kevin Fischer about 1 month ago

Trying the patch out with the most recent master, the tests don't seem to pass for SQLite.
I will fix the patch and post an updated version soon.

#7 Updated by Marius BALTEANU about 1 month ago

  • Related to Defect #33121: IssueQuery not usable from plugins added

#8 Updated by Marius BALTEANU about 1 month ago

  • Category set to Plugin API
  • Assignee set to Jean-Philippe Lang
  • Target version set to 4.1.2

Jean-Philippe, can you take a look on this and the related issue (#33121)?

#9 Updated by Kevin Fischer 22 days ago

I fixed the patch so it works for SQLite, too. Also, I refactored the code around `groupable` a little bit more to make it more obvious that it is a boolean property.

Also available in: Atom PDF