| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (c) 2007-2012 Colin Guthrie <trac@colin.guthr.ie> |
|---|
| 4 | # Copyright (c) 2011-2016 Ryan J Ollos <ryan.j.ollos@gmail.com> |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. |
|---|
| 9 | |
|---|
| 10 | from datetime import datetime |
|---|
| 11 | |
|---|
| 12 | from trac.core import * |
|---|
| 13 | from trac.db import Column, DatabaseManager, Table |
|---|
| 14 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 15 | from trac.util.datefmt import to_utimestamp, utc |
|---|
| 16 | |
|---|
| 17 | from usermanual import * |
|---|
| 18 | try: |
|---|
| 19 | from xmlrpc import * |
|---|
| 20 | except: |
|---|
| 21 | pass |
|---|
| 22 | |
|---|
| 23 | schema = [ |
|---|
| 24 | Table('work_log')[ |
|---|
| 25 | Column('worker'), |
|---|
| 26 | Column('comment'), |
|---|
| 27 | Column('ticket', type='int'), |
|---|
| 28 | Column('lastchange', type='int'), |
|---|
| 29 | Column('starttime', type='int'), |
|---|
| 30 | Column('endtime', type='int'), |
|---|
| 31 | ] |
|---|
| 32 | ] |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class WorkLogSetupParticipant(Component): |
|---|
| 36 | implements(IEnvironmentSetupParticipant) |
|---|
| 37 | |
|---|
| 38 | db_version_key = 'WorklogPlugin_Db_Version' |
|---|
| 39 | db_version = 3 |
|---|
| 40 | db_installed_version = None |
|---|
| 41 | |
|---|
| 42 | def __init__(self): |
|---|
| 43 | self.db_installed_version = self._get_db_version() |
|---|
| 44 | |
|---|
| 45 | # IEnvironmentSetupParticipant methods |
|---|
| 46 | |
|---|
| 47 | def environment_created(self): |
|---|
| 48 | with self.env.db_transaction as db: |
|---|
| 49 | self.upgrade_environment(db) |
|---|
| 50 | |
|---|
| 51 | def environment_needs_upgrade(self, db): |
|---|
| 52 | return self._system_needs_upgrade() or self._needs_user_man() |
|---|
| 53 | |
|---|
| 54 | def upgrade_environment(self, db): |
|---|
| 55 | print "Worklog needs an upgrade" |
|---|
| 56 | with self.env.db_transaction: |
|---|
| 57 | if self._system_needs_upgrade(): |
|---|
| 58 | print " * Upgrading Database" |
|---|
| 59 | self._do_db_upgrade(db) |
|---|
| 60 | if self._needs_user_man(): |
|---|
| 61 | print " * Upgrading usermanual" |
|---|
| 62 | self._do_user_man_update() |
|---|
| 63 | print "Done upgrading Worklog" |
|---|
| 64 | |
|---|
| 65 | # Internal methods |
|---|
| 66 | |
|---|
| 67 | def _system_needs_upgrade(self): |
|---|
| 68 | return self.db_installed_version < self.db_version |
|---|
| 69 | |
|---|
| 70 | def _do_db_upgrade(self, db): |
|---|
| 71 | with self.env.db_transaction as db: |
|---|
| 72 | skip = False |
|---|
| 73 | if self.db_installed_version < 1: |
|---|
| 74 | print 'Creating work_log table' |
|---|
| 75 | self._create_tables(schema) |
|---|
| 76 | skip = True |
|---|
| 77 | |
|---|
| 78 | if self.db_installed_version < 2: |
|---|
| 79 | if not skip: |
|---|
| 80 | print 'Updating work_log table (v2)' |
|---|
| 81 | db("ALTER TABLE work_log ADD COLUMN comment TEXT") |
|---|
| 82 | |
|---|
| 83 | if self.db_installed_version < 3: |
|---|
| 84 | if not skip: |
|---|
| 85 | print 'Updating work_log table (v3)' |
|---|
| 86 | # Rename 'user' column to 'worker', to support psql |
|---|
| 87 | db(""" |
|---|
| 88 | CREATE TABLE work_log_tmp |
|---|
| 89 | (worker TEXT, comment TEXT, ticket INTEGER, |
|---|
| 90 | lastchange INTEGER, starttime INTEGER, |
|---|
| 91 | endtime INTEGER) |
|---|
| 92 | """) |
|---|
| 93 | db(""" |
|---|
| 94 | INSERT INTO work_log_tmp |
|---|
| 95 | (worker,comment,ticket,lastchange,starttime, |
|---|
| 96 | endtime,comment) |
|---|
| 97 | SELECT user,comment, ticket,lastchange,starttime, |
|---|
| 98 | endtime,comment |
|---|
| 99 | FROM work_log |
|---|
| 100 | """) |
|---|
| 101 | db("DROP TABLE work_log") |
|---|
| 102 | db("ALTER TABLE work_log_tmp RENAME TO work_log") |
|---|
| 103 | |
|---|
| 104 | db("UPDATE system SET value=%s WHERE name=%s", |
|---|
| 105 | (self.db_version, self.db_version_key)) |
|---|
| 106 | |
|---|
| 107 | def _needs_user_man(self): |
|---|
| 108 | for maxversion, in self.env.db_transaction(""" |
|---|
| 109 | SELECT MAX(version) FROM wiki WHERE name=%s |
|---|
| 110 | """, (user_manual_wiki_title,)): |
|---|
| 111 | maxversion = int(maxversion) if maxversion else 0 |
|---|
| 112 | break |
|---|
| 113 | else: |
|---|
| 114 | maxversion = 0 |
|---|
| 115 | |
|---|
| 116 | return maxversion < user_manual_version |
|---|
| 117 | |
|---|
| 118 | def _do_user_man_update(self): |
|---|
| 119 | when = to_utimestamp(datetime.now(utc)) |
|---|
| 120 | self.env.db_transaction(""" |
|---|
| 121 | INSERT INTO wiki |
|---|
| 122 | (name,version,time,author,ipnr,text,comment,readonly) |
|---|
| 123 | VALUES (%s,%s,%s,%s,%s,%s,%s,%s) |
|---|
| 124 | """, (user_manual_wiki_title, user_manual_version, |
|---|
| 125 | when, 'WorkLog Plugin', '127.0.0.1', |
|---|
| 126 | user_manual_content, '', 0)) |
|---|
| 127 | |
|---|
| 128 | def _get_db_version(self): |
|---|
| 129 | with self.env.db_transaction as db: |
|---|
| 130 | for version, in db(""" |
|---|
| 131 | SELECT value FROM system WHERE name=%s |
|---|
| 132 | """, (self.db_version_key,)): |
|---|
| 133 | version = int(version) |
|---|
| 134 | break |
|---|
| 135 | else: |
|---|
| 136 | version = 0 |
|---|
| 137 | db(""" |
|---|
| 138 | INSERT INTO system (name,value) VALUES(%s,%s) |
|---|
| 139 | """, (self.db_version_key, version)) |
|---|
| 140 | return version |
|---|
| 141 | |
|---|
| 142 | def _create_tables(self, schema): |
|---|
| 143 | connector = DatabaseManager(self.env).get_connector()[0] |
|---|
| 144 | with self.env.db_transaction as db: |
|---|
| 145 | for table in schema: |
|---|
| 146 | for sql in connector.to_sql(table): |
|---|
| 147 | db(sql) |
|---|