Add rollback queue, like commit queue.

This commit is contained in:
voussoir 2020-02-19 23:46:30 -08:00
parent 8c854f3b6a
commit 0a984117c5
2 changed files with 26 additions and 6 deletions

View file

@ -1033,16 +1033,22 @@ class Photo(ObjectBase):
if new_path.normcase == old_path.normcase: if new_path.normcase == old_path.normcase:
# If they are equivalent but differently cased, just rename. # If they are equivalent but differently cased, just rename.
action = os.rename self.photodb.on_commit_queue.append({
args = [old_path.absolute_path, new_path.absolute_path] 'action': os.rename,
'args': [old_path.absolute_path, new_path.absolute_path],
})
else: else:
# Delete the original, leaving only the new copy / hardlink. # Delete the original, leaving only the new copy / hardlink.
action = os.remove self.photodb.on_commit_queue.append({
args = [old_path.absolute_path] 'action': os.remove,
'args': [old_path.absolute_path],
})
self.photodb.on_rollback_queue.append({
'action': os.remove,
'args': [new_path.absolute_path],
})
self._uncache() self._uncache()
queue_action = {'action': action, 'args': args}
self.photodb.on_commit_queue.append(queue_action)
self.__reinit__() self.__reinit__()

View file

@ -671,6 +671,7 @@ class PDBSQLMixin:
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.on_commit_queue = [] self.on_commit_queue = []
self.on_rollback_queue = []
self.savepoints = [] self.savepoints = []
def assert_table_exists(self, table): def assert_table_exists(self, table):
@ -689,6 +690,7 @@ class PDBSQLMixin:
args = task.get('args', []) args = task.get('args', [])
kwargs = task.get('kwargs', {}) kwargs = task.get('kwargs', {})
task['action'](*args, **kwargs) task['action'](*args, **kwargs)
self.savepoints.clear() self.savepoints.clear()
self.sql.commit() self.sql.commit()
@ -739,6 +741,17 @@ class PDBSQLMixin:
self.log.debug('Nothing to roll back.') self.log.debug('Nothing to roll back.')
return return
while len(self.on_rollback_queue) > 0:
task = self.on_rollback_queue.pop(-1)
if task == savepoint:
break
if isinstance(task, str):
# Intermediate savepoints.
continue
args = task.get('args', [])
kwargs = task.get('kwargs', {})
task['action'](*args, **kwargs)
if savepoint is not None: if savepoint is not None:
self.log.debug('Rolling back to %s', savepoint) self.log.debug('Rolling back to %s', savepoint)
self.sql_execute(f'ROLLBACK TO "{savepoint}"') self.sql_execute(f'ROLLBACK TO "{savepoint}"')
@ -761,6 +774,7 @@ class PDBSQLMixin:
self.sql.execute(query) self.sql.execute(query)
self.savepoints.append(savepoint_id) self.savepoints.append(savepoint_id)
self.on_commit_queue.append(savepoint_id) self.on_commit_queue.append(savepoint_id)
self.on_rollback_queue.append(savepoint_id)
return savepoint_id return savepoint_id
def sql_delete(self, table, pairs): def sql_delete(self, table, pairs):