
def do_upgrade(env, ver, cursor):
    """Upgrade the reports to better handle the new workflow capabilities"""
    db = env.get_db_cnx()
    owner = db.concat('owner', "' *'")
    cursor.execute('SELECT id, query, description FROM report')
    reports = cursor.fetchall()
    for report, query, description in reports:
        # All states other than 'closed' are "active".
        query = query.replace("IN ('new', 'assigned', 'reopened')",
                              "<> 'closed'")
        # Add a status column instead of adding an '*' to the owner's name for
        # the 'assigned' state.
        query = query.replace("(CASE status WHEN 'assigned' THEN %s "
                              "ELSE owner END) AS owner" % owner, "owner, status")
        description = description.replace(" * If a ticket has been accepted, "
                                          "a '*' is appended after the "
                                          "owner's name\n",
                                          '')
        cursor.execute("UPDATE report SET query=%s, description=%s "
                       "WHERE id=%s", (query, description, report))

@@ -313,16 +313,72 @@
 eports:%0A
+        q, d = query, description%0A        if query:%0A    
         
@@ -428,37 +428,33 @@
 e%22.%0A        
+   
-query = query
+ q = q
 .replace(%22IN
@@ -491,38 +491,8 @@
 ')%22,
-%0A                             
  %22%3C%3E
@@ -499,24 +499,28 @@
  'closed'%22)%0A
+    
         # Ad
@@ -585,13 +585,13 @@
 name
- for%0A
+%0A    
     
@@ -595,16 +595,20 @@
        #
+ for
  the 'as
@@ -634,21 +634,17 @@
     
+   
-query = query
+ q = q
 .rep
@@ -684,28 +684,24 @@
 ' THEN %25s %22%0A
-    
             
@@ -775,16 +775,19 @@
         
+if 
 descript
@@ -793,22 +793,27 @@
 tion
- = description
+:%0A            d = d
 .rep
@@ -852,28 +852,26 @@
 cepted, 
-%22%0A          
+a '*' is%22%0A
         
@@ -892,23 +892,9 @@
     
-      %22a '*' is
+%22
  app
@@ -913,53 +913,8 @@
 the 
-%22%0A                                          %22
 owne
@@ -925,16 +925,20 @@
 name%5Cn%22,
+ '')
 %0A       
@@ -942,46 +942,47 @@
     
-                                  '')%0A
+if q != query or d != description:%0A    
     
@@ -1069,16 +1069,20 @@
         
+    
 %22WHERE i
@@ -1090,33 +1090,19 @@
 =%25s%22, (q
-uery, description
+, d
 , report

The db21 upgrade step now copes with a report description or a report query being None