Skip to content

Instantly share code, notes, and snippets.

@lonetwin
Created June 23, 2011 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lonetwin/1042239 to your computer and use it in GitHub Desktop.
Save lonetwin/1042239 to your computer and use it in GitHub Desktop.
Snippet to undo zodb transactions upto a specific point in the past
#!/usr/bin/python
# zodb_undo.py - Snippet to undo zodb transactions upto a specific point in the past
zodb_file = "var/karl.db"
undo_back_to = (2011, 6, 15, 11, 00, 00, 00, 00, 00) # time tuple
import time
import transaction
from ZODB import FileStorage, DB
# init the storage object
storage = FileStorage.FileStorage(zodb_file)
# create a db object
db = DB(storage)
# now, zope records all transactions in a log which is available as
# the db.undoLog(). Note that at the time of this writing the zope
# documentation is incorrect (bug report:
# https://bugs.launchpad.net/zodb/+bug/622828). The first and second
# parameters are the index within the list of undoable transactions,
# instead of time in seconds since epoch.
log = db.undoLog(0, sys.maxint)
# We wanted to undo transactions based on the time, since we knew
# exactly when the delete occured. However, undo-ing based on time is
# not necessary. undoLog() returns back a list of dicts with the keys,
# 'id' (unique identifier for the transaction), 'time' (time measured
# in seconds since epoch), 'description' (the .description attribute
# of the transaction) and 'user' (the .user attribute of the
# transaction). We could use any of these keys.
t = time.mktime(undo_back_to)
undo_list = []
for i in log:
if i['time'] >= t:
undo_list.append(i['id'])
for i in range(len(undo_list)): # Doing it this way so that if for
tid = undo_list.pop(0) # some reason the undo fails, we'd at
db.undo(tid) # least know which ones did not go
transaction.commit() # through
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment