Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issues encountered after upgrading to Trac 0.12 running on PostgreSQL #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions tracsubtickets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

# i18n support for plugins, available since Trac r7705
# use _, tag_ and N_ as usual, e.g. _("this is a message text")
_, tag_, N_, add_domain = domain_functions('tracsubtickets',
_, tag_, N_, add_domain = domain_functions('tracsubtickets',
'_', 'tag_', 'N_', 'add_domain')


Expand All @@ -71,8 +71,7 @@ def environment_created(self):

def environment_needs_upgrade(self, db):
cursor = db.cursor()
cursor.execute("SELECT value FROM system WHERE name=%s",
(db_default.name, ))
cursor.execute("SELECT value FROM system WHERE name=%s", (db_default.name, ))
value = cursor.fetchone()
try:
self.found_db_version = int(value[0])
Expand All @@ -95,17 +94,15 @@ def upgrade_environment(self, db):
old_data = {} # {table.name: (cols, rows)}
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))
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))
cursor.execute("UPDATE system SET value=%s WHERE name=%s", (db_default.version, db_default.name))
for table in db_default.tables:
cursor.execute("SELECT * FROM %s", (table.name, ))
cols = [x[0] for x in cursor.description],
rows = cursor.fetchall()
old_data[table.name] = (cols, rows)
cursor.execute("DROP TABLE %s", (table.name))
cursor.execute("DROP TABLE %s", (table.name, ))

# insert the default table
for table in db_default.tables:
Expand All @@ -132,13 +129,15 @@ def ticket_created(self, ticket):
self.ticket_changed(ticket, '', ticket['reporter'], {'parents': ''})

def ticket_changed(self, ticket, comment, author, old_values):
self.log.debug("Ticket changed. Ticket id: %s" % ticket.id)
if 'parents' not in old_values:
return

old_parents = old_values.get('parents', '') or ''
old_parents = set(NUMBERS_RE.findall(old_parents))
new_parents = set(NUMBERS_RE.findall(ticket['parents'] or ''))

self.log.debug("Old parents: %s" % old_parents)
self.log.debug("New parents: %s" % new_parents)
if new_parents == old_parents:
return

Expand All @@ -147,19 +146,18 @@ def ticket_changed(self, ticket, comment, author, old_values):

# remove old parents
for parent in old_parents - new_parents:
cursor.execute("DELETE FROM subtickets WHERE parent=%s AND child=%s",
(parent, ticket.id))
cursor.execute("DELETE FROM subtickets WHERE parent = %s AND child = %s", [str(parent), str(ticket.id)] )

# add new parents
for parent in new_parents - old_parents:
cursor.execute("INSERT INTO subtickets VALUES(%s, %s)",
(parent, ticket.id))
cursor.execute("INSERT INTO subtickets VALUES(%s, %s)", (parent, ticket.id))
db.commit()

def ticket_deleted(self, ticket):
db = self.env.get_db_cnx()
cursor = db.cursor()
# TODO: check if there's any child ticket
cursor.execute("DELETE FROM subtickets WHERE child=%s", (ticket.id, ))
cursor.execute("DELETE FROM subtickets WHERE child=%s", [str(ticket.id)])
db.commit()

# ITicketManipulator methods
Expand Down Expand Up @@ -189,7 +187,7 @@ def validate_ticket(self, req, ticket):
def _check_parents(id, all_parents):
all_parents = all_parents + [id]
errors = []
cursor.execute("SELECT parent FROM subtickets WHERE child=%s", (id, ))
cursor.execute("SELECT parent FROM subtickets WHERE child=%s", [str(id)])
for x in [int(x[0]) for x in cursor]:
if x in all_parents:
error = ' > '.join(['#%s' % n for n in all_parents + [x]])
Expand Down
13 changes: 5 additions & 8 deletions tracsubtickets/web_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@

from api import NUMBERS_RE, _


class SubTicketsModule(Component):

implements(ITemplateProvider,
IRequestFilter,
ITicketManipulator,
Expand Down Expand Up @@ -94,12 +92,13 @@ def prepare_ticket(self, req, ticket, fields, actions):
pass

def get_children(self, parent_id, db=None):
self.log.debug("Get children for parent: %s" % parent_id)
children = {}
if not db:
db = self.env.get_db_cnx()

cursor = db.cursor()
cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s",
(parent_id, ))
cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s", [str(parent_id)] )

for parent, child in cursor:
children[child] = None
Expand All @@ -108,15 +107,13 @@ def get_children(self, parent_id, db=None):
children[id] = self.get_children(id, db)

return children

def validate_ticket(self, req, ticket):
action = req.args.get('action')
if action == 'resolve':
db = self.env.get_db_cnx()
cursor = db.cursor()

cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s",
(ticket.id, ))
cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s", [str(ticket.id)] )

for parent, child in cursor:
if Ticket(self.env, child)['status'] != 'closed':
Expand Down