Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
coderanger committed Jul 4, 2010
1 parent 2ecc525 commit ae03a92
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 11 additions & 3 deletions mastertickets/api.py
Expand Up @@ -57,11 +57,19 @@ def upgrade_environment(self, db):
try:
cursor.execute('SELECT * FROM %s'%tbl.name)
old_data[tbl.name] = ([d[0] for d in cursor.description], cursor.fetchall())
except Exception, e:
if 'OperationalError' not in e.__class__.__name__:
raise e # If it is an OperationalError, keep going
try:
cursor.execute('DROP TABLE %s'%tbl.name)
except Exception, e:
if 'OperationalError' not in e.__class__.__name__:
raise e # If it is an OperationalError, just move on to the next table


for vers, migration in db_default.migrations:
if self.found_db_version in vers:
self.log.info('MasterTicketsSystem: Running migration %s', migration.__doc__)
migration(old_data)

for tbl in db_default.tables:
for sql in db_manager.to_sql(tbl):
Expand Down Expand Up @@ -100,8 +108,8 @@ def ticket_changed(self, tkt, comment, author, old_values):
db = self.env.get_db_cnx()

links = TicketLinks(self.env, tkt, db)
links.blocking = set(self.NUMBERS_RE.findall(tkt['blocking'] or ''))
links.blocked_by = set(self.NUMBERS_RE.findall(tkt['blockedby'] or ''))
links.blocking = set(int(n) for n self.NUMBERS_RE.findall(tkt['blocking'] or ''))
links.blocked_by = set(int(n) for n self.NUMBERS_RE.findall(tkt['blockedby'] or ''))
links.save(author, comment, tkt.time_changed, db)

db.commit()
Expand Down
16 changes: 13 additions & 3 deletions mastertickets/db_default.py
Expand Up @@ -4,10 +4,20 @@
from trac.db import Table, Column

name = 'mastertickets'
version = 1
version = 2
tables = [
Table('mastertickets', key=('source','dest'))[
Column('source'),
Column('dest'),
Column('source', type='integer'),
Column('dest', type='integer'),
],
]

def convert_to_int(data):
"""Convert both source and dest in the mastertickets table to ints."""
for row in data['mastertickets'][1]:
for i, (n1, n2) in enumerate(row):
row[i] = [int(n1), int(n2)]

migrations = [
(xrange(1,2), convert_to_int),
]
13 changes: 8 additions & 5 deletions mastertickets/model.py
Expand Up @@ -20,11 +20,11 @@ def __init__(self, env, tkt, db=None):
cursor = db.cursor()

cursor.execute('SELECT dest FROM mastertickets WHERE source=%s ORDER BY dest', (self.tkt.id,))
self.blocking = set([num for num, in cursor])
self.blocking = set([int(num) for num, in cursor])
self._old_blocking = copy.copy(self.blocking)

cursor.execute('SELECT source FROM mastertickets WHERE dest=%s ORDER BY source', (self.tkt.id,))
self.blocked_by = set([num for num, in cursor])
self.blocked_by = set([int(num) for num, in cursor])
self._old_blocked_by = copy.copy(self.blocked_by)

def save(self, author, comment='', when=None, db=None):
Expand All @@ -39,10 +39,13 @@ def save(self, author, comment='', when=None, db=None):
handle_commit = True
cursor = db.cursor()

new_blocking = set(int(n) for n in self.blocking)
new_blocked_by = set(int(n) for n in self.blocked_by)

to_check = [
# new, old, field
(self.blocking, self._old_blocking, 'blockedby', ('source', 'dest')),
(self.blocked_by, self._old_blocked_by, 'blocking', ('dest', 'source')),
(new_blocking, self._old_blocking, 'blockedby', ('source', 'dest')),
(new_blocked_by, self._old_blocked_by, 'blocking', ('dest', 'source')),
]

for new_ids, old_ids, field, sourcedest in to_check:
Expand All @@ -59,7 +62,7 @@ def save(self, author, comment='', when=None, db=None):

if update_field is not None:
cursor.execute('SELECT value FROM ticket_custom WHERE ticket=%s AND name=%s',
(n, field))
(n, str(field)))
old_value = (cursor.fetchone() or ('',))[0]
new_value = [x.strip() for x in old_value.split(',') if x.strip()]
update_field(new_value)
Expand Down

0 comments on commit ae03a92

Please sign in to comment.