[ZODB-Dev] simple example of undo( )

morgen@osafoundation.org morgen@osafoundation.org
Mon, 23 Sep 2002 16:31:58 -0700


Hello,

While looking for a python object persistence mechanism, I came across
ZODB, and it looks promising.  However, I'm not sure I understand how
the undo( ) method is supposed to work.  I wrote a small script which
sets the property of a persistent object, commits, sets the property
again, commits, and then does an undo.  I was expecting the property
to be reverted back to the previous value but it isn't.  (The script is
included below)

First, is this list the right venue for a question like this?  If so,
am I misunderstanding how undo should work?  This is using ZODB3-3.1b1 &
Python 2.2.1.

Thank you in advance,
~morgen

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
import sys, ZODB
from ZODB.DB import DB
from ZODB.FileStorage import FileStorage
from Persistence import Persistent
from BTrees import IOBTree

# a simple class
class Item( Persistent ):
    pass

# Set up the database:
storage = FileStorage( 'UNDO.fs' )
db = DB( storage )
conn = db.open( )
dbroot = conn.root( )
if not dbroot.has_key( 'items' ):
    dbroot[ 'items' ] = IOBTree.IOBTree( )
    items = dbroot[ 'items' ]
# items is now persistent

# Does FileStorage support undo?
if storage.supportsUndo( ):
    print "Supports Undo"
else:
    print "Does NOT support Undo"

# Create an item and set a property a couple times:
item = Item( )
items[ 1 ] = item

item.foo = "one"
get_transaction( ).note( "Changing foo" )
get_transaction( ).commit( )
print "should be one:", item.foo

item.foo = "two"
get_transaction( ).note( "Changing foo again" )
get_transaction( ).commit( )
print "should be two:", item.foo

# Look at the undo log:
undolog = storage.undoLog( 0, sys.maxint )
for entry in undolog:
    print entry

# Pull out the last transaction id:
last_transaction_id = undolog[ 0 ][ 'id' ]
print last_transaction_id

db.undo( last_transaction_id )

# I would expect the value of item.foo to be reverted to 'one':
print "should be one:", item.foo

db.close( )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

The script's output is as follows:

Supports Undo
should be one: one
should be two: two
{'description': 'Changing foo again', 'user_name': '', 'id': 'A0evPSVjXio=', 'time': 1032823508.762849}
{'description': 'Changing foo', 'user_name': '', 'id': 'A0evPSSWEiI=', 'time': 1032823508.574894}
{'description': 'initial database creation', 'user_name': '', 'id': 'A0evPSSAsfc=', 'time': 1032823508.555324}
A0evPSVjXio=
should be one: two