Simplify and improve rollback logic.

This commit is contained in:
voussoir 2018-07-29 16:28:57 -07:00
parent 30d96139c2
commit 30e3aa9c6f
2 changed files with 27 additions and 23 deletions

View file

@ -408,6 +408,10 @@ def seconds_to_hms(seconds):
hms = ':'.join(f'{part:02d}' for part in parts) hms = ':'.join(f'{part:02d}' for part in parts)
return hms return hms
def slice_before(li, item):
index = li.index(item)
return li[:index]
def split_easybake_string(ebstring): def split_easybake_string(ebstring):
''' '''
Given an easybake string, return (tagname, synonym, rename_to), where Given an easybake string, return (tagname, synonym, rename_to), where

View file

@ -693,32 +693,32 @@ class PDBSQLMixin:
self.sql.commit() self.sql.commit()
def rollback(self, savepoint=None): def rollback(self, savepoint=None):
if savepoint is not None: '''
valid_savepoint = savepoint in self.savepoints Given a savepoint, roll the database back to the moment before that
else: savepoint was created. Keep in mind that a @transaction savepoint is
valid_savepoint = None always created *before* the method actually does anything.
if valid_savepoint is False: If no savepoint is provided then just roll back the most recent save.
self.log.warn('Tried to restore to a nonexistent savepoint. Did you commit too early?') '''
if savepoint is None:
if len(self.savepoints) == 0: try:
savepoint = self.savepoints.pop(-1)
except IndexError:
self.log.debug('Nothing to roll back.') self.log.debug('Nothing to roll back.')
return return
if valid_savepoint:
restore_to = savepoint
while self.savepoints.pop(-1) != restore_to:
pass
else: else:
restore_to = self.savepoints.pop(-1) try:
# Will reassign after everything goes well.
_savepoints = helpers.slice_before(self.savepoints, savepoint)
except ValueError:
self.log.warn('Tried to restore nonexistent savepoint %s.', savepoint)
return
self.log.debug('Rolling back to %s', restore_to) self.log.debug('Rolling back to %s', savepoint)
query = 'ROLLBACK TO "%s"' % restore_to query = 'ROLLBACK TO "%s"' % savepoint
self.sql_execute(query) self.sql_execute(query)
while len(self.on_commit_queue) > 0: self.savepoints = _savepoints
item = self.on_commit_queue.pop(-1) self.on_commit_queue = helpers.slice_before(self.on_commit_queue, savepoint)
if item == restore_to:
break
def savepoint(self, message=None): def savepoint(self, message=None):
savepoint_id = helpers.random_hex(length=16) savepoint_id = helpers.random_hex(length=16)