From 0a984117c5a9d1a83980518a2d68e9345d1ddff1 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 19 Feb 2020 23:46:30 -0800 Subject: [PATCH] Add rollback queue, like commit queue. --- etiquette/objects.py | 18 ++++++++++++------ etiquette/photodb.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 9ced37a..e121b89 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -1033,16 +1033,22 @@ class Photo(ObjectBase): if new_path.normcase == old_path.normcase: # If they are equivalent but differently cased, just rename. - action = os.rename - args = [old_path.absolute_path, new_path.absolute_path] + self.photodb.on_commit_queue.append({ + 'action': os.rename, + 'args': [old_path.absolute_path, new_path.absolute_path], + }) else: # Delete the original, leaving only the new copy / hardlink. - action = os.remove - args = [old_path.absolute_path] + self.photodb.on_commit_queue.append({ + 'action': os.remove, + 'args': [old_path.absolute_path], + }) + self.photodb.on_rollback_queue.append({ + 'action': os.remove, + 'args': [new_path.absolute_path], + }) self._uncache() - queue_action = {'action': action, 'args': args} - self.photodb.on_commit_queue.append(queue_action) self.__reinit__() diff --git a/etiquette/photodb.py b/etiquette/photodb.py index e352040..4fb46ef 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -671,6 +671,7 @@ class PDBSQLMixin: def __init__(self): super().__init__() self.on_commit_queue = [] + self.on_rollback_queue = [] self.savepoints = [] def assert_table_exists(self, table): @@ -689,6 +690,7 @@ class PDBSQLMixin: args = task.get('args', []) kwargs = task.get('kwargs', {}) task['action'](*args, **kwargs) + self.savepoints.clear() self.sql.commit() @@ -739,6 +741,17 @@ class PDBSQLMixin: self.log.debug('Nothing to roll back.') 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: self.log.debug('Rolling back to %s', savepoint) self.sql_execute(f'ROLLBACK TO "{savepoint}"') @@ -761,6 +774,7 @@ class PDBSQLMixin: self.sql.execute(query) self.savepoints.append(savepoint_id) self.on_commit_queue.append(savepoint_id) + self.on_rollback_queue.append(savepoint_id) return savepoint_id def sql_delete(self, table, pairs):