Skip to content

Commit

Permalink
be compatible with Trac 1.1 - replace deprecated get_db_cnx method
Browse files Browse the repository at this point in the history
  • Loading branch information
miihael committed Jun 23, 2015
1 parent b39b72b commit 3f64452
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 177 deletions.
142 changes: 68 additions & 74 deletions mastertickets/api.py
Expand Up @@ -30,20 +30,21 @@ class MasterTicketsSystem(Component):
# IEnvironmentSetupParticipant methods
def environment_created(self):
self.found_db_version = 0
self.upgrade_environment(self.env.get_db_cnx())

def environment_needs_upgrade(self, db):
cursor = db.cursor()
cursor.execute("SELECT value FROM system WHERE name=%s", (db_default.name,))
value = cursor.fetchone()
if not value:
self.found_db_version = 0
return True
else:
self.found_db_version = int(value[0])
#self.log.debug('WeatherWidgetSystem: Found db version %s, current is %s' % (self.found_db_version, db_default.version))
if self.found_db_version < db_default.version:
self.upgrade_environment()

def environment_needs_upgrade(self):
with self.env.db_query as db:
cursor = db.cursor()
cursor.execute("SELECT value FROM system WHERE name=%s", (db_default.name,))
value = cursor.fetchone()
if not value:
self.found_db_version = 0
return True
else:
self.found_db_version = int(value[0])
#self.log.debug('WeatherWidgetSystem: Found db version %s, current is %s' % (self.found_db_version, db_default.version))
if self.found_db_version < db_default.version:
return True

# Check for our custom fields
if 'blocking' not in self.config['ticket-custom'] or 'blockedby' not in self.config['ticket-custom']:
Expand All @@ -52,49 +53,49 @@ def environment_needs_upgrade(self, db):
# Fall through
return False

def upgrade_environment(self, db):
db_manager, _ = DatabaseManager(self.env)._get_connector()

# Insert the default table
old_data = {} # {table_name: (col_names, [row, ...]), ...}
cursor = db.cursor()
if not self.found_db_version:
cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)", (db_default.name, db_default.version))
else:
cursor.execute("UPDATE system SET value=%s WHERE name=%s", (db_default.version, db_default.name))
for tbl in db_default.tables:
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):
cursor.execute(sql)

# Try to reinsert any old data
if tbl.name in old_data:
data = old_data[tbl.name]
sql = 'INSERT INTO %s (%s) VALUES (%s)' % \
(tbl.name, ','.join(data[0]), ','.join(['%s'] * len(data[0])))
for row in data[1]:
def upgrade_environment(self):
db_manager, _ = DatabaseManager(self.env).get_connector()
with self.env.db_transaction as db:
# Insert the default table
old_data = {} # {table_name: (col_names, [row, ...]), ...}
cursor = db.cursor()
if not self.found_db_version:
cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)", (db_default.name, db_default.version))
else:
cursor.execute("UPDATE system SET value=%s WHERE name=%s", (db_default.version, db_default.name))
for tbl in db_default.tables:
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(sql, row)
cursor.execute('DROP TABLE %s' % tbl.name)
except Exception, e:
if 'OperationalError' not in e.__class__.__name__:
raise e
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):
cursor.execute(sql)

# Try to reinsert any old data
if tbl.name in old_data:
data = old_data[tbl.name]
sql = 'INSERT INTO %s (%s) VALUES (%s)' % \
(tbl.name, ','.join(data[0]), ','.join(['%s'] * len(data[0])))
for row in data[1]:
try:
cursor.execute(sql, row)
except Exception, e:
if 'OperationalError' not in e.__class__.__name__:
raise e

custom = self.config['ticket-custom']
config_dirty = False
Expand All @@ -114,31 +115,22 @@ def ticket_created(self, tkt):
self.ticket_changed(tkt, '', tkt['reporter'], {})

def ticket_changed(self, tkt, comment, author, old_values):
db = self.env.get_db_cnx()
links = self._prepare_links(tkt, db)
links.save(author, comment, tkt.time_changed, db)
db.commit()
links = self._prepare_links(tkt)
links.save(author, comment, tkt.time_changed)

def ticket_deleted(self, tkt):
db = self.env.get_db_cnx()

links = TicketLinks(self.env, tkt, db)
links = TicketLinks(self.env, tkt)
links.blocking = set()
links.blocked_by = set()
links.save('trac', 'Ticket #%s deleted' % tkt.id, when=None, db=db)

db.commit()
links.save('trac', 'Ticket #%s deleted' % tkt.id, when=None)

# ITicketManipulator methods
def prepare_ticket(self, req, ticket, fields, actions):
pass

def validate_ticket(self, req, ticket):
db = self.env.get_db_cnx()
cursor = db.cursor()

tid = ticket.id
links = self._prepare_links(ticket, db)
links = self._prepare_links(ticket)

if req.args.get('action') == 'resolve' and req.args.get('action_resolve_resolve_resolution') == 'fixed':
for i in links.blocked_by:
Expand All @@ -165,19 +157,21 @@ def validate_ticket(self, req, ticket):
for field in ('blocking', 'blockedby'):
try:
ids = self.NUMBERS_RE.findall(ticket[field] or '')
for tid in ids[:]:
cursor.execute('SELECT id FROM ticket WHERE id=%s', (tid,))
row = cursor.fetchone()
if row is None:
ids.remove(tid)
with self.env.db_query as db:
cursor = db.cursor()
for tid in ids[:]:
cursor.execute('SELECT id FROM ticket WHERE id=%s', (tid,))
row = cursor.fetchone()
if row is None:
ids.remove(tid)
ticket[field] = ', '.join(sorted(ids, key=lambda x: int(x)))
except Exception, e:
self.log.debug('MasterTickets: Error parsing %s "%s": %s', field, ticket[field], e)
yield field, 'Not a valid list of ticket IDs'

# Internal methods
def _prepare_links(self, tkt, db):
links = TicketLinks(self.env, tkt, db)
def _prepare_links(self, tkt):
links = TicketLinks(self.env, tkt)
links.blocking = set(int(n) for n in self.NUMBERS_RE.findall(tkt['blocking'] or ''))
links.blocked_by = set(int(n) for n in self.NUMBERS_RE.findall(tkt['blockedby'] or ''))
return links

0 comments on commit 3f64452

Please sign in to comment.