Skip to content

Commit

Permalink
web_ui: make the backlog queries compatible with MySQL
Browse files Browse the repository at this point in the history
The issue is that the databases are all a bit picky about the column
type in a cast.  MySQL wants `signed`, PostgreSQL and SQLite prefer
`int` or `integer`.  As a result, we have to customize the query based
on the database being used.  Trac has a `cast()` method on the `db`
object, so let's use it to get the query into the right form.
  • Loading branch information
jszakmeister committed Apr 29, 2013
1 parent 965de20 commit 0d9f31b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Expand Up @@ -13,6 +13,8 @@ In Development
* Changed queries to use single quotes for better compatibility with
PostgreSQL.

* Updated queries to use the `db.cast()` method for compatibility with MySQL.

TracBacklog 0.3.0 (March 1, 2013)

* Added the ability to send tickets to the top or bottom via a context menu.
Expand Down
50 changes: 23 additions & 27 deletions backlog/web_ui.py
Expand Up @@ -24,30 +24,6 @@
from backlog.schema import schema_version, schema


BACKLOG_QUERY = '''SELECT id FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN backlog bp ON bp.ticket_id = t.id
WHERE status <> 'closed' AND milestone = %s
ORDER BY bp.rank, CAST(p.value AS int), t.type, time
'''

# Need a separate unscheduled backlog query since deleting a
# milestone and re-targeting a ticket can set the milestone field
# to null, instead of the empty string.
UNSCHEDULED_BACKLOG_QUERY = '''SELECT id FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
LEFT JOIN backlog bp ON bp.ticket_id = t.id
WHERE status <> 'closed' AND (milestone = '' or milestone is null)
ORDER BY bp.rank, CAST(p.value AS int), t.type, time
'''

MILESTONE_QUERY = '''SELECT name, due FROM milestone
WHERE completed = 0
ORDER BY (due = 0), due, UPPER(name), name
'''



class BacklogPlugin(Component):
implements(INavigationContributor, IRequestHandler,
IEnvironmentSetupParticipant, ITemplateProvider,
Expand Down Expand Up @@ -250,16 +226,34 @@ def __init__(self):

return 'backlog.html', data, None



def _get_active_tickets(self, milestone = None):
db = self.env.get_db_cnx()
cursor = db.cursor()

try:
if milestone is None:
# Check the comment for UNSCHEDULED_BACKLOG_QUERY about
# why this is necessary.
# Need a separate unscheduled backlog query since deleting a
# milestone and re-targeting a ticket can set the milestone field
# to null, instead of the empty string.
UNSCHEDULED_BACKLOG_QUERY = '''SELECT id FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND
p.type = 'priority'
LEFT JOIN backlog bp ON bp.ticket_id = t.id
WHERE status <> 'closed' AND
(milestone = '' or milestone is null)
ORDER BY bp.rank, %s, t.type, time
''' % db.cast('p.value', 'int')
cursor.execute(UNSCHEDULED_BACKLOG_QUERY)
else:
BACKLOG_QUERY = '''SELECT id FROM ticket t
LEFT JOIN enum p ON p.name = t.priority AND
p.type = 'priority'
LEFT JOIN backlog bp ON bp.ticket_id = t.id
WHERE status <> 'closed' AND milestone = %%s
ORDER BY bp.rank, %s, t.type, time
''' % db.cast('p.value', 'int')
cursor.execute(BACKLOG_QUERY, (milestone,))
except:
db.rollback()
Expand Down Expand Up @@ -402,7 +396,9 @@ def _get_active_milestones(self, exclude = None):
results.append(
dict(name='(unscheduled)', due='--', num_tickets=num_tickets))

cursor.execute(MILESTONE_QUERY)
cursor.execute('''SELECT name, due FROM milestone
WHERE completed = 0
ORDER BY (due = 0), due, UPPER(name), name''')

rows = cursor.fetchall()

Expand Down

0 comments on commit 0d9f31b

Please sign in to comment.