| 1 |
import re |
|---|
| 2 |
import time |
|---|
| 3 |
from usermanual import * |
|---|
| 4 |
from trac.log import logger_factory |
|---|
| 5 |
from trac.ticket import ITicketChangeListener, Ticket |
|---|
| 6 |
from trac.core import * |
|---|
| 7 |
from trac.env import IEnvironmentSetupParticipant |
|---|
| 8 |
from trac.perm import IPermissionRequestor, PermissionSystem |
|---|
| 9 |
from webui import * |
|---|
| 10 |
from webadminui import * |
|---|
| 11 |
from ticket_filter import * |
|---|
| 12 |
from timeline_hook import * |
|---|
| 13 |
from ticket_daemon import * |
|---|
| 14 |
try: |
|---|
| 15 |
from xmlrpc import * |
|---|
| 16 |
except: |
|---|
| 17 |
pass |
|---|
| 18 |
|
|---|
| 19 |
class WorkLogSetupParticipant(Component): |
|---|
| 20 |
implements(IEnvironmentSetupParticipant) |
|---|
| 21 |
|
|---|
| 22 |
db_version_key = None |
|---|
| 23 |
db_version = None |
|---|
| 24 |
db_installed_version = None |
|---|
| 25 |
|
|---|
| 26 |
"""Extension point interface for components that need to participate in the |
|---|
| 27 |
creation and upgrading of Trac environments, for example to create |
|---|
| 28 |
additional database tables.""" |
|---|
| 29 |
def __init__(self): |
|---|
| 30 |
self.db_version_key = 'WorklogPlugin_Db_Version' |
|---|
| 31 |
self.db_version = 3 |
|---|
| 32 |
self.db_installed_version = None |
|---|
| 33 |
|
|---|
| 34 |
# Initialise database schema version tracking. |
|---|
| 35 |
db = self.env.get_db_cnx() |
|---|
| 36 |
cursor = db.cursor() |
|---|
| 37 |
cursor.execute("SELECT value FROM system WHERE name=%s", (self.db_version_key,)) |
|---|
| 38 |
try: |
|---|
| 39 |
self.db_installed_version = int(cursor.fetchone()[0]) |
|---|
| 40 |
except: |
|---|
| 41 |
self.db_installed_version = 0 |
|---|
| 42 |
cursor.execute("INSERT INTO system (name,value) VALUES(%s,%s)", |
|---|
| 43 |
(self.db_version_key, self.db_installed_version)) |
|---|
| 44 |
db.commit() |
|---|
| 45 |
db.close() |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
def environment_created(self): |
|---|
| 49 |
"""Called when a new Trac environment is created.""" |
|---|
| 50 |
if self.environment_needs_upgrade(None): |
|---|
| 51 |
self.upgrade_environment(None) |
|---|
| 52 |
|
|---|
| 53 |
def system_needs_upgrade(self): |
|---|
| 54 |
return self.db_installed_version < self.db_version |
|---|
| 55 |
|
|---|
| 56 |
def do_db_upgrade(self): |
|---|
| 57 |
# Legacy support hack (supports upgrades from revisions r2495 or before) |
|---|
| 58 |
if self.db_installed_version == 0: |
|---|
| 59 |
db = self.env.get_db_cnx() |
|---|
| 60 |
cursor = db.cursor() |
|---|
| 61 |
try: |
|---|
| 62 |
cursor.execute('SELECT * FROM work_log LIMIT 1') |
|---|
| 63 |
# We've succeeded so we actually have version 1 |
|---|
| 64 |
self.db_installed_version = 1 |
|---|
| 65 |
except: |
|---|
| 66 |
pass |
|---|
| 67 |
db.close() |
|---|
| 68 |
# End Legacy support hack |
|---|
| 69 |
|
|---|
| 70 |
db = self.env.get_db_cnx() |
|---|
| 71 |
cursor = db.cursor() |
|---|
| 72 |
|
|---|
| 73 |
# Do the staged updates |
|---|
| 74 |
try: |
|---|
| 75 |
# This var is to deal with a problem case with pgsql and the "user" |
|---|
| 76 |
# keyword. We need to skip over new installations but not upgrades |
|---|
| 77 |
# for other db backends. |
|---|
| 78 |
skip = False |
|---|
| 79 |
|
|---|
| 80 |
if self.db_installed_version < 1: |
|---|
| 81 |
print 'Creating work_log table' |
|---|
| 82 |
cursor.execute('CREATE TABLE work_log (' |
|---|
| 83 |
'worker TEXT,' |
|---|
| 84 |
'ticket INTEGER,' |
|---|
| 85 |
'lastchange INTEGER,' |
|---|
| 86 |
'starttime INTEGER,' |
|---|
| 87 |
'endtime INTEGER' |
|---|
| 88 |
')') |
|---|
| 89 |
skip = True |
|---|
| 90 |
|
|---|
| 91 |
if self.db_installed_version < 2: |
|---|
| 92 |
print 'Updating work_log table (v2)' |
|---|
| 93 |
cursor.execute('ALTER TABLE work_log ' |
|---|
| 94 |
'ADD COLUMN comment TEXT') |
|---|
| 95 |
|
|---|
| 96 |
if self.db_installed_version < 3: |
|---|
| 97 |
print 'Updating work_log table (v3)' |
|---|
| 98 |
if not skip: |
|---|
| 99 |
# This whole section is just to rename the "user" column to "worker" |
|---|
| 100 |
# This column used to be created in step 1 above, but we |
|---|
| 101 |
# can no longer do this in order to support pgsql. |
|---|
| 102 |
# This step is skipped if step 1 was also run (e.g. new installs) |
|---|
| 103 |
# The below seems to be the only way to rename (or drop) a column on sqlite *sigh* |
|---|
| 104 |
cursor.execute('CREATE TABLE work_log_tmp (' |
|---|
| 105 |
'worker TEXT,' |
|---|
| 106 |
'ticket INTEGER,' |
|---|
| 107 |
'lastchange INTEGER,' |
|---|
| 108 |
'starttime INTEGER,' |
|---|
| 109 |
'endtime INTEGER,' |
|---|
| 110 |
'comment TEXT' |
|---|
| 111 |
')') |
|---|
| 112 |
cursor.execute('INSERT INTO work_log_tmp (worker, ticket, lastchange, starttime, endtime, comment) ' |
|---|
| 113 |
'SELECT user, ticket, lastchange, starttime, endtime, comment FROM work_log') |
|---|
| 114 |
cursor.execute('DROP TABLE work_log') |
|---|
| 115 |
cursor.execute('ALTER TABLE work_log_tmp RENAME TO work_log') |
|---|
| 116 |
|
|---|
| 117 |
#if self.db_installed_version < 4: |
|---|
| 118 |
# print 'Updating work_log table (v4)' |
|---|
| 119 |
# cursor.execute('...') |
|---|
| 120 |
|
|---|
| 121 |
# Updates complete, set the version |
|---|
| 122 |
cursor.execute("UPDATE system SET value=%s WHERE name=%s", |
|---|
| 123 |
(self.db_version, self.db_version_key)) |
|---|
| 124 |
db.commit() |
|---|
| 125 |
db.close() |
|---|
| 126 |
except Exception, e: |
|---|
| 127 |
self.log.error("WorklogPlugin Exception: %s" % (e,)); |
|---|
| 128 |
db.rollback() |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
def needs_user_man(self): |
|---|
| 133 |
db = self.env.get_db_cnx() |
|---|
| 134 |
cursor = db.cursor() |
|---|
| 135 |
try: |
|---|
| 136 |
cursor.execute('SELECT MAX(version) FROM wiki WHERE name=%s', (user_manual_wiki_title,)) |
|---|
| 137 |
maxversion = int(cursor.fetchone()[0]) |
|---|
| 138 |
except: |
|---|
| 139 |
maxversion = 0 |
|---|
| 140 |
db.close() |
|---|
| 141 |
|
|---|
| 142 |
return maxversion < user_manual_version |
|---|
| 143 |
|
|---|
| 144 |
def do_user_man_update(self): |
|---|
| 145 |
when = int(time.time()) |
|---|
| 146 |
db = self.env.get_db_cnx() |
|---|
| 147 |
cursor = db.cursor() |
|---|
| 148 |
cursor.execute('INSERT INTO wiki (name,version,time,author,ipnr,text,comment,readonly) ' |
|---|
| 149 |
'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', |
|---|
| 150 |
(user_manual_wiki_title, user_manual_version, when, |
|---|
| 151 |
'WorkLog Plugin', '127.0.0.1', user_manual_content, |
|---|
| 152 |
'', 0)) |
|---|
| 153 |
db.commit() |
|---|
| 154 |
db.close() |
|---|
| 155 |
|
|---|
| 156 |
def environment_needs_upgrade(self, db): |
|---|
| 157 |
"""Called when Trac checks whether the environment needs to be upgraded. |
|---|
| 158 |
|
|---|
| 159 |
Should return `True` if this participant needs an upgrade to be |
|---|
| 160 |
performed, `False` otherwise. |
|---|
| 161 |
|
|---|
| 162 |
""" |
|---|
| 163 |
return (self.system_needs_upgrade() \ |
|---|
| 164 |
or self.needs_user_man()) |
|---|
| 165 |
|
|---|
| 166 |
def upgrade_environment(self, db): |
|---|
| 167 |
"""Actually perform an environment upgrade. |
|---|
| 168 |
|
|---|
| 169 |
Implementations of this method should not commit any database |
|---|
| 170 |
transactions. This is done implicitly after all participants have |
|---|
| 171 |
performed the upgrades they need without an error being raised. |
|---|
| 172 |
""" |
|---|
| 173 |
def p(s): |
|---|
| 174 |
print s |
|---|
| 175 |
return True |
|---|
| 176 |
print "Worklog needs an upgrade" |
|---|
| 177 |
if self.system_needs_upgrade(): |
|---|
| 178 |
p("Upgrading Database") |
|---|
| 179 |
self.do_db_upgrade() |
|---|
| 180 |
if self.needs_user_man(): |
|---|
| 181 |
p("Upgrading usermanual") |
|---|
| 182 |
self.do_user_man_update() |
|---|
| 183 |
print "Done upgrading Worklog" |
|---|